英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论