如何通过Python解密用于AES加密AES

时间:2017-11-29 02:31:54

标签: python encryption aes

我通过PHP获得AES加密的十六进制文本。这是:

守则如下:

<?php
$str = "abcdefghijklmnopqrstuvwxyz";
$method = "aes-256-cbc";
$key = 'pqrstuvwxyz$abcdefghijAB';
$iv = 'DEFGHTABCIESPQXO';
echo bin2hex(openssl_encrypt($str, $method, $key, OPENSSL_RAW_DATA, $iv));
echo "\n";
?>

2324ab5ec7a901247bf01b08bd1956688843dad5a8e15106ca3a5b9258918527

我制作了Python脚本:

import binascii
from Crypto.Cipher import AES

data = "abcdefghijklmnopqrstuvwxyz"
key = "pqrstuvwxyz$abcdefghijAB"
iv = "DEFGHTABCIESPQXO"

encrypted = "2324ab5ec7a901247bf01b08bd1956688843dad5a8e15106ca3a5b9258918527"

cipher = AES.new(key, AES.MODE_CBC, iv)
decrypted = cipher.decrypt(binascii.unhexlify(encrypted))
print(decrypted)

我得到了这个,它与“abcdefghijklmnopqrstuvwxyz”的字符串不同,:

b' \xea\xbc\x13\x98!xw\x0c,\xac6\xeb\x1c\x14\\Z\x0b\x0fTY\xbe\x0b^W\xcem\xa4\x9ea\xde\x1b'

我收到了错误

decrypted.decode('utf-8')

UnicodeDecodeError: 'utf-8' codec can't decode bytes in position 1-2: invalid continuation byte

我想解决这个Python脚本。

1 个答案:

答案 0 :(得分:0)

修改后的PHP代码

a

我得到了新的加密字符串:

b

然后,我修改了密钥的Python代码,

$key = 'pqrstuvwxyz$abcdefghijAB12345678';

我收到了以下内容。

4a3d8b7449fb45c521b04314382670e9c4255682cde4fda51d715a3631718ccb

我添加了删除

key = "pqrstuvwxyz$abcdefghijAB12345678"

我解决了!!