vendredi 29 mai 2015

Why node_crypto is giving different result than Java Cypher?

I'm trying to understand why encrypted data changes when using Java or Node.js to encrypt it, I need to adapt node.js code to make it return exactly the same encrypted data that I have on Java. (Note that I cannot modify the java snippet)

Node.js Implementation:

var crypto = require('crypto');

console.log("\n\n============");
var cKey = new Buffer("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", "utf-8");
var cIv = new Buffer("1111111111111111", "utf-8");
var cData = "x";
console.log(cKey);
console.log(cIv);
console.log("UTF-8 Data: " + cData);

var cipher = crypto.createCipheriv("aes-256-cbc", cKey, cIv);
var cipherText = cipher.update(cData, 'utf8', 'hex') + cipher.final('hex');

console.log("Our data: " + cipherText);

The previous snippet will print the following result:

<Buffer 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41>
<Buffer 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31>
UTF-8 Data: x
Our data: 0eddfe1857248c7057904455d189cf31

Java Implementation:

AesSymmetricKey key = new AesSymmetricKey("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA".getBytes());
byte[] data = "x".getBytes();
byte[] iv = "1111111111111111".getBytes();
byte[] encrypted = new EncryptionService().encryptAesData(data, iv, key);
Cipher cipher = Cipher.getInstance("AES");
IvParameterSpec ivspec = new IvParameterSpec(initializationVector);
            cipher.init(Cipher.ENCRYPT_MODE, key, ivspec);
byte[] result = cipher.doFinal(data);
_print(result);

That snippet will print:

17b0ccd594229baa6dabd5e850e07fdf

Please note that I compared bytes for data, iv and key and those are exactly the same.

How can I modify node's snippet to make it return the same bytes of java's?

Aucun commentaire:

Enregistrer un commentaire