用于C / C ++中AES加密的PKCS#5填充

时间:2016-05-10 17:46:43

标签: java c++ encryption openssl aes

我在AES/ECB/PKCS5Padding中进行了JAVA加密。现在我在寻找C/C++中的等效方法。我确实使用以下代码在C中加密:

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;
}

1 个答案:

答案 0 :(得分:2)

使用填充时,即使输入是块大小的倍数,也必须添加始终

不正确,这是一个常见的错误:

/s/sign_up

正确:

if(iLen % 16 == 0){
    len = iLen;
}

请参阅PKCS#7 padding