Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...