I'm using an implementation as below to do an encryption where the bouncycastle is used as the crypto provider. So the ciphertext for a particular plaintext will not be dynamic as the RSA from bouncycastle is not having a padding. It will behave like a textbook cipher.
private static byte [] encrypt(String plaintext) throws Exception {
KeyStore keyStore = getKeyStore();
Certificate[] certs = keyStore.getCertificateChain("alias");
Cipher cipher = Cipher.getInstance("RSA","BC");
cipher.init(Cipher.ENCRYPT_MODE, certs[0].getPublicKey());
return cipher.doFinal(plaintext.getBytes());
}
Then I'm decrypting the ciphertext as below
private static String decrypt(byte [] ciphertext) throws Exception {
KeyStore keyStore = getKeyStore();
PrivateKey privateKey = (PrivateKey) keyStore.getKey("alias",
"password".toCharArray());
Cipher cipher = Cipher.getInstance("RSA","BC");
cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] cipherbyte=cipher.doFinal(ciphertext);
return new String(cipherbyte);
}
During that time I'm getting the below exception
Caused by: javax.crypto.BadPaddingException: Given final block not properly padded
at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:966)
at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:824)
at com.sun.crypto.provider.AESCipher.engineDoFinal(AESCipher.java:436)
at javax.crypto.Cipher.doFinal(Cipher.java:2165)
at
What would be the reason for this? As far as know this can happen only when the padding is applied such as RSA with OAEP or something similar. I have checked many questions asked by many other people and all are demonstrating examples with padding.
Please let me know if there are any other possible places where this exception can occur. Really appreciate your help on this