AES ECB encrypt/decrypt only decrypts the first 16 bytes

后端 未结 2 1629
囚心锁ツ
囚心锁ツ 2020-12-20 22:42

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* k         


        
2条回答
  •  不思量自难忘°
    2020-12-20 22:58

    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.

    In case of any doubt, please let me know.

提交回复
热议问题