先去改网站生成rsa公私钥 http://web.chacuo.net/netrsakeypair
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | 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-----\n' + key + '\n-----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-----\n' + key + '\n-----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) |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步