I am trying to decrypt in NodeJs. It is working in Java. But I am not able to achieve same in Node.
node-version: 8.4
Please find my NodeJs code:
var crypto = require('crypto');
function decryption (message, key) {
var messageArray = Buffer.from(message, 'base64');
// var kekbuf = Buffer(key, 'utf8');
var ivBuffer = new Buffer([0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]);
var iv = ivBuffer.slice(0, 16);
var decipher = crypto.createDecipheriv('aes-256-cbc', key, iv);
decipher.setAutoPadding(false);
var dec = decipher.update(messageArray, 'base64');
dec += decipher.final();
return dec.toString();
}
Please find working Java decryption code
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import javax.xml.bind.DatatypeConverter;
class Test1 {
public String decrypt(String message, String key) throws Exception {
DatatypeConverter dtc = null;
byte[] messagArray = dtc.parseBase64Binary(message);
byte[] keyArray = dtc.parseBase64Binary(key);
byte[] iv = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
IvParameterSpec ivspec = new IvParameterSpec(iv);
SecretKey secretKey = new SecretKeySpec(keyArray, "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
cipher.init(Cipher.DECRYPT_MODE, secretKey, ivspec);
return new String(cipher.doFinal(messagArray));
}
}
I am getting a different decrypted text. I am not able to achieve the same result in NodeJs as I had in Java.
Also, I could not modify my java encryption code. So I have to figure out decryption in Node.
Could you please help me with this.
See Question&Answers more detail:
os