Skip to content
Advertisement

Cipher functions : WRONG FINAL BLOCK LENGTH Android Studio

There are a lot of threads on stack overflow with this topic, and always the same solutions, but these doesn’t work for me. I am looking for a way to decrypt the value byte[] encrypted and return byte[] decodedBytes.

With the method AESCrypt. I use compile 'com.scottyab:aescrypt:0.0.1'

private void testAES() {
    try {

       final byte[] encrypted = Base64.decode("R3JhbmRlIFZpY3RvaXJlICE=", Base64.NO_WRAP);

       byte[] keyBytes = Base64.decode("L/91ZYrliXvmhYt9FKEkkDDni+PzcnOuV9cikm188+4=", Base64.NO_WRAP);
       final byte[] ivBytes = Base64.decode("gqjFHI+YQiP7XYEfcIEJHw==".getBytes(), Base64.NO_WRAP);

       final SecretKeySpec keySpec = new SecretKeySpec(keyBytes, "AES");

       byte[] decodedBytes = AESCrypt.decrypt(keySpec, ivBytes, encrypted);

   } catch (Exception e) {
       e.printStackTrace();
   }
 }

With the value Cipher, i use it like that.

private static byte[] testCipher() {

    try {

        final byte[] encrypted = Base64.decode("R3JhbmRlIFZpY3RvaXJlICE=", Base64.NO_WRAP);

        byte[] keyBytes = Base64.decode("L/91ZYrliXvmhYt9FKEkkDDni+PzcnOuV9cikm188+4=", Base64.NO_WRAP);
        byte[] ivBytes = Base64.decode("gqjFHI+YQiP7XYEfcIEJHw==".getBytes(), Base64.NO_WRAP);

        final IvParameterSpec ivSpecForData = new IvParameterSpec(ivBytes);

        SecretKeySpec decodedKeySpec = new SecretKeySpec(keyBytes, "AES");
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, decodedKeySpec, ivSpecForData);

        byte[] decodedBytes = cipher.doFinal(encrypted); // I have the error here //

        return decodedBytes;

    } catch (Exception e) {
        e.printStackTrace();
    }

    return null;
}

Whatever i do, i allways have the same error :

error:1e06b07b:Cipher functions:EVP_DecryptFinal_ex:WRONG_FINAL_BLOCK_LENGTH

I try to put in Cipher.getInstance (AES/CBC/NoPadding, AES/CBC/PKCS5Padding, AES/CBC/PKCS7Padding) but nothing change. Do you have any idea to help me ?

Advertisement

Answer

"R3JhbmRlIFZpY3RvaXJlICE=" decoded to 17 hex bytes 4772616E646520566963746F6972652021 which is the ASCII text: “Grande Victoire !”.

17 bytes is not a valid size for AES which is a block cipher that requires encrypted data to be a multiple of the block size of 16-bytes.

There is no encryption just Base64 encoding.

Advertisement