1. 程式人生 > >python 實現rsa 的加密解密存讀取

python 實現rsa 的加密解密存讀取

廢話不多說直接上程式碼

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)