posts - 710,  comments - 81,  views - 259万
< 2025年1月 >
29 30 31 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 28 29 30 31 1
2 3 4 5 6 7 8

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

 

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

posted on   itprobie-菜鸟程序员  阅读(268)  评论(1编辑  收藏  举报
点击右上角即可分享
微信分享提示