【问题标题】:Decrypt a file encrypted in Python using only OpenSSL仅使用 OpenSSL 解密在 Python 中加密的文件
【发布时间】:2018-05-19 21:22:00
【问题描述】:

我有一个用 Python 和pycryptodome 加密的文件,如下所示:

from Crypto.PublicKey import RSA
from Crypto.Random import get_random_bytes
from Crypto.Cipher import AES, PKCS1_OAEP

key = RSA.generate(2048)
secret_key = key.exportKey(passphrase='letmein', pkcs=8, protection='scryptAndAES128-CBC')
public_key = key.publickey().exportKey()

rsa_key = RSA.importKey(public_key)
session_key = get_random_bytes(16)
cipher_rsa = PKCS1_OAEP.new(rsa_key)
cipher_aes = AES.new(session_key, AES.MODE_EAX)
ciphertext, tag = cipher_aes.encrypt_and_digest(data)

dst.write(cipher_rsa.encrypt(session_key))
dst.write(cipher_aes.nonce)
dst.write(tag)
dst.write(ciphertext)

我可以这样解码:

rsa_key = RSA.importKey(secret_key, 'letmein')

enc_session_key, nonce, tag, ciphertext = [
    src.read(x) for x in (rsa_key.size_in_bytes(), 16, 16, -1)
]

cipher_rsa = PKCS1_OAEP.new(rsa_key)
session_key = cipher_rsa.decrypt(enc_session_key)
cipher_aes = AES.new(session_key, AES.MODE_EAX, nonce)

decoded = cipher_aes.decrypt_and_verify(ciphertext, tag)

有没有办法使用带有openssl 的命令行来解密文件?或者我应该如何修改代码以使其成为可能?

【问题讨论】:

    标签: python openssl cryptography aes rsa


    【解决方案1】:

    您可以对单独的组件进行 base 64 编码,然后使用分隔符将它们拆分。命令行主要是基于文本的,因此在例如重击。

    不直接支持 EAX 模式,因此尝试 CBC 模式会更容易。 OpenSSL 命令行似乎不支持任何 AEAD 密码目前,因此您将失去它可能提供的真实性。

    最后,OAEP 和密码的组合似乎不受支持,因此您可能必须处理二进制结果并将其转换为对称密码,例如十六进制。

    【讨论】:

      猜你喜欢
      • 2019-04-20
      • 2013-05-21
      • 2018-11-28
      • 1970-01-01
      • 2021-05-03
      • 2016-05-04
      • 1970-01-01
      • 2021-02-02
      • 2018-12-25
      相关资源
      最近更新 更多