用 Java (AES) 加密,用 C++ 解密(OpenSSL)
所有,
我正在尝试使用 AES 256 对 Java 中的字符串进行加密,并使用 openssl 在 C++ 中对其进行解密。在java中,我:
- 在JCEKS中生成了SecretKey
- 字符串
- 加密了从SecretKey以base64编码的字符串和getEncoded()的
现在我尝试使用OpenSSL在C ++中解密它:
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;
我得到的只是打印到终端的垃圾。我觉得我很接近,所以任何帮助将不胜感激。
谢谢 马克
All,
I'm trying to encrypt a string in Java using AES 256 and decrypt it in C++ using openssl. In java I:
- Generated a SecretKey in a JCEKS
- Encrypted the string
- encoded both the string and getEncoded() from the SecretKey in base64
Now I'm trying to decrypt it in C++ using 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;
All I am getting is garbage printed to the terminal. I feel like I'm close, so any help would be greatly appreciated.
Thanks
Marc
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
根据我的理解,尝试解密密钥,然后尝试
AES_decrypt
,应按如下方式调用:从这一行开始...使用代码如下:
according to my understanding, try the decryption key and then
AES_decrypt
which should be called as follows:from this line onwards... use the code as follows:
除了检查密钥长度之外,还要确保 Java 和 C/OpenSSL 使用相同的初始化向量 (IV)。有些框架会为您初始化它,有些则不会。这是初始块将与其进行异或的数据向量(IIRC,至少在一种编码模式下),其中 AES 将每个块与前一个块进行异或。如果没有这个(IIRC,它是 CBC/循环块编码),则可以检查最后一个块,因为它通常具有易于以强力方式验证的填充。
In addition to checking key length, make sure both Java and C/OpenSSL are using the same Initialization Vector (IV). Some frameworks initialize it for you, others do not. This is the vector of data that the initial block will be XOR'd with (IIRC, at least in one encoding mode), where AES XORs each block against the previous block. Without this (IIRC it's CBC / cyclic block coding), the last block can be inspected as it typically has padding that is easy to verify in a brute force manner.
我相信 Java 框架或 OpenSSL 之一支持 128 位密钥,而不是 256 字节密钥。
参考的错误报告 >
I believe one of the Java framework or OpenSSL supports 128 bit keys, and not 256 byte keys.
<Referenced bug report>