Reputation: 193
I need to encrypt a string in Java and decrypt it in C++. I've seen C++ has a Crypto++ library and Java has JCE.
For c++, I refer to this page
The result is defferent.
In java abcd1234 7e77643ca7d46d46298be3239f1a5cdb abcd1234
In c++ Strange characters...
What i have to do?
Upvotes: 0
Views: 382
Reputation: 42585
One of your problems is that your "key" is derived from a String which gives you different results for Java and C:
Java: "abcdefgh12345678".getBytes() gets you the UTF-8 (Linux) or ISO-8859-1 (Windows) representation. In any way the characters are interpreted as 8bit characters and you are getting a 16 byte long array.
C: You are using a WCHAR which uses AFAIK a 16bit unicode characters. Therefore your key is in the end 32 byte long.
Conclusion: Different keys means different results...
Important: Please never ever use ASCII characters as a cryptographic key! If you want to encrypt something using a password use a password derivation function like PBKDF2 for generating a cryptographic from the password.
For testing purposes you can statically define a byte/char array in your code:
char myArray[] = { 0x00, 0x11, 0x22 }; // c
byte[] myArray = new byte[]{ 0x00, 0x11, 0x22 }; // Java
Upvotes: 2
Reputation: 673
apparently the java version is displayed in hex to string format,try this
for (int i = 0 ; i < 16 ; ++i)
std::cout << std::hex << static_cast<int>(buf[i]);
std::cout << std::endl;
edit : return byteArrayToHex(encrypted); in your java code does the same on the bytearray
Upvotes: 1