Please help me on solving below problem/issue:
I have a document which is encrypted using the algorithm AES/CTR/NOPADDING. The encryption is made by a project developped using Java.
We recently launched a migration project aiming to migrate all our microservice from Java to NodeJS. The current microservice responsible for the encryption can't be migrated right now.
My task actually is to migrate one of these microservice from Java to NodeJS, this microsevice should be able to decrypt these document. I'm facing an issue when trying to decrypt the document, result is still an encrypted document instead of an Excel file...
I'm sharing with you:
- The Java code for encryption
- The node code for decryption
I have no knowledge of Java or cryptography so any help in that direction will be highly appreciated.
Java code:
/ **
* Encryption utilities.
*
*/
public class EncryptionUtil {
/**
* Salt.
*/
private static final String SALT = "dummydummydummy1";
static {
// Initialize Bouncy Castle provider
Security.insertProviderAt(new BouncyCastleProvider(), 1);
}
private String privateKey = "kais"
/**
* Initialize a Cipher.
*
* @param privateKey Private key
* @param mode Mode (encrypt or decrypt)
* @return Cipher
* @throws Exception e
*/
private static Cipher getCipher(String privateKey, int mode) throws Exception {
PBEKeySpec keySpec = new PBEKeySpec(privateKey.toCharArray(), SALT.getBytes(), 2000, 256);
SecretKeyFactory skf = SecretKeyFactory.getInstance("PBEWITHSHA256AND256BITAES-CBC-BC");
SecretKey desKey = skf.generateSecret(keySpec);
Cipher cipher = Cipher.getInstance("AES/CTR/NOPADDING");
cipher.init(mode, desKey);
return cipher;
}
}
Node code:
const algorithm = 'aes-256-ctr';
const SALT = "dummydummydummy1";
const iv = crypto.randomBytes(16);
const secretKey = 'kais'
// input file
const r = fs.createReadStream(appRoot + `/1c6dd5c1-02ff-49c9-813b-a554fd94344e`);
// decrypt content
const decrypt = crypto.createDecipheriv(algorithm, SALT, iv);
// write file
const w = fs.createWriteStream(appRoot + `/Jiratraited.xlsx`);
// start pipe
r.pipe(decrypt).pipe(w);