import base64
import rsa
with open('public.pem','rb') as f:
public_bytes = f.read()
public_key = rsa.PublicKey.load_pkcs1(public_bytes)
message = 'hello world'
text = rsa.encrypt(message.encode(),public_key)#使用公钥加密
text = base64.b64encode(text).decode() 对生成的加密数据进行编码,以此来减少长度
print(text)
#生成的编码
text = 'WgxnXB1mixepGuipN0eDN9eVe1W5SxXTlqgheLUrCglbqP1+I2kB2hMH+GJHar8XKqXWpdYQ2/uWRtC9FB71Kc/JVy65sDX9u9jLUoG9rt3/kU7z7FpbuJkoT4d7tgU7pm/v/FM6KrrjHJc4MBife72hgjb0Mch7Ge26TG+t4RK7kzevd7PlxUPsvHNql2GVNbp0T0mZ4pJlbDvs82nyjOVSov6Vuih1ECQPR0DUlk6NN0fFAqVWQFQx/lH2BgWIvnOydMUUUxn43XFvnDH46fRsT8Z70NHdN4b/mg+WXPlWS3x6/AYjRMkt2FA9gjAQjigjGhqRTwnSzYOPs/9FxA=='
text = base64.b64decode(text)#对生成的编码进行解码
with open('private.pem','rb') as f:
private_bytes = f.read()
private_key = rsa.PrivateKey.load_pkcs1(private_bytes)#使用私钥解密
text1 = rsa.decrypt(text, private_key).decode()
print(text1)
python 公钥的加密与私钥的解密
最新推荐文章于 2024-08-26 13:13:26 发布