在下面的代码中,解密后的文本与原始明文不匹配。前 12 个字节搞砸了。请注意,分组密码填充已被禁用。我尝试了 BUF_SIZE 的不同值,所有 16 的倍数 - 每次解密数据的前 12 个字节都是错误的。这是输出:
plain buf[32]:
11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11
11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11
outlen=32
outlen=32
dec buf[32]:
0C 08 01 46 6D 3D FC E9 98 0A 2D E1 AF A3 95 3A
0B 31 1B 9D 11 11 11 11 11 11 11 11 11 11 11 11
这是代码:
#include <stdio.h>
#include <string.h>
#include <CommonCrypto/CommonCryptor.h>
static void
dumpbuf(const char* label, const unsigned char* pkt, unsigned int len)
{
const int bytesPerLine = 16;
if (label) {
printf("%s[%d]:\n", label, len);
}
for (int i = 0; i < int(len); i++) {
if (i && ((i % bytesPerLine) == 0)) {
printf("\n");
}
unsigned int c = (unsigned int)pkt[i] & 0xFFu;
printf("%02X ", c);
}
printf("\n");
}
int main(int argc, char* argv[])
{
unsigned char key[16];
unsigned char iv[16];
memset(key, 0x22, sizeof(key));
memset(iv, 0x33, sizeof(iv));
#define BUF_SIZE 32
unsigned char plainBuf[BUF_SIZE];
unsigned char encBuf[BUF_SIZE];
memset(plainBuf, 0x11, sizeof(plainBuf));
dumpbuf("plain buf", plainBuf, sizeof(plainBuf));
int outlen;
CCCryptorStatus status;
status = CCCrypt(kCCEncrypt, kCCAlgorithmAES128, 0,
key, kCCKeySizeAES128, iv, plainBuf, sizeof(plainBuf),
encBuf, sizeof(encBuf), (size_t*)&outlen);
if (kCCSuccess != status) {
fprintf(stderr, "FEcipher: CCCrypt failure\n");
return -1;
}
printf("outlen=%d\n", outlen);
status = CCCrypt(kCCDecrypt, kCCAlgorithmAES128, 0,
key, kCCKeySizeAES128, iv, encBuf, sizeof(encBuf),
plainBuf, sizeof(plainBuf), (size_t*)&outlen);
if (kCCSuccess != status) {
fprintf(stderr, "FEcipher: CCCrypt failure\n");
return -1;
}
printf("outlen=%d\n", outlen);
dumpbuf("dec buf", plainBuf, sizeof(plainBuf));
return 0;
}
谢谢,哈里