AES 在 Angular 中加密并在 Python 中解密

问题描述 投票:0回答:1

我正在尝试使用 crypto-js 在 Angular 中加密密码,并使用 pycryptodome 在 Python 中解密。

我的角度代码是-

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);
  }

在构造函数中 -

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

在Python中,我的代码是-

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"))

我收到填充不正确的错误。

请有人帮忙,因为我不擅长。 PS:我尝试了堆栈溢出和互联网中的大多数可用解决方案,但均无济于事。

python-3.x angular encryption cryptojs pycryptodome
1个回答
0
投票

根据docs

crypto-js
中的默认模式是CBC,并且您在Python端使用ECB。尝试改为
AES.MODE_CBC

© www.soinside.com 2019 - 2024. All rights reserved.