openssl aes128ecb加密与python不匹配密码密码AES加密

2024-04-30 01:36:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试加密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。普通.txt是一个文本文件,包含字符串“Attack at dawn!!”公司名称:

^{pr2}$

这将返回“X+fHjd97VRZLbH+BCgu6Xw==”作为密文。在

为什么它们不一样?在

我试过阅读文档或示例,但在互联网上没有发现任何有用的东西。我尝试过更改openssl命令中的选项。我不知道如何解决这个问题。在


Tags: 字符串fromimportobj密码字节ataes
1条回答
网友
1楼 · 发布于 2024-04-30 01:36:19

在python中,使用AES加密密码密码接受一个密钥(16个字节的字符串)和一个明文(16个字节)并输出一个密文(16个字节)。在

要使用OpenSSL实现同样的效果,首先需要禁用-nosalt-nopad的salting和padding,以确保它接受16字节的输入并返回16字节的输出。提供密码会导致OpenSSL派生自己的密钥。要覆盖此选项,请使用-K选项(其中密钥需要以十六进制表示)。或者,输入密码并指定-p将使OpenSSL输出它使用的密钥。在

  • 使用“黄色潜水艇”键:

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

开放式SSL

^{pr2}$

这两种方法的“kBKTLPWpU7Dpf/TiGo6p3w==”。在

  • 使用OpenSSL密码“黄色潜水艇”:

    openssl enc-aes-128-ecb-nosalt-nopad-p-pass pass:“黄色潜艇”-输入普通.txt-出去密码.txt-a

这将输出密钥“A35EC217E15C1DD258201A184814897C”。把这个和密码密码,我们首先需要把它转换成hex。在

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

这两个方法都得到了“X+fHjd97VRZLbH+BCgu6Xw==”。在

  • 要回答最后一个问题:您应该更仔细地阅读opensslenc的手册,并找到-p-P和{}选项。在

相关问题 更多 >