0

I am trying to decrypt an encrypted data obtained from a web service using AES128 cryptography.

following is the code i am using to achieve the same.

But i always end up with the following exception: javax.crypto.IllegalBlockSizeException: last block incomplete in decryption

public static String decrypt(String strToDecrypt)
    {

        try
        {
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
            final SecretKeySpec secretKey = new SecretKeySpec(AppConstants.AESEncryptionKey.getBytes("UTF8"), "AES");
            cipher.init(Cipher.DECRYPT_MODE, secretKey,new IvParameterSpec(new byte[16])); //new IvParameterSpec(new byte[16])
            byte base64Data[] = Base64.encode(strToDecrypt.getBytes(), Base64.DEFAULT);
            @SuppressWarnings("unused")
            String s = base64Data.toString();
            byte decBytes[] = cipher.doFinal(base64Data);
            String decStr = new String(decBytes);
            return decStr;
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return null;
    }

Please Pour in your valuable inputs as i am badly stuck over here.

iAviatorJose
  • 418
  • 2
  • 10
  • 25

3 Answers3

2

You have to call Base64.decodeBase64(s). Then call Cipher.doFinal()

Cipher.doFinal(Base64.decodeBase64(s));

UVM
  • 9,776
  • 6
  • 41
  • 66
1

You are encoding the base 64 encoded ciphertext instead of decoding it. Depending on your Base64 you need to call a function that decodes from a String or CharSequence to an array of bytes, and then decrypt that. Please test if the result is a multiple of the block size, 16 bytes for AES.

Maarten Bodewes
  • 90,524
  • 13
  • 150
  • 263
  • i have one more question nice u mentioned of the block size , i want use AES 128 algorithm for decryption do u know how to do that. – iAviatorJose May 05 '14 at 04:16
  • ok sir on using decode function i am getting the following exception now javax.crypto.BadPaddingException: pad block corrupted – iAviatorJose May 05 '14 at 07:12
  • 1
    OK, so the ciphertext has the correct block size now. The above exception is when you use the wrong key value, if the ciphertext is corrupted or if the IV is incorrect for very short ciphertext lengths. – Maarten Bodewes May 05 '14 at 07:32
  • I have set IV like this cipher.init(Cipher.DECRYPT_MODE, secretKey,new IvParameterSpec(new byte[16])); is there anything wrong with this way setting an iv – iAviatorJose May 05 '14 at 07:46
  • No, functionally it matches the encryption. The problem is likely elsewhere, match the key bytes first (print out on both sides using hex right before encryption/decryption). Setting a static IV is however not cryptographically secure, you should use a random value and prefix to ciphertext after fixing the issues. – Maarten Bodewes May 05 '14 at 08:33
  • @iAviatorJose you better create real array filled with random values with `new byte[] {12, 123, 123 ...}`. In my case it did failed because I was using `new byte[16];` – Roger Alien Jan 12 '17 at 23:26
0

Important note

In my case this issue came because of encryption not done properly, when i try to encrypt data that time my code through error that's why in middle encryption is terminated, so once you check your encryption is working properly.

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