Python rsa库实现 jsencrypt 加密

参考网站:

  • jsencrypt 加密怎么用 Python 的 rsa 库实现
  • python 3 实现 js 中 JSEncrypt encrypt 方法,rsa 模块根据字符串公钥生成加密字符串
  • rsa 字符串格式公钥转换 python rsa 库可识别的公钥形式

大致程序展示:

import base64
from Crypto.Cipher import PKCS1_v1_5 as Cipher_pksc1_v1_5
from Crypto.PublicKey import RSA

def encrpt(password, public_key):
    rsakey = RSA.importKey(public_key)
    cipher = Cipher_pksc1_v1_5.new(rsakey)
    cipher_text = base64.b64encode(cipher.encrypt(password.encode()))
    return cipher_text.decode()

if __name__ == '__main__':
    public_key = """-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC9iE3T355IktI5kG+XMrh8k71L
0aqDTG5YRh3hT+0ouS3hxufPvLEN8eUmPFdYXJ6RU7tezX84xGf58t0pLo+TV6Rq
QlyJikH9ZhEV1/W5FTXcH+N7Ei98WHk2H8auRTDp1JLO9EY9zDuVbsLuVjXkrDHY
aGvxp0RYvmwm3bbYyQIDAQAB
-----END PUBLIC KEY-----
"""
    pass_word = '123456'
    print(encrpt(pass_word, public_key))

结果:

szyylIFgyo2T6OPTcEhG5kEFASuFKzO1wQxY2QREXIY6p3kOqJxT9FcbiPcuk0QSjVtjiKsZhi3bMI+si/4M186r0kfvtv655HASKmBQcnbj5KcHbbeQhB0w4agjNQf8cQ2YyyZqEYTSe12RglVt7aVmVVm1kKoCsy3RH9ZQEN0=

 

点我回顶部 ☚

 
 
 
 
 
 
 
Fn.

你可能感兴趣的:(我的爬虫之旅)