Sachin
Sachin

Reputation: 32

AES encrypt in Angular and decrypt in Python

I am trying to encrypt a password in Angular using crypto-js and decrypt in Python using pycryptodome.

My Angular code is -

import * as CryptoJS from 'crypto-js';
secretKey = "lazydog";
password = "This is to be encrypted";
enc: any;
encrypt(value : string) : string{
    return CryptoJS.AES.encrypt(value, this.secretKey.trim()).toString();
  }
decrypt(textToDecrypt : string){
    return CryptoJS.AES.decrypt(textToDecrypt, this.secretKey.trim()).toString(CryptoJS.enc.Utf8);
  }

In the constructor -

this.enc = this.encrypt(this.password);
console.log(this.enc);

In Python, my code is -

from Crypto.Cipher import AES
from Crypto.Util.Padding import pad, unpad
import base64
secretKey = "lazydog"
data = "s2SS6ugKbTGjTaT0U+X8mw=="
def encrypt(raw):
        raw = pad(raw.encode(),16)
        cipher = AES.new(key.encode('utf-8'), AES.MODE_ECB)
        return base64.b64encode(cipher.encrypt(raw))
def decrypt(enc):
        enc = base64.b64decode(enc)
        cipher = AES.new(key.encode('utf-8'), AES.MODE_ECB)
        return unpad(cipher.decrypt(enc),16)

decrypted = decrypt(encrypted)
print('data: ',decrypted.decode("utf-8", "ignore"))

I am getting error that the padding is incorrect.

Please someone help as I am not good in it. PS: I tried most of the available solutions in stack overflow and internet to no avail.

Upvotes: -2

Views: 349

Answers (1)

bereal
bereal

Reputation: 34290

According to the docs, the default mode in crypto-js is CBC, and you're using ECB on the Python side. Try AES.MODE_CBC instead.

Upvotes: 1

Related Questions