Java RSA3加密时因编码不同导致结果不同

最近遇到一个问题,使用RSA3加密及验证时候,因为我方与客户方工程的编码格式不同,导致我方使用私钥加密后,客户使用公钥解密失败。
最后问题定位到src.getBytes()代码,因我方工程使用GBK编码,src.getBytes()执行时获取默认编码为GBK(即src.getBytes("GBK")),而客户方工程使用UTF-8编码,解密执行src.getBytes()时获取默认编码为UTF-8(即src.getBytes("UTF-8"))。造成解密校验失败。
解决方法:加密时设置UTF-8编码格式—src.getBytes("UTF-8",如下:

测试(file的默认编码为GBK):

valueT.getBytes():
[48, 50, 48, 49, -49, -62, -75, -91, -77, -55, -71, -90, -93, -84, -74, -87, -75, -91, -76, -90, -45, -38, -77, -28, -42, -75, -42, -48, 50, 48, 49, 55, 48, 56, 48, 49, 49, 49, 49, 50, 48, 53, 56, 56, 51, 51, 48, 48, 54, 55, 54, 52, 55, 85, 78, 68, 69, 82, 87, 65, 89, 50, 48, 49, 55, 45, 48, 56, 45, 48, 49, 32, 49, 49, 58, 49, 50, 58, 48, 54]
valueT.getBytes("GBK"):
[48, 50, 48, 49, -49, -62, -75, -91, -77, -55, -71, -90, -93, -84, -74, -87, -75, -91, -76, -90, -45, -38, -77, -28, -42, -75, -42, -48 , 50, 48, 49, 55, 48, 56, 48, 49, 49, 49, 49, 50, 48, 53, 56, 56, 51, 51, 48, 48, 54, 55, 54, 52, 55, 85, 78, 68, 69, 82, 87, 65, 89, 50, 48, 49, 55, 45, 48, 56, 45, 48, 49, 32, 49, 49, 58, 49, 50, 58, 48, 54]
valueT.getBytes("UTF-8"):
[48, 50, 48, 49, -28, -72, -117, -27, -115, -107, -26, -120, -112, -27, -118, -97, -17, -68, -116, -24, -82, -94, -27, -115, -107, -27, -92, -124, -28, -70, -114, -27, -123, -123, -27, -128, -68, -28, -72, -83, 50, 48, 49, 55, 48, 56, 48, 49, 49, 49, 49 , 50, 48, 53, 56, 56, 51, 51, 48, 48, 54, 55, 54, 52, 55, 85, 78, 68, 69, 82, 87, 65, 89, 50, 48, 49, 55, 45, 48, 56, 45, 48, 49, 32, 49, 49, 58, 49, 50, 58, 48, 54]












  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中使用RSA算法进行公钥加密的步骤如下: 1. 导入所需的类和包: ```java import java.security.Key; import java.security.KeyFactory; import java.security.spec.X509EncodedKeySpec; import javax.crypto.Cipher; import java.util.Base64; ``` 2. 定义RSA算法和密钥相关的常量: ```java private final static String RSA_ALGORITHM = "RSA"; private final static String RSA_PUBLIC_KEY = "RSAPublicKey"; ``` 3. 获取公钥字符串,并将其转换为字节数组: ```java String publicKeyStr = "这里填写你的公钥字符串"; byte[] publicKeyBytes = Base64.getDecoder().decode(publicKeyStr); ``` 4. 根据公钥字节数组生成公钥对象: ```java X509EncodedKeySpec keySpec = new X509EncodedKeySpec(publicKeyBytes); KeyFactory keyFactory = KeyFactory.getInstance(RSA_ALGORITHM); Key publicKey = keyFactory.generatePublic(keySpec); ``` 5. 创建Cipher对象并进行加密操作: ```java Cipher cipher = Cipher.getInstance(RSA_ALGORITHM); cipher.init(Cipher.ENCRYPT_MODE, publicKey); byte[] encryptedBytes = cipher.doFinal(plainText.getBytes()); ``` 6. 将加密后的字节数组转换为Base64编码字符串: ```java String encryptedStr = Base64.getEncoder().encodeToString(encryptedBytes); ``` 完整的示例代码如下: ```java import java.security.Key; import java.security.KeyFactory; import java.security.spec.X509EncodedKeySpec; import javax.crypto.Cipher; import java.util.Base64; public class RSAEncryptionExample { private final static String RSA_ALGORITHM = "RSA"; private final static String RSA_PUBLIC_KEY = "RSAPublicKey"; public static void main(String[] args) throws Exception { // 获取公钥字符串,并将其转换为字节数组 String publicKeyStr = "这里填写你的公钥字符串"; byte[] publicKeyBytes = Base64.getDecoder().decode(publicKeyStr); // 根据公钥字节数组生成公钥对象 X509EncodedKeySpec keySpec = new X509EncodedKeySpec(publicKeyBytes); KeyFactory keyFactory = KeyFactory.getInstance(RSA_ALGORITHM); Key publicKey = keyFactory.generatePublic(keySpec); // 加密操作 String plainText = "要加密的数据"; Cipher cipher = Cipher.getInstance(RSA_ALGORITHM); cipher.init(Cipher.ENCRYPT_MODE, publicKey); byte[] encryptedBytes = cipher.doFinal(plainText.getBytes()); // 将加密后的字节数组转换为Base64编码字符串 String encryptedStr = Base64.getEncoder().encodeToString(encryptedBytes); System.out.println("加密后的数据:" + encryptedStr); } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值