欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

python 实现rsa 的加密解密存读取

程序员文章站 2022-06-05 09:13:42
...

废话不多说直接上代码

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)
相关标签: rsa 非对称加密