OpenSSL提供了一个流行的(但不安全的--见下文!)用于AES加密的命令行界面:
openssl aes-256-cbc -salt -in filename -out filename.encPython以PyCrypto包的形式支持AES,但它只提供工具。如何使用Python/PyCrypto对使用OpenSSL加密的文件进行解密?
通知
这个问题过去也涉及到Python中使用相同方案的加密。后来,我删除了这一部分,以阻止任何人使用它。不要用这种方式加密更多的数据,因为按照今天的标准,它是不安全的。您应该只使用解密,除了向后兼容之外没有其他原因,即当您没有其他选择的时候。想要加密吗?如果可能的话,使用NaCl/libsodium。
发布于 2013-05-27 00:47:42
考虑到Python的流行,一开始我对这个问题没有完整的答案感到失望。我花了相当多的时间阅读了这块板上的不同答案,以及其他资源,才把它弄对了。我想我可能会分享结果以供将来参考或审查;我绝不是密码学专家!但是,下面的代码似乎可以无缝地工作:
from hashlib import md5
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)用法:
with open(in_filename, 'rb') as in_file, open(out_filename, 'wb') as out_file:
decrypt(in_file, out_file, password)如果您看到有机会改进这一点或将其扩展为更灵活(例如,让它在没有盐的情况下工作,或提供Python 3兼容性),请随时这样做。
通知
这个答案过去也涉及到Python中使用相同方案的加密。后来,我删除了这一部分,以阻止任何人使用它。不要用这种方式加密更多的数据,因为按照今天的标准,它是不安全的。您应该只使用解密,除了向后兼容之外没有其他原因,即当您没有其他选择的时候。想要加密吗?如果可能的话,使用NaCl/libsodium。
发布于 2013-12-09 02:54:31
我正在重新发布您的代码,并进行了一些更正(我不想使您的版本变得模糊)。虽然您的代码可以正常工作,但它不会检测到有关填充的一些错误。特别是,如果提供的解密密钥不正确,填充逻辑可能会做一些奇怪的事情。如果您同意我的更改,您可以更新您的解决方案。
from hashlib import md5
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]
# This encryption mode is no longer secure by today's standards.
# See note in original question above.
def obsolete_encrypt(in_file, out_file, password, key_length=32):
bs = AES.block_size
salt = Random.new().read(bs - len('Salted__'))
key, iv = derive_key_and_iv(password, salt, key_length, bs)
cipher = AES.new(key, AES.MODE_CBC, iv)
out_file.write('Salted__' + salt)
finished = False
while not finished:
chunk = in_file.read(1024 * bs)
if len(chunk) == 0 or len(chunk) % bs != 0:
padding_length = bs - (len(chunk) % bs)
chunk += padding_length * chr(padding_length)
finished = True
out_file.write(cipher.encrypt(chunk))
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])
if padding_length < 1 or padding_length > bs:
raise ValueError("bad decrypt pad (%d)" % padding_length)
# all the pad-bytes must be the same
if chunk[-padding_length:] != (padding_length * chr(padding_length)):
# this is similar to the bad decrypt:evp_enc.c from openssl program
raise ValueError("bad decrypt")
chunk = chunk[:-padding_length]
finished = True
out_file.write(chunk)发布于 2014-02-12 01:33:13
下面的代码应该与Python3兼容,并与代码中记录的小更改兼容。我也想用os.urandom代替Crypto.Random。'Salted__‘被替换为salt_header,如果需要,可以定制或保留为空。
from os import urandom
from hashlib import md5
from Crypto.Cipher import AES
def derive_key_and_iv(password, salt, key_length, iv_length):
d = d_i = b'' # changed '' to b''
while len(d) < key_length + iv_length:
# changed password to str.encode(password)
d_i = md5(d_i + str.encode(password) + salt).digest()
d += d_i
return d[:key_length], d[key_length:key_length+iv_length]
def encrypt(in_file, out_file, password, salt_header='', key_length=32):
# added salt_header=''
bs = AES.block_size
# replaced Crypt.Random with os.urandom
salt = urandom(bs - len(salt_header))
key, iv = derive_key_and_iv(password, salt, key_length, bs)
cipher = AES.new(key, AES.MODE_CBC, iv)
# changed 'Salted__' to str.encode(salt_header)
out_file.write(str.encode(salt_header) + salt)
finished = False
while not finished:
chunk = in_file.read(1024 * bs)
if len(chunk) == 0 or len(chunk) % bs != 0:
padding_length = (bs - len(chunk) % bs) or bs
# changed right side to str.encode(...)
chunk += str.encode(
padding_length * chr(padding_length))
finished = True
out_file.write(cipher.encrypt(chunk))
def decrypt(in_file, out_file, password, salt_header='', key_length=32):
# added salt_header=''
bs = AES.block_size
# changed 'Salted__' to salt_header
salt = in_file.read(bs)[len(salt_header):]
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 = chunk[-1] # removed ord(...) as unnecessary
chunk = chunk[:-padding_length]
finished = True
out_file.write(bytes(x for x in chunk)) # changed chunk to bytes(...)https://stackoverflow.com/questions/16761458
复制相似问题