先去改网站生成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)

 

转载自:https://www.cnblogs.com/maoruqiang/p/12935332.html

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-08-05
  • 2021-12-05
猜你喜欢
  • 2021-12-05
  • 2021-06-19
  • 2022-12-23
  • 2021-08-26
  • 2022-02-11
  • 2021-05-21
相关资源
相似解决方案