使用AES解密时,前16个字节已损坏
First 16 bytes corrupted while decrypting using AES
我使用openssl
编码了一个文本文件,如下所示:
openssl enc -nosalt -aes-128-cbc -k mypass -in "test.txt" -out "test_enc.txt" -p
并且它返回密钥和iv如下:
key=A029D0DF84EB5549C641E04A9EF389E5
iv =A10CE9C4682486F8622F2F18E7291367
这是我用来解密文件的代码:
int main() {
streampos size;
char * indata;
ifstream file ("test_enc.txt", ios::in|ios::binary|ios::ate);
ofstream outfile ("test_decoded.txt",std::ofstream::binary);
if (file.is_open())
{
size = file.tellg();
indata = new char [size];
file.seekg (0, ios::beg);
file.read (indata, size);
file.close();
unsigned char* outdata=new unsigned char [size];
unsigned char ckey[] = "xA0x29xD0xDFx84xEBx55x49xC6x41xE0x4Ax9ExF3x89xE5";
unsigned char ivec[] = "xA1x0CxE9xC4x68x24x86xF8x62x2Fx2Fx18xE7x29x13x67";
AES_KEY key;
AES_set_decrypt_key(ckey, 256, &key);
AES_cbc_encrypt((unsigned char*) indata, outdata, size, &key, ivec, AES_DECRYPT);
outfile.write ((char*) outdata,size);
delete[] indata;
delete[] outdata;
}
else{
cout << "Unable to open file";
cerr << "Error: " << strerror(errno);
}
outfile.close();
file.close();
return 0;
}
这个代码运行得很好。然而,当我在编码时使用salt时,如以下命令所示:
openssl enc -aes-128-cbc -k mypass -in "test.txt" -out "test_enc.txt" -p
并适当地替换代码中的密钥和ivec,整个文件被正确解密,但前16个字节!我从其他有类似问题的帖子中了解到,我知道iv值是错误的,但我不知道什么应该是正确的iv值。我只使用加密后返回的密钥和iv值,也没有考虑salt值(实际上我不知道如何)。正确的iv值应该是多少?
从您的问题症状来看,在我看来,您的以下逻辑可能正在释放内存,而稍后程序中的代码正在读取内存。这导致了这种情况。通常情况下,这是因为在释放内存后,堆管理器通常会为自己的使用写入一些内务管理信息。
关键是我们不应该在释放内存后使用它,因为它是悬空的。
//Allocate the memory
unsigned char* outdata=new unsigned char [size];
// It appears to me that here the outdata is getting freed. I do not see anywhere
// else this memory is getting freed.
AES_cbc_encrypt((unsigned char*) indata, outdata, size, &key, ivec, AES_DECRYPT);
// Due to this, while accessing it we are seeing the 16 bytes corrupted.
outfile.write ((char*) outdata,size);
您可以使用任何动态工具来识别此类问题。
我也遇到了类似的问题。在我的案例中,问题是传递给AES_cbc_encrypt的IV在算法执行期间被修改。为了保存IV的内容,我以前不得不保存一份副本。
这里描述了一个类似问题的解决方案。
相关文章:
- 从文件中读取多个字节,并将它们存储在C++中进行比较
- 更改高度贴图,使其在 4x4 网格上显示 16 个 hieghtmap
- 为什么 bool 和 _Bool 如果它们在内存中占用 1 个字节,它们只能存储 0 或 1
- 在 Linux 中,uint32_t从 4 个字节更改为 6 个字节
- 运行时错误:引用绑定到类型"int"的未对齐地址0xbebebebebebebec6,这需要 4 个字节对齐 (stl_vector.h)
- 向指针地址添加 20 个字节偏移量
- 为什么在我的实现中,所有数组都对齐到 16 个字节?
- AES-128 CFB-8解密的前16个字节已损坏
- 为什么分配的变量地址之间相差 16 个字节?
- 如何创建 std::vector of char/std::byte,其中第一个字节对齐到 16 个字节,但没有填充
- 将分配的前 16 个字节的内存强制转换为 UDT
- _declSpec(Align(16))不将指针与16个字节对齐
- AES CBC 加密/解密仅解密前 16 个字节
- 如何从字符串中每隔16个字节复制一次数据
- 如何在使用高达 SSSE3 的内部函数后存储 16 字节寄存器中的 4 个字节
- 使用 AES 解密时错误的 16 个字节
- 使用AES解密时,前16个字节已损坏
- 为什么这个源代码分配了16个字节
- 将两个字节转换为16位值的最有效方法
- 为什么 Linux 的 IPv4 地址占用 16 个字节而不是 4 个字节