Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23131

AES simple encryption in node and decryption in python

$
0
0

NODE Environment

I generate secretKey in nodejs as below,

const generateAesKey = () => {    const { randomBytes } = require('node:crypto');    randomBytes(32, (err, buf) => {        if (err) throw err;        return buf.toString('hex');    });}const secretKey = generateAesKey(); 

SecretKey looks something like this : 491fb9719864f51e19a0705a3ef2de15cd91576d881cdc4bd4394bf7451ee404

Using above key, I encrypt and decrypt data as below (Node environment)

const CryptoJS = require("crypto-js");const encrypt = (plainText) => {    const iv = CryptoJS.enc.Utf8.parse('BBBBBBBBBBBBBBBB');    const encrypted = CryptoJS.AES.encrypt(plainText, CryptoJS.enc.Utf8.parse(secretKey), {        iv: iv,        mode: CryptoJS.mode.CBC,    });    return encrypted.toString();}const decrypt = (cipherText) => {    const iv = CryptoJS.enc.Utf8.parse('BBBBBBBBBBBBBBBB');    const decrypted = CryptoJS.AES.decrypt(cipherText, CryptoJS.enc.Utf8.parse(secretKey), {        iv: iv,        mode: CryptoJS.mode.CBC,    });    return CryptoJS.enc.Utf8.stringify(decrypted);}const en = encrypt("Text to be encrypted");console.log(decrypt(en))

I am able to encrypt/decrypt data successfully in Node environment.The problem comes when I have to decrypt data in Python environment that I encrypted in Node environment.

Python Environment

I have written below Python code which is equivalent Node code (correct me if this statement is not right).

from Crypto.Cipher import AESfrom Crypto.Util.Padding import pad,unpadimport base64import jsonimport os#CBC with Fix IV# key = os.urandom(32)                                 // to generate random secret keykey =b'\x88.V\x8dXJ:\xdd\xeb \xc8uo\xe2\xff9\x02\xbe\xa6\x81W~M/B3\x02\x17\x95\x15\xa6\xdd'  // this key was generated by above line os.urandom(32)data = 'Text to be encrypted'#FIX IViv =  'BBBBBBBBBBBBBBBB'.encode('utf-8') #16 char for AES128def encrypt(data,key,iv):        data= pad(data.encode(),16)        cipher = AES.new(key,AES.MODE_CBC,iv)        return base64.b64encode(cipher.encrypt(data))def decrypt(enc,key,iv):        enc = base64.b64decode(enc)        cipher = AES.new(key, AES.MODE_CBC, iv)        return unpad(cipher.decrypt(enc),16)def lambda_handler(event, context):    encrypted = encrypt(data,key,iv)    print('encrypted CBC base64 : ',encrypted.decode("utf-8", "ignore"))    decrypted = decrypt(encrypted,key,iv)    print('decrypted data: ', decrypted.decode("utf-8", "ignore"))    return {'statusCode': 200,'decrypted text': json.dumps(decrypted.decode("utf-8", "ignore"))    }

I am able to encrypt/decrypt data successfully in Python environment now when I use \x88.V\x8dXJ:\xdd\xeb \xc8uo\xe2\xff9\x02\xbe\xa6\x81W~M/B3\x02\x17\x95\x15\xa6\xdd secretKey but when I use 491fb9719864f51e19a0705a3ef2de15cd91576d881cdc4bd4394bf7451ee404 in Python, it throws below error,

errorMessage": "Incorrect AES key length (64 bytes)","errorType": "ValueError",

My main purpose is to encrypt data in Node and decrypt it in Python. What am I doing wrong ?

Please help. I really need a solution. Been trying to resolve since many days...


Viewing all articles
Browse latest Browse all 23131

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>