用 Java (AES) 加密,用 C++ 解密(OpenSSL)

发布于 2024-11-02 18:24:38 字数 1502 浏览 0 评论 0原文

所有,

我正在尝试使用 AES 256 对 Java 中的字符串进行加密,并使用 openssl 在 C++ 中对其进行解密。在java中,我:

  1. 在JCEKS中生成了SecretKey
  2. 字符串
  3. 加密了从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:

  1. Generated a SecretKey in a JCEKS
  2. Encrypted the string
  3. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

请别遗忘我 2024-11-09 18:24:38

根据我的理解,尝试解密密钥,然后尝试 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);

according to my understanding, try the decryption key and then AES_decrypt which should be called as follows:

cout << "Decrypting" << endl;

from this line onwards... use the code as follows:

AES_KEY k
AES_set_decrypt_key(keyBytes, 256, &k);

unsigned char* outdecrypt = new unsigned char[1024];
AES_decrypt(out, outdecrypt, &k);
我很OK 2024-11-09 18:24:38

除了检查密钥长度之外,还要确保 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.

梦过后 2024-11-09 18:24:38

我相信 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>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文