I am sending some data through socket using python and receive using JavaScript. At sender I am applying AES256 encryption and at receiver I have to decrypt the data back.
For sender I am using pycrypto library
Sender python
class AES_256(object):
def __init__(self, key):
self.bs = AES.block_size
self.key = hashlib.sha256(key.encode("utf-8")).digest()
def encrypt(self, raw):
private_key = self.key
#raw = pad(raw)
iv = Random.new().read(AES.block_size)
cipher = AES.new(private_key, AES.MODE_CBC, iv)
return base64.b64encode(iv + cipher.encrypt(raw))
..........................................
..........................................
encryption_256 = AES_256("password")
encrData = encryption_256.encrypt(bytes(buffer))
And at Javascript receiver side I am using crypto-js to decrypt this data, where I am receiving data as ArrayBuffer.
JavaScript receiver
var i8a = new Uint8Array(encodedFrame.data); // is of type ArrayBuffer
var wordArray = CryptoJS.lib.WordArray.create(i8a);
var key = CryptoJS.enc.Hex.parse(CryptoJS.SHA256("password").toString().toUpperCase());
var bytes = CryptoJS.AES.decrypt(wordArray, cryptkey, { iv:CryptoJS.enc.Hex.parse('00000000000000000000000000000000'), mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7});
But I am getting bytes length 0 after decryption. I am referring code here How to decrypt an ArrayBuffer?
Please give some advice how I can fix it.
encodedFrame.data, the raw data or the Base64 encoded data?encryptfrom the Python code returns the data Base64 encoded. My question was whether the data inencodedFrame.dataare these Base64 encoded data or the already decoded data. And what does the link in your comment mean? I thought the question was about decrypting the ciphertext from the Python code! Or am I misunderstanding something here? And one more thing: PyCrypto / PyCryptodome doesn't pad automatically, so which padding is used in the Python code, PKCS7 as the most recent JavaScipt code implies?49, 110, 84, 82, 73, 77, 107, 69, 108, 104, 70, 56, 53, 80, 89, 85, 115, 86, 54, 89, 47using the code,str = str+", "+ encodedFrame.data.getUint8(i);1nTRIMkElhF85PYUsV6Y/. These are all Base64 characters, so the data is probably Base64 encoded after all.