【发布时间】:2015-04-07 03:01:45
【问题描述】:
所以我在 Openssl 中使用 C++ 中的以下代码。 我从另一个 SO 线程得到这个。
int bytes_read, bytes_written;
unsigned char indata[AES_BLOCK_SIZE];
unsigned char outdata[AES_BLOCK_SIZE];
/* ckey and ivec are the two 128-bits keys necesary to
en- and recrypt your data. Note that ckey can be
192 or 256 bits as well */
unsigned char ckey[] = "thiskeyisverybad";
unsigned char ivec[] = "dontusethisinput";
/* data structure that contains the key itself */
AES_KEY key;
/* set the encryption key */
AES_set_encrypt_key(ckey, 128, &key);
/* set where on the 128 bit encrypted block to begin encryption*/
int num = 0;
FILE *ifp = fopen("out.txt", "r");
FILE *ofp = fopen("orig.txt", "w");
while (true) {
bytes_read = fread(indata, 1, AES_BLOCK_SIZE, ifp);
AES_cfb128_encrypt(indata, outdata, bytes_read, &key, ivec, &num,
AES_DECRYPT); //or AES_DECRYPT
bytes_written = fwrite(outdata, 1, bytes_read, ofp);
if (bytes_read < AES_BLOCK_SIZE) {
std::cout << bytes_read << std::endl;
break;
}
}
fclose(ifp);
fclose(ofp);
我正在做的是加密文件“test.txt”,首先将AES_ENCRYPT 传递给AES_set_encrypt_key,然后尝试解密同一个文件。加密文件存储为 out.txt。
我使用上面的代码解密。我的问题是解密后的文件似乎只能解密 454 字节的数据。它正确地解密了数据,但不是全部。我尝试了一个
将“ivec”设为空字符串可以让我改为解密 545 字节的加密文本。
我做错了什么?
【问题讨论】: