Python 和 OpenSSL:无法解密
Python and OpenSSL: unable to decrypt
我找到了一小段代码,可以在 AES(128 位)CBC 模式下加密(和解密)文件。它甚至在解密时也能完美工作,所以我相信 OpenSSL 能够(当然)解密我的文件,但这似乎是不可能的。我收到“读取输入文件时出错”
import os, random, struct
from Crypto.Cipher import AES
def encrypt_file(key, in_filename, out_filename=None, chunksize=64*1024):
""" Encrypts a file using AES (CBC mode) with the
given key.
key:
16, 24 or 32 bytes long
in_filename:
Name of the input file
out_filename:
If None, '<in_filename>.enc' will be used.
chunksize:
Sets the size of the chunk which the function
uses to read and encrypt the file.
Chunksize must be divisible by 16.
"""
if not out_filename:
out_filename = in_filename + '.enc'
iv = ''.join(chr(random.randint(0, 0xFF)) for i in range(16))
encryptor = AES.new(key, AES.MODE_CBC, iv)
filesize = os.path.getsize(in_filename)
with open(in_filename, 'rb') as infile:
with open(out_filename, 'wb') as outfile:
outfile.write(struct.pack('<Q', filesize))
outfile.write(iv)
while True:
chunk = infile.read(chunksize)
if len(chunk) == 0:
break
elif len(chunk) % 16 != 0:
chunk += ' ' * (16 - len(chunk) % 16)
outfile.write(encryptor.encrypt(chunk))
错误每次都一样:"Error reading input files"。这怎么可能?我使用的命令是这样的:
openssl aes-128-cbc -d -in test_enc.txt -out test_dec.txt
为什么不起作用?
OpenSSL 使用自己的文件格式。特别是,有一个 Salted__
header,以及用于派生 IV 的盐(即 IV 不直接与加密数据一起存储)。
您可以在 https://scottlinux.com/2013/10/13/how-to-encrypt-a-file-with-openssl/ and in https://crypto.stackexchange.com/questions/3298/is-there-a-standard-for-openssl-interoperable-aes-encryption 中找到一些提示,但它们并未描述实际格式。
您可以检查 OpenSSL 代码来弄清楚这一点,但底线是 OpenSSL 使用自己的文件格式,如果您希望 OpenSSL 解密您的文件,则必须使用它。
我找到了一小段代码,可以在 AES(128 位)CBC 模式下加密(和解密)文件。它甚至在解密时也能完美工作,所以我相信 OpenSSL 能够(当然)解密我的文件,但这似乎是不可能的。我收到“读取输入文件时出错”
import os, random, struct
from Crypto.Cipher import AES
def encrypt_file(key, in_filename, out_filename=None, chunksize=64*1024):
""" Encrypts a file using AES (CBC mode) with the
given key.
key:
16, 24 or 32 bytes long
in_filename:
Name of the input file
out_filename:
If None, '<in_filename>.enc' will be used.
chunksize:
Sets the size of the chunk which the function
uses to read and encrypt the file.
Chunksize must be divisible by 16.
"""
if not out_filename:
out_filename = in_filename + '.enc'
iv = ''.join(chr(random.randint(0, 0xFF)) for i in range(16))
encryptor = AES.new(key, AES.MODE_CBC, iv)
filesize = os.path.getsize(in_filename)
with open(in_filename, 'rb') as infile:
with open(out_filename, 'wb') as outfile:
outfile.write(struct.pack('<Q', filesize))
outfile.write(iv)
while True:
chunk = infile.read(chunksize)
if len(chunk) == 0:
break
elif len(chunk) % 16 != 0:
chunk += ' ' * (16 - len(chunk) % 16)
outfile.write(encryptor.encrypt(chunk))
错误每次都一样:"Error reading input files"。这怎么可能?我使用的命令是这样的:
openssl aes-128-cbc -d -in test_enc.txt -out test_dec.txt
为什么不起作用?
OpenSSL 使用自己的文件格式。特别是,有一个 Salted__
header,以及用于派生 IV 的盐(即 IV 不直接与加密数据一起存储)。
您可以在 https://scottlinux.com/2013/10/13/how-to-encrypt-a-file-with-openssl/ and in https://crypto.stackexchange.com/questions/3298/is-there-a-standard-for-openssl-interoperable-aes-encryption 中找到一些提示,但它们并未描述实际格式。
您可以检查 OpenSSL 代码来弄清楚这一点,但底线是 OpenSSL 使用自己的文件格式,如果您希望 OpenSSL 解密您的文件,则必须使用它。