I have an encrypt-code in Java. I'm trying to port the encrypt part to node. Basically, node will do the encryption using the crypto module, and then Java will do the decryption.
Here's how I do encryption in Java:
protected static String encrypt(String plaintext) {
final byte[] KEY = {
0x6d, 0x79, 0x56, 0x65, 0x72, 0x79, 0x54, 0x6f, 0x70,
0x53, 0x65, 0x63, 0x72, 0x65, 0x74, 0x4b
};
try {
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
final SecretKeySpec secretKey = new SecretKeySpec(KEY, "AES");
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
final String encryptedString = Base64.encodeToString(
cipher.doFinal(plaintext.getBytes()), Base64.DEFAULT);
return encryptedString;
} catch (Exception e) {
return null;
}
}
Here's how I do encryption in node:
var crypto = require('crypto'),
key = new Buffer('6d7956657279546f705365637265744b', 'hex'),
cipher = crypto.createCipher('aes-128-ecb', key),
chunks = [];
cipher.setAutoPadding(true);
chunks.push(cipher.update(
new Buffer(JSON.stringify({someKey: "someValue"}), 'utf8'),
null, 'base64'));
chunks.push(cipher.final('base64'));
var encryptedString = chunks.join('');
In Java, I get the string T4RlJo5ENV8h1uvmOHzz1KjyXzBoBuqVLSTHsPppljA=. This gets decrypted correctly. However, in node, I get al084hEpTK7gOYGQRSGxF+WWKvNYhT4SC7MukrzHieM= which is obviously different and thus it won't get decrypted correctly.
I tried to look for people who has the same problem as me, and this github issue is the closest I can find. As suggested in that issue, I tried running openssl like so:
$ echo -e '{"someKey": "someValue"}' | openssl enc -a -e -aes-128-ecb -K "6d7956657279546f705365637265744b"
T4RlJo5ENV8h1uvmOHzz1MY2bhoFRHZ+ClxsV24l2BU=
The result I got was close enough to the one produced by java, but still different:
T4RlJo5ENV8h1uvmOHzz1MY2bhoFRHZ+ClxsV24l2BU= // openssl
T4RlJo5ENV8h1uvmOHzz1KjyXzBoBuqVLSTHsPppljA= // java
al084hEpTK7gOYGQRSGxF+WWKvNYhT4SC7MukrzHieM= // node
Which brings me to the question, how do I make node output the same encrypted string as my java code? I can only change my code in node, but not in java.
JSON.stringifyoutput is identical to whatever you're passing to Java asplaintext)? - chrylis -cautiouslyoptimistic-cipher.setAutoPadding(true). EDIT: Found the link. See the accepted answer here - avendaelJSON.stringify's output (manually) to javaencryptwith some escape characters:encrypt("{\"someKey\":\"someValue\"}")- avendaelString#getBytesexplicitly depends on the platform character set. Dump the byte arrays. - chrylis -cautiouslyoptimistic-7b22736f6d654b6579223a22736f6d6556616c7565227dwhich is the same as how it looks like in node. Probably, the problem lies somewhere else. - avendael