python文本文件加密_Python PyCrypto使用AE加密/解密文本文件

在Python程序中,作者遇到了一个关于加密解密文件的问题。encrypt_file()和decrypt_file()函数在处理文本文件时出现错误。尽管encrypt()和decrypt()核心函数正常工作,但在读取和编码文件时,字节和字符串转换导致了TypeError和ValueError。错误出现在尝试解密文件时,可能是因为输入长度不符合AES块大小的要求。
摘要由CSDN通过智能技术生成

我已经有了一个工作程序,但唯一不工作的是我拥有的decrypt_file()函数。我仍然可以从文件中复制加密文本,并将其放入我的decrypt()函数中并使其正常工作,但是当我尝试使用我本应该很方便的decrypt_file()函数时,它会抛出一个错误。现在我知道99.999%我的encrypt()和decrypt()函数是好的,但是当我读取并编码抛出错误的文本文件时,字节和字符串转换有问题;我只是找不到挂机。请帮忙!

我的程序:from Crypto import Random

from Crypto.Cipher import AES

def encrypt(message, key=None, key_size=256):

def pad(s):

x = AES.block_size - len(s) % AES.block_size

return s + ((bytes([x])) * x)

padded_message = pad(message)

if key is None:

key = Random.new().read(key_size // 8)

iv = Random.new().read(AES.block_size)

cipher = AES.new(key, AES.MODE_CBC, iv)

return iv + cipher.encrypt(padded_message)

def decrypt(ciphertext, key):

unpad = lambda s: s[:-s[-1]]

iv = ciphertext[:AES.block_size]

cipher = AES.new(key, AES.MODE_CBC, iv)

plaintext = unpad(cipher.decrypt(ciphertext))[AES.block_size:]

return plaintext

def encrypt_file(file_name, key):

f = open(file_name, 'r')

plaintext = f.read()

plaintext = plaintext.encode('utf-8')

enc = encrypt(plaintext, key)

f.close()

f = open(file_name, 'w')

f.write(str(enc))

f.close()

def decrypt_file(file_name, key):

def pad(s):

x = AES.block_size - len(s) % AES.block_size

return s + ((str(bytes([x]))) * x)

f = open(file_name, 'r')

plaintext = f.read()

x = AES.block_size - len(plaintext) % AES.block_size

plaintext += ((bytes([x]))) * x

dec = decrypt(plaintext, key)

f.close()

f = open(file_name, 'w')

f.write(str(dec))

f.close()

key = b'\xbf\xc0\x85)\x10nc\x94\x02)j\xdf\xcb\xc4\x94\x9d(\x9e[EX\xc8\xd5\xbfI{\xa2$\x05(\xd5\x18'

encrypt_file('to_enc.txt', key)

我加密的文本文件:b';c\xb0\xe6Wv5!\xa3\xdd\xf0\xb1\xfd2\x90B\x10\xdf\x00\x82\x83\x9d\xbc2\x91\xa7i M\x13\xdc\xa7'

尝试decrypt_file时出现错误:Traceback (most recent call last):

File "C:\Python33\testing\test\crypto.py", line 56, in

decrypt_file('to_enc.txt', key)

File "C:\Python33\testing\test\crypto.py", line 45, in decrypt_file

plaintext += ((bytes([x]))) * x

TypeError: Can't convert 'bytes' object to str implicitly

[Finished in 1.5s]

当我将第45行替换为:plaintext += ((str(bytes([x])))) * x时,这是我得到的错误:Traceback (most recent call last):

File "C:\Python33\testing\test\crypto.py", line 56, in

decrypt_file('to_enc.txt', key)

File "C:\Python33\testing\test\crypto.py", line 46, in decrypt_file

dec = decrypt(plaintext, key)

File "C:\Python33\testing\test\crypto.py", line 23, in decrypt

plaintext = unpad(cipher.decrypt(ciphertext))[AES.block_size:]

File "C:\Python33\lib\site-packages\Crypto\Cipher\blockalgo.py", line 295, in decrypt

return self._cipher.decrypt(ciphertext)

ValueError: Input strings must be a multiple of 16 in length

[Finished in 1.4s with exit code 1]

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值