I have been unable to get existing legacy code in Python to encrypt/decrypt the same as NodeJS. I do get the first 16 characters correctly decoded.
Here is the Python code:
from Crypto.Cipher import AES
counter = b'HOJFKGQMRCTKUQHP'
cipher = AES.new(self.symmetric_key, AES.MODE_CTR, counter=lambda: counter)
encrypted = cipher.encrypt(data)
I found a source that the same counter is used in each iteration: PyCrypto problem using AES+CTR
What does work in NodeJS (ts-node) for the first 16 characters only is this:
import { createDecipheriv, Decipher } from 'crypto'
const decrypt = (inputBase64: string): string => {
const algorithm = 'aes-192-ctr'; // 24 byte key, 16 byte "counter"
var decipher: Decipher = createDecipheriv(algorithm, key, counter /* yes, it's an iv */)
// decipher.setAutoPadding(false);
return decipher.update(inputBase64, 'base64', 'utf8') + decipher.final('utf8');
}
I have found various online sources, which all increment the counter - is there a way to control the counter increment with the built-in Node crypto library? I have found some online implementations that I could possibly override (if that is what's happening): https://github.com/ricmoo/aes-js/blob/master/index.js#L656 https://github.com/brix/crypto-js/blob/develop/src/mode-ctr.js#L26
How can I get this to work in Node? My python code (which is legacy and I cannot change without migrating existing values) has this output:
encrypt('Testing--StackOverflow')
# outputs: 'r7G8gFNIHuY27nBjSo51nZ6mqZhVUQ=='
From node using above decrypt
function:
const key = 'LKOXBRRUNBOSMENKEPPZUKWB';
const counter = 'HOJFKGQMRCTKUQHP';
const encrypted = 'r7G8gFNIHuY27nBjSo51nZ6mqZhVUQ==';
const clearText = decrypt(encrypted);
console.log('clear text:', clearText)
// outputs: clear text: Testing--StackOv�::��m
Hope somebody can share some insight here!