Migration Cypher encryption from Java to NodeJS
Clash Royale CLAN TAG#URR8PPP
Migration Cypher encryption from Java to NodeJS
I am migrating a Java app to a NodeJs one and I'm facing trouble with encryption. I used javax.crypto.Cipher to encrypt and decrypt files.
Now I need to migrate these methods to NodeJS, be able to decrypt files encrypted with the Java code and encrypt new one with NodeJS with the same algorithm.
I can't find out how to do this. I tried everything I found on SO but nothing worked properly.
Here is my Java code :
private static byte encrypt(byte plainData)
byte encryptedData = new byte[0];
try
byte cle = (new String(PASSWORD)).getBytes();
SecretKey key = new SecretKeySpec(cle, ALGO);
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, key);
encryptedData = cipher.doFinal(plainData);
catch (Exception ex)
//Log Error
return encryptedData;
private static void decrypt(InputStream in, OutputStream out)
try
// Cipher INIT
byte cle = (new String(PASSWORD)).getBytes();
SecretKey key = new SecretKeySpec(cle, ALGO);
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, key);
// Decrypt
CipherInputStream cis = new CipherInputStream(in, cipher);
byte block = new byte[8];
int i;
while ((i = cis.read(block)) != -1)
out.write(block, 0, i);
out.close();
catch (IOException ex)
//Log Error
Here is my trial to decrypt in Node. In seems that the decipher.update and decipher.final works as it doesn't throw any exception. But I'm not sure. When i send back the result Buffer to my client, the file (a jpg image in my case) is unreadable.
Note that sending files to the client works perfectly for not encrypted data. So I really think that the problem is in this method.
public static decrypt(encryptedData: Buffer): any
var cypher = encryptedData.toString('base64');
var decipher = crypto.createDecipher('aes-128-ecb', process.env.CYPHER_PASSWORD);
decipher.setAutoPadding(false);
var s = decipher.update(cypher, 'base64', 'base64');
s += decipher.final('base64');
return new Buffer(s, 'base64');
Sorry, I did not post it as I don't believe in it. I added the code i tried to the post.
– Benjamin D.
Aug 9 at 12:45
1 Answer
1
I finally found the solution by playing randomly with the different variables I identified : Key encoding, algorithm, padding and createCipher vs createCipheriv.
I admit that I don't understand why this configuration works, but if it can help someone. If someone can explain why this configuration works and not the others, it could help me and maybe others to understand.
Here is the working configuration :
public static decrypt(encryptedData: Buffer): Buffer
var decipher = crypto.createDecipheriv(process.env.CYPHER_ALGO, process.env.CYPHER_PASSWORD, "");
var s;
try
var b1: Buffer = decipher.update(encryptedData);
var b2: Buffer = decipher.final();
s = Buffer.concat([b1, b2]);
catch (e)
console.log(e);
return s;
// ALGO : 'aes-128-ecb'
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
StackOverflow isn't a code writing service. Show what you've tried.
– Luke Joshua Park
Aug 9 at 10:40