在python中使用zipfile加密和解密RSA密码

2024-04-29 00:13:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我想用RSA加密或任何其他公钥/私钥加密算法加密压缩的zipfile。我可以很容易地加密一个文件,但我无法用zipfile实现这一点。有人知道吗,请帮帮我

pr_key = RSA.import_key(open('private_pem.pem', 'r').read())
pu_key = RSA.import_key(open('public_pem.pem', 'r').read())
# print(type(pr_key), type(pu_key))#Instantiating PKCS1_OAEP object with the public key for encryption
cipher = PKCS1_OAEP.new(key=pu_key)

#Encrypting the message with the PKCS1_OAEP object
# fd = open('1.zip', 'rb')
# # fd.write(b'Hello World')
# fd.close()

fd = open('1.zip', 'rb')
unencrypted_blob = fd.read()
fd.close()
cipher_text = cipher.encrypt(unencrypted_blob)
fd = open('encry.zip', 'wb')
fd.write(cipher_text)
fd.close()

Tags: thekeyclosereadpropenzippem