4

I try to decrypt an encrypted data that I receive from a web service.

The encryption is done using AES 128.

I use the following code to decrypt the data:

public static String decrypt(String strToDecrypt)
{       
    try
    {
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding"); //AES/CBC/PKCS7Padding
        SecretKeySpec secretKey = new SecretKeySpec(AppConstants.AESEncryptionKey.getBytes("UTF8"), "AES");
        int blockSize = cipher.getBlockSize();
        cipher.init(Cipher.DECRYPT_MODE, secretKey, new IvParameterSpec(new byte[blockSize])); //new IvParameterSpec(new byte[16])
        byte decBytes[] = cipher.doFinal(Base64.decode(strToDecrypt, 0));
        // byte decBytes[] = cipher.doFinal(Base64.decodeBase64(strToDecrypt));
        String decStr = new String(decBytes);
        System.out.println("After decryption :" + decStr);
        return decStr;
    }
    catch (Exception e)
    {
        System.out.println("Exception in decryption : " + e.getMessage());
    }
    return null;
}

At

cipher.doFinal()

I got the following Exception:

javax.crypto.badpaddingexception pad block corrupted

I went through my post but ended up with no solution. I am badly stuck over here.

Regent
  • 5,142
  • 3
  • 21
  • 35
iAviatorJose
  • 418
  • 2
  • 10
  • 25
  • Either the encrypting side uses some other padding (or mode, or cipher) or the `strToDecrypt` contains truncated or otherwise corrupted data (the string itself can be proper Base64, but the data it represents is corrupted). – Oleg Estekhin May 06 '14 at 10:32
  • strToDecrypt has the perfect encrypted data encoded using Base64 and even the padding,mode and Algorithm is same – iAviatorJose May 06 '14 at 12:40
  • In that case the last option is that the keys used for encoding and decoding are different: decrypting with the wrong key will result in the last block which does not contain the properly padded data and this condition is detectable and causes BadPaddingException. – Oleg Estekhin May 06 '14 at 13:35
  • Shouldn't this be `.getBytes("UTF-8")`? – petey Oct 28 '16 at 21:47

3 Answers3

11
SecureRandom sr = SecureRandom.getInstance("SHA1PRNG","Crypto");

works perfectly

Note: This code works only on devices up to Android 6. Starting with Android 7.0 the "Crypto" provider has been removed, therefore this code will fail.

Robert
  • 39,162
  • 17
  • 99
  • 152
Pavan Pyati
  • 950
  • 2
  • 13
  • 18
4

AES keys should consist of random data. If you store them as a String then you are likely to loose information, especially if you use encodings such as UTF-8. Your line:

AppConstants.AESEncryptionKey.getBytes("UTF8")

Makes it likely that you've lost data during conversion to/from a string. Use hexadecimals instead if you require a string, or simply store the key as a byte array.


Note that this answer doesn't indicate any security related hints. In general you only want to derive keys or store them in containers. You don't want to use CBC over an insecure channel either.

Maarten Bodewes
  • 90,524
  • 13
  • 150
  • 263
2

In my case issue is came because encrypted key and decrypted key both are different, when I check both key with same value then issue is not came

Yogesh Rathi
  • 6,331
  • 4
  • 51
  • 81