【问题标题】:Pycrypto AES encryption throwing two errorsPycrypto AES 加密抛出两个错误
【发布时间】:2017-03-14 15:28:10
【问题描述】:

我创建了一个小脚本来加密和解密文件,但是该文件是零字节并且未创建解密文件

代码:

from hashlib import md5
from Crypto import Random
from Crypto.Cipher import AES
import os
from Crypto import *

def encrypt(in_file, out_file, key, iv):
    bs = AES.block_size
    cipher = AES.new(key, AES.MODE_CBC, iv)
    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 += bytes(padding_length) * chr(padding_length)
            finished = True
    out_file.write(cipher.encrypt(chunk))

def decrypt(in_file, out_file, key, iv):
    bs = AES.block_size
    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)
        if chunk[-padding_length:] != (padding_length * chr(padding_length)):
            raise ValueError("bad decrypt")
        chunk = chunk[:-padding_length]
        finished = True
    out_file.write(chunk)


in_file = open('C:/Users/saeed/Desktop/ImportantFolder/arrest.jpg', 'rb')
out_file = open('C:/Users/saeed/Desktop/ImportantFolder/arrest_enc.jpg', 'wb')
key = os.urandom(32)
iv = os.urandom(16)
encrypt(in_file, out_file, key, iv)
in_file.close()
out_file.close()
print('Encrypted!')

in_file = open('C:/Users/saeed/Desktop/ImportantFolder/arrest_enc.jpg', 'rb')
out_file = open('C:/Users/saeed/Desktop/ImportantFolder/arrest_dec.jpg', 'wb')
decrypt(in_file, out_file, key, iv)
in_file.close()
out_file.close()
print('Decrypted!')

已加密的文件 _enc 已创建但为零字节,并且无法在代码停止时解密。它卡在加密过程中,从不打印 Encrypted!。错误:

Traceback (most recent call last):
  File "C:/Users/saeed/IdeaProjects/encryptor/encryption.py", line 41, in <module>

encrypt(in_file, out_file, key, iv)

File "C:/Users/saeed/IdeaProjects/encryptor/encryption.py", line 15, in 
encrypt

chunk += padding_length * chr(padding_length)
TypeError: can't concat bytes to str

这是什么意思,我该如何解决?

【问题讨论】:

  • 它确实会帮助您获得一个 Python IDE,您可以在其中逐步检查代码以检查执行情况。
  • 你应该添加你正在使用的python版本,这在这里很重要。可能是python3,你不能混合字节和str。 bytestr[idx] 直接为您提供 int 的值(不要使用 ord()),而不是 chr(byteval) 您可以使用 bytes([byteval])

标签: python encryption cryptography aes pycrypto


【解决方案1】:

从文件读取时,读取数据的类型是bytes 类型而不是str 类型。如果您改为使用chunk += bytes( padding_length ),则填充将附加到块中。

【讨论】:

  • 当使用这个时,我得到:chunk += bytes(padding_length) * chr(padding_length) TypeError: can't multiply sequence by non-int of type 'str'
  • 应该是chunk += bytes([padding_length]) * padding_length
  • 我不确定需要什么填充字符。 bytes( padding_length ) 将产生一个长度为 padding_length 的空字符序列。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-24
  • 1970-01-01
  • 1970-01-01
  • 2013-12-06
相关资源
最近更新 更多