0

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.

7
  • In the JavaScript code, the derivation of the key from the digest and the separation of IV and ciphertext are missing. What is contained in encodedFrame.data, the raw data or the Base64 encoded data? Commented Jun 9, 2020 at 16:55
  • Its actually webrtc media stream data based on example here github.com/webrtc/samples/blob/…. I have modified the question Commented Jun 9, 2020 at 17:24
  • encrypt from the Python code returns the data Base64 encoded. My question was whether the data in encodedFrame.data are 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? Commented Jun 9, 2020 at 18:12
  • I think the data is not base64 encoded beacuse when I print the data I am getting 49, 110, 84, 82, 73, 77, 107, 69, 108, 104, 70, 56, 53, 80, 89, 85, 115, 86, 54, 89, 47 using the code, str = str+", "+ encodedFrame.data.getUint8(i); Commented Jun 9, 2020 at 18:59
  • This sequence corresponds (ASCII-) decoded to the string 1nTRIMkElhF85PYUsV6Y/. These are all Base64 characters, so the data is probably Base64 encoded after all. Commented Jun 9, 2020 at 19:38

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.