Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

exception: wrong final block length...aes decryption

what I'm trying to achieve is an AES CBC decryption like this in android using online tool

I'm not really an android developer, and the following code doesn't have to be 100% secure(just for an example) encrypting works just fine, but I'm really stuck with the decrypting:

the line with: AESCrypt.decrypt(key2, ivBytes, todecode); throws the exception I think the problem is something about the actual dataconversion of the "key2" and "todecode" variables... anyone know what I am missing?

settings:

private static final String TAG = "AESCrypt";

// AESCrypt-ObjC uses CBC and PKCS7Padding
private static final String AES_MODE = "AES/CBC/PKCS7Padding";
private static final String CHARSET = "UTF-8";

// AESCrypt-ObjC uses SHA-256 (and so a 256-bit key)
private static final String HASH_ALGORITHM = "SHA-256";





    byte[] key = hexStringToByteArray("E0E1E2E3E5E6E7E8EAEBECEDEFF0F1F2");
    SecretKeySpec key2 = new SecretKeySpec(key, 0, key.length, "AES");
    byte[] ivBytes = hexStringToByteArray("12CEC438810CFA399A81139AF7D648BC");
    byte[] todecode = Base64.decode("CD46009A232420B2CBF6E4148EE17AA4",
            Base64.NO_WRAP);
    try {

        resultbytes = AESCrypt.decrypt(key2, ivBytes, todecode);
        result = resultbytes.toString();

    } catch (Exception e) {

        // TODO Auto-generated catch block
        feedbackBody.append("catch blok \n");
        e.printStackTrace();

    }
    feedbackBody.append(result + " \n");



public static byte[] decrypt(final SecretKeySpec key, final byte[] iv,
        final byte[] decodedCipherText) throws GeneralSecurityException {
    final Cipher cipher = Cipher.getInstance(AES_MODE);
    IvParameterSpec ivSpec = new IvParameterSpec(iv);
    cipher.init(Cipher.DECRYPT_MODE, key, ivSpec);
    byte[] decryptedBytes = cipher.doFinal(decodedCipherText);

    log("decryptedBytes", decryptedBytes);

    return decryptedBytes;
}

edit: seems like the input data is the fault here:

if i encrypt raw data and decrypt it right after that, it works just fine... but I get the decrypted file from elsewhere

suspect the error around the encoding from the string: Base64.decode("CD46009A232420B2CBF6E4148EE17AA4", Base64.NO_WRAP);

like image 969
Lukas Plazovnik Avatar asked Feb 11 '23 09:02

Lukas Plazovnik


2 Answers

Please try using the following methods for AES encryption and decryption, they are tested over long time:

 public static String encrytData(String text) throws Exception {

    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
    byte[] static_key = Constants.AES_KEY.getBytes();

    SecretKeySpec keySpec = new SecretKeySpec(static_key, "AES");
    IvParameterSpec ivSpec = new IvParameterSpec(Constants.IV_VECTOR);
    cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivSpec);

    byte[] results = cipher.doFinal(text.getBytes());

    String result = Base64.encodeToString(results, Base64.NO_WRAP|Base64.DEFAULT);
    return result;

}


public static String decryptData(String text)throws Exception{

    byte[] encryted_bytes = Base64.decode(text, Base64.DEFAULT);

    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
    byte[] static_key = Constants.AES_KEY.getBytes();

    SecretKeySpec keySpec = new SecretKeySpec(static_key, "AES");
    IvParameterSpec ivSpec = new IvParameterSpec(Constants.IV_VECTOR);
    cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);

    byte[] decrypted = cipher.doFinal(encryted_bytes);
    String result = new String(decrypted);

    return result;
}
like image 165
kamal2305 Avatar answered Feb 15 '23 09:02

kamal2305


ZeroBytePadding at the AES_MODE did the magic!!

private static final String AES_MODE = "AES/CBC/ZeroBytePadding";

also: Base64.decode(...) on line 4 made some problems, changed to hexStringToByteArray(...) and it now works just fine :D

like image 27
Lukas Plazovnik Avatar answered Feb 15 '23 10:02

Lukas Plazovnik