Skip to content

@kennydude /

Embed URL


Subversion checkout URL

You can clone with
Download ZIP
Encrypt from Java and decrypt on Node.js
// Encrypt where jo is input, and query is output and ENCRPYTION_KEy is key
byte[] input = jo.toString().getBytes("utf-8");
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] thedigest = md.digest(ENCRYPTION_KEY.getBytes("UTF-8"));
SecretKeySpec skc = new SecretKeySpec(thedigest, "AES/ECB/PKCS5Padding");
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, skc);
byte[] cipherText = new byte[cipher.getOutputSize(input.length)];
int ctLength = cipher.update(input, 0, input.length, cipherText, 0);
ctLength += cipher.doFinal(cipherText, ctLength);
String query = Base64.encodeToString(cipherText, Base64.DEFAULT);
var decipher = crypto.createDecipher('aes-128-ecb', encryption_key);
chunks = []
chunks.push( decipher.update( new Buffer(fullBuffer, "base64").toString("binary")) );
chunks.push('binary') );
var txt = chunks.join("");
txt = new Buffer(txt, "binary").toString("utf-8");
// where encryption_key = key, fullBuffer is the input and txt is output

Very useful, thank you!


After some minor tweaking this worked perfectly for me!
Thank you very much!


Getting a ... on the java side

Exception in thread "main" Wrong algorithm: AES or Rijndael required
at com.sun.crypto.provider.SunJCE_c.a(DashoA13..)
at com.sun.crypto.provider.SunJCE_g.a(DashoA13
at com.sun.crypto.provider.SunJCE_f.a(DashoA13..)
at com.sun.crypto.provider.SunJCE_f.a(DashoA13
at com.sun.crypto.provider.AESCipher.engineInit(DashoA13..)
at javax.crypto.Cipher.a(DashoA13
at javax.crypto.Cipher.a(DashoA13..)
at javax.crypto.Cipher.init(DashoA13
at javax.crypto.Cipher.init(DashoA13*..)
at ci.ThirdCipher.main(


Thanks for posting. You just saved me a lot of time. The only change I made was:
SecretKeySpec skc = new SecretKeySpec(thedigest, "AES/ECB/PKCS5Padding");
SecretKeySpec skc = new SecretKeySpec(thedigest, "AES");


I used it in my Android code....Thanks dude


Super helpful. Thanks a ton for posting this


I used "sha-1" for message digest. how should the node equivalent code be changed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.