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.

CodeDezk
  • 1,230
  • 1
  • 12
  • 40
  • 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? – Topaco Jun 09 '20 at 16:55
  • Its actually webrtc media stream data based on example here https://github.com/webrtc/samples/blob/2f3a097b60332a32fc51d2a452985c7c1ac213ff/src/content/peerconnection/endtoend-encryption/js/worker.js#L81. I have modified the question – CodeDezk Jun 09 '20 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? – Topaco Jun 09 '20 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); ` – CodeDezk Jun 09 '20 at 18:59
  • This sequence corresponds (ASCII-) decoded to the string `1nTRIMkElhF85PYUsV6Y/`. These are all Base64 characters, so the data is probably [Base64](https://en.wikipedia.org/wiki/Base64#Base64_table) encoded after all. – Topaco Jun 09 '20 at 19:38
  • I have analysed the value `encodedFrame.data` and I can see there are values between 0 to 255 – CodeDezk Jun 10 '20 at 06:26
  • This excludes Base64. So there is a contradiction: The ciphertext generated by the Python `encrypt` method should be decrypted. But it is Base64 encoded. On the other hand, the data in `encodedFrame.data` is obviously _not_ Base64 encoded. So here at least the Base64 decoding is missing in the chain, possibly more (also the padding is not clear). Thus: _Does `encodedFrame.data` really contain the ciphertext of the Python `encrypt` method?_ A decryption must be _tailor-made_ for encryption (in all details), otherwise it will generally fail. – Topaco Jun 10 '20 at 06:46

0 Answers0