全,
我试图用AES 256加密Java中的字符串,并使用openssl在C++中解密它。在java i中:
现在,我正在尝试使用C++使用OpenSSL解密它:
string encoded = string("LtANvfmnb5zj+4+g6I7hC53eHMIRa4BOkzMpXYLlA9DRnRWjQjO9uMot6hR7zzTIOtdmkRJ16aVZRfIT3sYn17jYEJjvAN9/N7FbblLplCtOuHatGffH0pSf8lu76SUzDIZU+EXgTnK1SsEa4sndcXvg5jaElxr4GCHq+F2aL7t+LVjbqWg4kpYkYbKdrKQgOsMCbBBG2aMFTmQ/cxnVyH8juC/ZTSrPMyjZ7KxS0P9PzfmxkeSi3VsBIjXL6Q4pneZeemP+1JdG02yQWhruJUuH5aRE0piQ776lxt6g0wU=");
string encodedKey = string("1rE2AM4Xf0ItxN/s1oDvaNmXhXlVF3hE+vSkyMPzDl4=");
string decodedEnc = base64_decode(encoded);
string decodedKey = base64_decode(encodedKey);
const unsigned char *keyBytes = reinterpret_cast<const unsigned char*>(decodedKey.c_str());
const unsigned char *in = reinterpret_cast<const unsigned char*>(decodedEnc.c_str());
cout << "initializing" << endl;
AES_KEY key;
/* set the encryption key */
AES_set_encrypt_key(keyBytes, 256, &key);
unsigned char *out = (unsigned char*) malloc(1024);
cout << "Decrypting" << endl;
AES_ecb_encrypt(in,out,&key,AES_DECRYPT);
cout << "decrypted " << out << endl;
char* dec = reinterpret_cast< char*>(out);
string decrypted = std::string(dec);
cout << "Decrypted String : '" << decrypted << "'" << endl;我得到的只是印在终端上的垃圾。我觉得我很亲近,所以任何帮助都会受到极大的感谢。
谢谢马克
发布于 2011-05-12 12:40:31
根据我的理解,尝试解密密钥,然后使用AES_decrypt,如下所示:
cout << "Decrypting" << endl;从这条线开始..。请按以下方式使用代码:
AES_KEY k
AES_set_decrypt_key(keyBytes, 256, &k);
unsigned char* outdecrypt = new unsigned char[1024];
AES_decrypt(out, outdecrypt, &k);发布于 2011-04-23 22:02:49
除了检查密钥长度之外,还要确保Java和C/OpenSSL都使用相同的初始化向量(IV)。有些框架会为您初始化它,而另一些框架则不会。这是初始块将使用的数据向量(IIRC,至少在一种编码模式下),其中AES针对前一个块对每个块进行XOR处理。没有这个(IIRC,它是CBC /循环块编码),最后一个块可以被检查,因为它通常有填充,这是很容易用蛮力方式验证的。
发布于 2011-04-23 19:11:10
我相信其中一个或OpenSSL支持128个位键,而不是256个字节键。
<参考错误报告>
https://stackoverflow.com/questions/5766252
复制相似问题