所有,加密在Java中(AES),解密C++(OpenSSL的)
我試圖加密使用AES 256在Java中的字符串,並使用OpenSSL的解密在C++中。在java中我:
- 生成SecretKey的一個JCEKS
- 加密字符串
- 編碼都用base64字符串,並從SecretKey的的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;
我得到的所有內容都是垃圾郵件打印到終端。我覺得我很近,所以任何幫助將不勝感激。
感謝 馬克
除這是歐洲央行,所以沒有IV(我知道它並不安全,但我只是試圖找到一些工作) – mlbiam 2011-04-24 13:50:17