Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AES ECB encrypt/decrypt only decrypts the first 16 bytes

Tags:

c

openssl

aes

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");

Any idea

like image 717
user956584 Avatar asked Mar 13 '13 02:03

user956584


1 Answers

It appears that you are confusing key length and block size.

AES can be used with 3 different key lengths: 128-bits, 192-bits & 256-bits.

AES always uses a block size of 128 bits (16 bytes). For messages that are more than 16 bytes long, you need to decrypt (or encrypt) 16 bytes at a time and expect to get 16 bytes of output each time. (You will also need to decide which mode to use - e.g. CBC, CTR, ECB, etc.. If you're decrypting text provided by somebody else then that decision has already been taken for you. If making the decision for yourself, bear in mind that ECB is almost never the right choice.) If the message isn't a multiple of 16 bytes long, you'll need to pad it so that it is. PKCS #7 padding is the most common.

See the Wikipedia article on AES for more information.

like image 100
Andrew Rose Avatar answered Oct 04 '22 23:10

Andrew Rose