AES - AES ECB encrypt/decrypt only decrypts the first 16 bytes

Question:

I had function that decode AES 256 string but it return only 16 char

bool decrypt_block(unsigned char* cipherText, unsigned char* plainText, unsigned char* key)
{
    AES_KEY decKey;
    if (AES_set_decrypt_key(key, 256, &decKey) < 0)
        return false;
    AES_decrypt(cipherText, plainText, &decKey);
    return true;
}

decrypt_block( encoded, resultText, ( unsigned char *) "57f4dad48e7a4f7cd171c654226feb5a");

Answer:

AES is a block cipher. It encrypts and decrypts a block of 128 bits (16 bytes).AES_decrypt and AES_encrypt acts on a single block at a time. So, you will get only first 16 bytes. You have to manually decrypt or encrypt other blocks.

If you know the mode (like CBC, ECB etc.), you can call functions such AES_decrypt_cbc etc.

You need to modify the code as follows (I am giving just an example):

 int len = strlen(ciphertext); //or get cipher text length by any mean.
 int i;
 for(i=0; i<=len; i+=16)
    AES_decrypt(cipherText+i, plainText+i, &decKey);
If you are sure about mode, call cbc/ecb/cfb/ofb mode functions.


你可能感兴趣的:(AES - AES ECB encrypt/decrypt only decrypts the first 16 bytes)