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",
    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)

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

  • I think you should set your cipher explicitly to Nopadding in decryption. Cipher cipher = Cipher.getInstance("RSA/NONE/NoPadding", "BC");
    – Limit
    Commented Jun 14, 2018 at 16:54
  • I checked with the ciphertexts and it's not dynamic. so padding is not getting applied even without explicitly adding NoPadding.
    – Prakhash
    Commented Jun 14, 2018 at 17:06
  • I meant in decryption. Your decryption is expecting a padding so what if you explicitly mention no padding there?
    – Limit
    Commented Jun 14, 2018 at 17:07
  • Your stacktrace is for a problem in AES which cannot be from the code you posted. It must be in some other code you didn't show us, and perhaps code that is not what you intended to run. Commented Jun 15, 2018 at 4:34


You must log in to answer this question.

Browse other questions tagged .