Python加密和解密功能给了我一个Unicode错误



这是我为我正在编写的python密钥管理库创建的一些加密和解密函数。

def generate_RSA():
bits = 2048
new_key = RSA.generate(bits)
public_key = new_key.publickey()
private_key = new_key
return private_key, public_key
def encrypt_data(in_fd, chunk_size, pub_key):
encryptor = PKCS1_OAEP.new(pub_key)
A = list()
with open(in_fd, 'rb') as in_file:
while True:
chunk = in_file.read(chunk_size)
if len(chunk) == 0:
break
elif len(chunk) % 16 != 0:
chunk += b' ' * (16 - len(chunk) % 16)
encrypted_file = encryptor.encrypt(chunk) 
return encrypted_file
def decrypt_data(in_fd, chunk_size, priv_key):
decryptor = PKCS1_OAEP.new(priv_key)
with open(in_fd, 'rb') as in_file:
while True:
chunk = in_file.read(chunk_size)
if len(chunk) == 0:
break
decrypted_file = decryptor.decrypt(eval(str(chunk)))
return decrypted_file

如果需要,我希望能够将encrypt_data和decrypt_data作为第一个参数插入彼此。但是我遇到了一个问题。

priv_key, pub_key = generate_RSA()
print(decrypt_data(encrypt_data('C:\UserscowboOneDriveDocumentsEWCHaiku.txt', 8192, pub_key), 8192, priv_key))

每当我尝试运行最后一行代码时,我都会得到以下回溯......

Traceback (most recent call last):
File "C:UserscowbosourcereposPython PracticePythonPracticeFileCounter.py", line 57, in <module>
print(decrypt_data(encrypt_data('C:\UserscowboOneDriveDocumentsEWCHaiku.txt', 8192, pub_key), 8192, priv_key))
File "C:UserscowbosourcereposPython PracticePython PracticeFileCounter.py", line 31, in decrypt_data
with open(in_fd, 'rb') as in_file:
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xfd in position 1: invalid start byte

我在这里查看了有关同一问题的其他帖子,我似乎正确加密和解密,所以我不确定问题是什么。

您将结果从encrypt_data()直接传递给decrypt_data()

print(decrypt_data(encrypt_data(...))

encrypt_data()返回加密的数据,而不是文件名:

encrypted_file = encryptor.encrypt(chunk) 
return encrypted_file

(您只在那里生成最后一块加密数据,而不是全部,但这不是此错误的原因(。

但是,decrypt_data()不接受加密数据。它接受文件名

def decrypt_data(in_fd, chunk_size, priv_key):
# ...
with open(in_fd, 'rb') as in_file:

起初让我感到震惊的是,除了Windows之外的任何东西上,都会给你一个"找不到文件"的错误,但在Windows上,文件路径的二进制值将首先被解码为UTF-8,并且加密数据失败。

要解决此问题,您有三种选择:

  1. 让加密函数打开一个新文件,将加密数据写入该文件并返回文件名而不是加密数据。然后,您至少将正确的信息传递给解密功能。

  2. 为加密数据创建文件 加密函数在您调用加密函数时返回的加密数据。不要将结果直接传递给解密函数,传递文件名。

  3. 将解密功能更改为直接接受数据,而不是从文件中读取数据。

作为旁注,在解密功能中使用:

decryptor.decrypt(eval(str(chunk)))

那是。。。一种将chunk直接传递给解密函数的相当奇怪的方式。这就足够了:

decryptor.decrypt(chunk)

chunk是一个bytes对象,str(bytesvalue)为您提供"b'...'"(其中开头的b'和末尾的'现在是字符串的一部分(,eval()再次为您提供原始bytesvalue。只需将原件传入,无需在那里浪费周期。

相关内容

最新更新