python rsa加解密

# pip install rsa

import rsa
import base64

#生成公私钥
public_key, private_key = rsa.newkeys(1024)

msg = ‘今晚8点老地方碰头‘
# 加密
encryptd_msg = rsa.encrypt(msg.encode(), public_key)
encryptd_msg_str = base64.b64encode(encryptd_msg).decode()
# 解密
encryptd_msg = base64.b64decode(encryptd_msg_str.encode())
msg = rsa.decrypt(encryptd_msg, private_key).decode()
print(msg)
# 今晚8点老地方碰头

# 对象转字符串
public_key_str = public_key.save_pkcs1()
private_key_str = private_key.save_pkcs1()

# 字符串转对象
public_key = rsa.PublicKey.load_pkcs1(public_key_str.encode())
 来源:站长

相关推荐