Decryption error while Decrypt the symmetric key

628 views Asked by At

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

There are 1 answers

0
Mohammadreza Khatami On BEST ANSWER

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.