I have following python code for AES decryption using pycryptodome, and which I verified and working.
key = "password"
key = hashlib.sha256(key.encode("utf-8")).digest()
iv = b'This is an IV456' #Random.new().read(AES.block_size)
iv_int = int(binascii.hexlify(iv), 16) # Convert the IV to a Python integer.
ctr = Counter.new(AES.block_size * 8, initial_value=iv_int) # Create a new Counter object with IV = iv_int.
aes = AES.new(key, AES.MODE_CTR, counter=ctr)
decryptData = aes.decrypt(encryptedData) # Decrypt and return the plaintext.
Now I need to convert this code in python using CryptoJS, I have done some coding but no idea how counter ctr generated.
var i8a = new Uint8Array(data);
var password = "password";
var key = CryptoJS.SHA256(password);
var input = CryptoJS.lib.WordArray.create(i8a);
var output =CryptoJS.AES.decrypt(input, key, { mode: CryptoJS.mode.CTR});
var str = output.toString(CryptoJS.enc.Base64);
But the str is always none.
Edit:
Based on below comment I have added iv and noPadding option with config but still the decrypt return empty.
var iv = "112197289293498629157884805399637669174";
//iv = CryptoJS.enc.Base64.parse(iv); //enable this doesn't make any changes.
var output =CryptoJS.AES.decrypt(input, key,{ mode: CryptoJS.mode.CTR,iv:iv,padding: CryptoJS.pad.NoPadding});
Where I got the iv value from python side iv_int.
And cannot remove the counter from python script where I am getting error like,
'counter' keyword parameter is required with CTR mode
Note: I am sending the encrypted data from python to js through webRTC data channel.
Edit2:
The encrypt code in python which I actually used in python,
def encrypt(row):
key = "password"
key = hashlib.sha256(key.encode("utf-8")).digest()
iv = b'This is an IV456' #Random.new().read(AES.block_size)
iv_int = int(binascii.hexlify(iv), 16)
ctr = Counter.new(AES.block_size * 8, initial_value=iv_int)
cipher = AES.new(self.key, AES.MODE_CTR,counter=ctr)
return base64.b64encode(cipher.encrypt(raw))
Input data for encryption
fp = open(filepathFull, "rb")
while (True) and not done_reading:
data = fp.read(16384)
encrypted = encrypt(data)
sendWebrtcDataChannel(encrypted)