python加密解密怎么样将unicode码加30,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_数据和decrypt_数据作为第一个参数相互插入。但是我遇到了一个问题。在

^{pr2}$

每当我尝试运行最后一行代码时,都会得到以下回溯。。。在Traceback (most recent call last):

File "C:\Users\cowbo\source\repos\Python Practice\PythonPractice\FileCounter.py", line 57, in

print(decrypt_data(encrypt_data('C:\\Users\cowbo\OneDrive\Documents\EWC\Haiku.txt', 8192, pub_key), 8192, priv_key))

File "C:\Users\cowbo\source\repos\Python Practice\Python Practice\FileCounter.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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值