有没有一种方法可以在Python 3.x中解密OpenSSL AES加密的文件

时间:2020-01-26 11:50:02

标签: python python-3.x encryption openssl aes

我在终端中使用openssl aes-256-cbc命令加密了一些文件

openssl aes-256-cbc -in filename.txt -out filename.enc -k password

不幸的是,我无法使用Python 3.x解密那些文件

我尝试使用this answer中的代码,但发生TypeError

from Crypto.Cipher import AES
from Crypto import Random

def derive_key_and_iv(password, salt, key_length, iv_length):
    d = d_i = ''
    while len(d) < key_length + iv_length:
        d_i = md5(d_i + password + salt).digest()
        d += d_i
    return d[:key_length], d[key_length:key_length+iv_length]

def decrypt(in_file, out_file, password, key_length=32):
    bs = AES.block_size
    salt = in_file.read(bs)[len('Salted__'):]
    key, iv = derive_key_and_iv(password, salt, key_length, bs)
    cipher = AES.new(key, AES.MODE_CBC, iv)
    next_chunk = ''
    finished = False
    while not finished:
        chunk, next_chunk = next_chunk, cipher.decrypt(in_file.read(1024 * bs))
        if len(next_chunk) == 0:
            padding_length = ord(chunk[-1])
            chunk = chunk[:-padding_length]
            finished = True
        out_file.write(chunk)

那么,python可以解密openssl aes加密的文件吗?

0 个答案:

没有答案