解密时出现解密错误,解密对称密钥。

huangapple go评论144阅读模式
英文:

Decryption error while Decrypt the symmetric key

问题

当调用cipher.doFinal()时,会引发BadPaddingException异常。

以下是代码部分的翻译:

String rsaKeyAlgorithm = "RSA";
String rsaEncryptAlgorithm = "RSA/ECB/PKCS1Padding";

PrivateKey privateKey = (RSAPrivateKey) KeyFactory.getInstance(rsaKeyAlgorithm).generatePrivate(new PKCS8EncodedKeySpec(Base64.getDecoder().decode(privKey.getBytes(StandardCharsets.UTF_8))));

byte[] bytes = Base64.getDecoder().decode(dataKey.getBytes(StandardCharsets.UTF_8));

Cipher cipher = Cipher.getInstance(rsaEncryptAlgorithm);
cipher.init(Cipher.DECRYPT_MODE, privateKey);
System.out.println(new String(cipher.doFinal(bytes)));

有关特定提供程序(例如SUN)可能引发BadPaddingException的问题,因为不同的Java版本可能没有特定提供程序,并且从KeyFactory.getInstance方法中移除了提供程序,我目前无法提供额外的翻译。

英文:

I'm trying to use RSA algorithm and PKCS1Padding for encryption the given password but when cipher.doFinal() is called the BadPaddingException is thrown.

Here is the code :

String rsaKeyAlgorithm = "RSA";
String rsaEncryptAlgorithm = "RSA/ECB/PKCS1Padding";

PrivateKey privateKey = (RSAPrivateKey) KeyFactory.getInstance(rsaKeyAlgorithm).generatePrivate(new PKCS8EncodedKeySpec(Base64.getDecoder().decode(privKey.getBytes(StandardCharsets.UTF_8))));

byte[] bytes = Base64.getDecoder().decode(dataKey.getBytes(StandardCharsets.UTF_8));

Cipher cipher = Cipher.getInstance(rsaEncryptAlgorithm);
cipher.init(Cipher.DECRYPT_MODE, privateKey);
System.out.println(new String(cipher.doFinal(bytes)));

I've also reading some articles about encryption by specific provider for example SUN may cause BadPaddingException because different Java versions may have not the specific provider and removed the provider from the KeyFactory.getInstance method. But now have issue about how default (non-specific) provider on this method is handling the different Java versions on different environments ?

答案1

得分: 0

问题在于生成和复制密钥(hibernate-keystore.key)文件时,这些密钥每次都会创建,因此输入以前的密码会引发“badPaddingException”,因为它们不再存在于密钥库文件中。

英文:

After spending about two days finally my issue get solved! the problem was in generating and copying the keys (hibernate-keystore.key) file also those keys create each time so entering previous passwords get badPaddingException because they aren't in keystore file anymore.

huangapple
  • 本文由 发表于 2023年6月19日 19:25:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76506158.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定