我正在尝试加密16字节的字符串“黎明时攻击!!”使用密码为“黄色潜水艇”的AES-128。
在python中:
from Crypto.Cipher import AES
from base64 import b64encode
plaintext = 'Attack at dawn!!'
obj = AES.new("yellow submarine", AES.MODE_ECB)
ciphertext = obj.encrypt(plaintext)
print(b64encode(ciphertext).decode())
这使得密文为'kBKTLPWpU7Dpf / TiGo6p3w =='
现在在终端中使用openssl。 plain.txt是一个包含字符串'Attack at dawn !!'的文本文件:
openssl enc -aes-128-ecb -nosalt -nopad -pass pass:"yellow submarine" -in plain.txt -out cipher.txt -a
这将返回'X + fHjd97VRZLbH + BCgu6Xw =='作为密文。
他们为什么不一样?
我试过阅读文档或示例,但在互联网上找不到任何有用的东西。我已经尝试更改openssl命令中的选项。我不知道如何处理这个问题。
答案 0 :(得分:1)
在python中,使用来自Crypto.Cipher的AES进行加密需要一个密钥(16字节的字符串)和一个明文(16字节)并输出一个密文(16字节)。
要实现与OpenSSL相同的操作,您需要先使用ionViewCanEnter
和-nosalt
禁用salting和padding,以确保它需要16个字节的输入并返回16个字节的输出。提供密码会导致OpenSSL派生自己的密钥。要覆盖它,请使用-nopad
选项(其中密钥需要以十六进制形式给出)。或者,输入密码并指定-K
将使OpenSSL吐出它使用的密钥。
蟒
-p
OpenSSL的
from Crypto.Cipher import AES
from base64 import b64encode
plaintext = 'Attack at dawn!!'
obj = AES.new("yellow submarine", AES.MODE_ECB)
ciphertext = obj.encrypt(plaintext)
print(b64encode(ciphertext).decode())
这两种方法都给出了“kBKTLPWpU7Dpf / TiGo6p3w ==”。
使用OpenSSL密码'黄色潜水艇':
openssl enc -aes-128-ecb -nosalt -nopad -p -pass pass:“yellow submarine”-in plain.txt -out cipher.txt -a
输出密钥为'A35EC217E15C1DD258201A184814897C'。要在Crypto.Cipher中使用它,我们首先需要将其转换为十六进制。
enc -aes-128-ecb openssl enc -aes-128-ecb -nosalt -nopad -K 79656c6c6f77207375626d6172696e65 -in plain.txt -out cipher.txt -a
这给出了两种方法的'X + fHjd97VRZLbH + BCgu6Xw =='。
from Crypto.Cipher import AES
from base64 import b64encode
hex_key = 'A35EC217E15C1DD258201A184814897C'
key = bytes.fromhex(hex_key)
plaintext = 'Attack at dawn!!'
obj = AES.new(key, AES.MODE_ECB)
ciphertext = obj.encrypt(plaintext)
print(b64encode(ciphertext).decode())
,-p
和-P
选项。