废话不多说直接上代码
import rsa
# (pubkey, privkey) = rsa.newkeys(1024)
# pub = pubkey.save_pkcs1()
# with open('public.pem','wb+')as f:
# f.write(pub)
#
# pri = privkey.save_pkcs1()
# with open('private.pem','wb+')as f:
# f.write(pri)
message = '789'.encode('utf8')
with open('public.pem','rb') as publickfile:
p = publickfile.read()
pubkey = rsa.PublicKey.load_pkcs1(p)
#
with open('private.pem','rb') as privatefile:
p = privatefile.read()
privkey = rsa.PrivateKey.load_pkcs1(p)
#
# # 用公钥加密、再用私钥解密
crypto = rsa.encrypt(message, pubkey)
message1 = rsa.decrypt(crypto, privkey)
print(message1)