python aes加密文件_python-PyCrypto可以检查文件是否已被AES加密?

该博客介绍了如何使用PyCrypto库用AES(CBC模式)加密Python文件,并探讨了如何在多次加密后检查文件是否已被AES加密的可能性。
摘要由CSDN通过智能技术生成

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:

The encryption key - a string that must be

either 16, 24 or 32 bytes long. Longer keys

are more secure.

in_filename:

Name of the input file

out_filename:

If None, '.enc' will be used.

chunksize:

Sets the size of the chunk which the function

uses to read and encrypt the file. Larger chunk

sizes can be faster for some files and machines.

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('

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))

这就是我加密文件的方式,但是如果您在同一文件上运行两次或更多次,它将继续对其进行加密,而不会出现任何问题,我想添加某种if检查来检查它是否尚未被AES加密吗?这可能吗?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值