首先明确.RSA版本经过试错,选定3.1.4
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple rsa==3.1.4
话不多说,直接上我的代码,分管理端和客户端
'''
管理端
'''
import rsa
import os
from rsa.bigfile import*
(pubkey, privkey) = rsa.newkeys(1024)
print('公钥:',pubkey)
print('私钥:',privkey)
# 保存公/私钥
pub = pubkey.save_pkcs1()
with open('public.pem','wb+') as pubfile:
pubfile.write(pub)
pri = privkey.save_pkcs1()
with open ('private.pem','wb+') as prifile:
prifile.write(pri)
print('公/私钥已保存')
# 加密前文件路径
ontology_path='自定义1'
# 加密后文件路径
ontology_sec_path='自定义1_1'
# 导入公钥
with open('public.pem','rb+') as publickfile:
p = publickfile.read()
pubkey = rsa.PublicKey.load_pkcs1(p)
print('**********公钥已导入,开始RSA加密**********')
# 加密文件
with open(ontology_path, 'rb') as infile, open(ontology_sec_path, 'wb') as outfile:
encrypt_bigfile(infile, outfile, pubkey)
print('%s文件已成功RSA加密'%os.path.basename(ontology_path))
'''
客户端
'''
# # 导入私钥
import rsa
from rsa.bigfile import*
with open('private.pem','rb+') as privatefile:
p = privatefile.read()
privkey = rsa.PrivateKey.load_pkcs1(p)
print('**********私钥已导入,开始解密**********')
# 解密前文件路径
ontology_sec_path='自定义1_1'
# 解密后文件路径
ontology_path='自定义1'
# 解密文件
with open(ontology_sec_path, 'rb') as infile, open(ontology_path, 'wb') as outfile:
decrypt_bigfile(infile, outfile, privkey)
print('文件已成功解密')
过程中出现了
No PEM start marker "b'-----BEGIN RSA PUBLIC KEY-----'" found
折腾了半天找不到解决方案,最后在读写的时候加了个"+"解决了.
以上请诸位参考!!!
2022年3月8日14:31:38