0

I have Encrypted a string in python using pycryptodome lib and I want to decrypt the string in react using cryptoJs, but I could not find any proper way to do that.

this is my code in python:

def encrypt_string(string):
   aes = AES.new(secret_key, AES.MODE_CFB, secret_iv)
    encrypted = aes.encrypt(string.encode('utf-8'))
    return b64encode(encrypted).decode('utf-8')

js:

let decrypted = crypto.AES.decrypt(crypto.enc.Base64.parse(encrypted_string), key, { mode: crypto.mode.CFB, iv })
console.log(decrypted.toString(crypto.enc.Utf8)) //returns empty string

this implementation returns an empty string.

Fateme
  • 1
  • 2
  • Both codes use different segment sizes for CFB, additionally in the CryptoJS code the ciphertext is passed incorrectly and the padding is not disabled. Have a look at the documentation of PyCryptodome and CryptoJS. – Topaco Jun 15 '23 at 17:13
  • @Topaco thanks for your help, unfortunately I'm from iran and cryptoJs documentation is locked for us :) so I should search stackoverflow, github or other unofficial docs. If you can help with the implementation of js part it would really appreciated. with python lib I can encrypt and decrypt with no problem. – Fateme Jun 16 '23 at 13:10
  • The Python code must also be changed, because the default segment size of PyCryptodome is not supported by CryptoJS. If the Python code cannot be changed, you need another JavaScript library. If the Python code can be changed: Have a look here for the [fixed Python/PyCryptodome code](https://replit.com/@3hK8cL8H24hwiS7/CultivatedLimeChemistry#main.py) and here for the [fixed JavaScript/CryptoJS code](https://jsfiddle.net/452m3t96/). Explanations in the code. – Topaco Jun 16 '23 at 20:39
  • @Topaco thank you, you saved me from thousands of unsuccessful searches. – Fateme Jun 16 '23 at 23:20

0 Answers0