Encryption Openssl 1.0.2p解密失败?

Encryption Openssl 1.0.2p解密失败?,encryption,openssl,pycrypto,Encryption,Openssl,Pycrypto,我使用1.0.2p使用以下命令对文件进行加密 #openssl aes-128-cbc -e -k 'abcdefghijklmnop' -in my.txt -out myencrypt.txt 我的解密基于Crypto.Cipher python模块。 这是我的密码。但是,我无法成功解密文本。 我不确定我错过了什么 from Crypto.Cipher import AES def decrypt(ciphertext, key): iv = ciphertext[:AES.

我使用1.0.2p使用以下命令对文件进行加密

#openssl aes-128-cbc  -e -k 'abcdefghijklmnop' -in my.txt -out myencrypt.txt
我的解密基于Crypto.Cipher python模块。 这是我的密码。但是,我无法成功解密文本。 我不确定我错过了什么

from Crypto.Cipher import AES


def decrypt(ciphertext, key):
    iv = ciphertext[:AES.block_size]
    cipher = AES.new(key, AES.MODE_CBC, iv)
    plaintext = cipher.decrypt(ciphertext[AES.block_size:])
    return plaintext.rstrip(b"\0")

def decrypt_file(file_name, key):
    with open(file_name, 'rb') as encrypt_file:
        ciphertext = encrypt_file.read()
    dec = decrypt(ciphertext, key)
    with open("plain.txt", "wb") as plain_file:
        plain_file.write(dec)

if __name__ == "__main__":
    decrypt_file('myencrypt.txt', 'abcdefghijklmnop')

您的问题可能是OpenSSL 1.0.2仍然使用MD5作为其哈希算法,而不是SHA。我不太熟悉这个Python库,但从它看来,默认的哈希算法是SHA1

从new()的参数部分:

hashAlgo(哈希对象)–要使用的哈希函数。这可以是Crypto.Hash下的模块,也可以是从任何此类模块创建的现有哈希对象。如果未指定,则使用Crypto.Hash.SHA1

因此,我认为正在发生的是,加密线路(使用OpenSSL 1.0.2)在加密时使用MD5,但Python库默认为SHA1。因此,在调用
new()
时,您需要更新OpenSSL版本和/或在Python代码中指定哈希算法

我相信OpenSSL在1.1.0中进行了更新,使用SHA算法进行散列。实际上,我在尝试从旧服务器解密存档文件时遇到了这个问题,使用OpenSSL进行加密和解密,但版本不匹配。如果从较旧版本的OpenSSL解密存档时遇到任何问题,则可能必须使用
-md md5
指定较旧的哈希算法,如:

openssl enc -aes256 -d -in your/input/file.encrypt -out your/output/file -md md5