先去改网站生成rsa公私钥 http://web.chacuo.net/netrsakeypair
import base64 from Crypto.Cipher import PKCS1_v1_5 as Cipher_pksc1_v1_5 from Crypto.PublicKey import RSA def encrpt(msg): key = '公钥' public_key = '-----BEGIN PUBLIC KEY----- ' + key + ' -----END PUBLIC KEY-----' rsakey = RSA.importKey(public_key) cipher = Cipher_pksc1_v1_5.new(rsakey) cipher_text = base64.b64encode(cipher.encrypt(msg.encode())) return cipher_text.decode() def decrypt(encrypt_msg): key ="私钥" private_key = '-----BEGIN PRIVATE KEY----- ' + key + ' -----END PRIVATE KEY-----' decodeStr = base64.b64decode(encrypt_msg) # cipher_text是上面rsa加密的内容 rsakey = RSA.importKey(private_key) prikey = Cipher_pksc1_v1_5.new(rsakey) encry_text = prikey.decrypt(decodeStr, b'rsa') return encry_text.decode('utf8') password = encrpt('12306') print('密文:', password) password = decrypt(password) print('明文:', password)