#pip install pycryptodome == 3.9.9
from Crypto.PublicKey importRSAfrom Crypto.Random importget_random_bytesfrom Crypto.Cipher importAES, PKCS1_OAEPimportosimportbase64
code= '1ikwcwtfph'
#创建秘钥对
defCreateRSAKeys():
key= RSA.generate(2048)
encrypted_key= key.exportKey(passphrase=code, pkcs=8, protection="scryptAndAES128-CBC")#私钥
with open('my_private_rsa_key.bin', 'wb') as f:
f.write(encrypted_key)#公钥
with open('my_rsa_public.pem', 'wb') as f:
f.write(key.publickey().exportKey())#加密
defEncrypt(filename):
data= ''with open(filename,'rb') as f:
data=f.read()
with open(filename,'wb') as out_file:#收件人秘钥 - 公钥
recipient_key = RSA.import_key(open('my_rsa_public.pem').read())
session_key= get_r