问题描述
我试图用C ++解密一个文件。该文件使用以下命令加密: openssl enc -nosalt -aes-128-cbc -pass pass:test -in test.txt-outtest_enc.txt-p
控制台显示 key = 098F6BCD4621D373CADE4E832627B4F6
和 iv = 0A9172716AE6428409885B8B829CCB05
。
在C ++中我有包含 #include openssl / aes.h
行,并尝试使用以下代码进行解密:
const char * indata = string.toAscii()。constData();
unsigned char outdata [strlen(indata)];
unsigned char ckey [] =098F6BCD4621D373CADE4E832627B4F6;
unsigned char ivec [] =0A9172716AE6428409885B8B829CCB05;
/ *包含密钥本身的数据结构* /
AES_KEY键;
/ *设置加密密钥* /
AES_set_decrypt_key(ckey,256,& key);
AES_cbc_encrypt((unsigned char *)indata,outdata,strlen(indata),& key,ivec,AES_DECRYPT);
QString result = QString((const char *)outdata);
返回结果;
变量outdata包含与OpenSSL加密之前的值不同。
您在OpenSSL中指定 -aes-128-cbc
作为选项,所以键和初始化向量将 128位长。 openssl
将这些输出为十六进制字符串,因为它们会在控制台上被混淆,如果打印为二进制。
因此,您应该将您的 ckey []
和 ivec []
初始化为十六进制的二进制值这样的字符串:
unsigned char ckey [] =\x09\x8F\x6B\xCD\x46 \x21\xD3\x73\xCA\xDE\x4E\x83\x26\x27\xB4\xF6\" ;
unsigned char ivec [] =\x0A\x91\x72\x71\x6A\xE6\x42\x84\x09\x88\x5B\x8B\x82 \x9C\xCB\x05\" ;
,并使用密钥长度 128 而不是 256 in:
AES_set_decrypt_key(ckey,128,& key);
I'm trying to decrypt a file in C++. This file is encrypted with the following command:
openssl enc -nosalt -aes-128-cbc -pass pass:test -in "test.txt" -out "test_enc.txt" -p
The console shows the key=098F6BCD4621D373CADE4E832627B4F6
and iv=0A9172716AE6428409885B8B829CCB05
.
In C++ I have included the #include openssl/aes.h
line and try to decrypt with the following code:
const char *indata = string.toAscii().constData();
unsigned char outdata[strlen(indata)];
unsigned char ckey[] = "098F6BCD4621D373CADE4E832627B4F6";
unsigned char ivec[] = "0A9172716AE6428409885B8B829CCB05";
/* data structure that contains the key itself */
AES_KEY key;
/* set the encryption key */
AES_set_decrypt_key(ckey, 256, &key);
AES_cbc_encrypt((unsigned char*) indata, outdata, strlen(indata), &key, ivec, AES_DECRYPT);
QString result = QString((const char*) outdata);
return result;
The variable outdata contains different value than before encryption with OpenSSL.
You specify -aes-128-cbc
as an option on OpenSSL so the key and initialization vector will be 128 bits long. openssl
prints these out as hex strings, as they would be obfuscated on the console if printed binary.
Therefor you should initialize your ckey[]
and ivec[]
as the binary value of the hex strings like this:
unsigned char ckey[] = "\x09\x8F\x6B\xCD\x46\x21\xD3\x73\xCA\xDE\x4E\x83\x26\x27\xB4\xF6";
unsigned char ivec[] = "\x0A\x91\x72\x71\x6A\xE6\x42\x84\x09\x88\x5B\x8B\x82\x9C\xCB\x05";
and also, use key length 128 instead of 256 in:
AES_set_decrypt_key(ckey, 128, &key);
这篇关于以C ++解密文件,该文件使用openssl -aes-128-cbc加密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!