C/C ++中用于AES加密的PKCS#5填充 [英] PKCS#5 padding for AES encryption in C/C++
本文介绍了C/C ++中用于AES加密的PKCS#5填充的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在JAVA
中进行了AES/ECB/PKCS5Padding
加密.现在,我正在寻找C/C++
中的等效方法.我确实使用以下代码在C中进行了加密:
I did AES/ECB/PKCS5Padding
encryption in JAVA
. Now I am looking for equivalent method in C/C++
. I did manage to encrypt in C using the following code :
const unsigned char *key = (unsigned char *)"A2XTXFEQFOPZFAKXFOPZFAK5";
unsigned char input[] = {"Hello World"};
int iLen = strlen((char*)input);
int len = 0;
if(iLen % 16 == 0){
len = iLen;
}
else{
len = (iLen/16)*16 + 16;
}
unsigned char output[len]; //next 16bit
AES_KEY enc_key;
AES_set_encrypt_key(key, strlen((char*)key)*8, &enc_key);
int position = 0;
while(position < iLen){
AES_ecb_encrypt(input+position,output+position,&enc_key,AES_ENCRYPT);
position += 16;
}
推荐答案
使用填充时,即使输入是块大小的倍数,也必须始终添加 .
When using padding it must always be added, even if the input is a multiple of the block size.
不正确,这是一个常见错误:
Incorrect, this is a common mistake:
if(iLen % 16 == 0){
len = iLen;
}
正确:
int blockSize = 16;
int len = iLen + blockSize - (iLen % blockSize);
请参见 PKCS#7填充.
这篇关于C/C ++中用于AES加密的PKCS#5填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文