python rsa加解密
阿新 • • 發佈:2020-08-26
先去改網站生成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-----\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)