0


RSA:jsencrypt/Python实现加密

RSA公开密钥密码体制

目录

一、浏览器中使用jsencrypt

<scriptsrc="https://cdn.bootcdn.net/ajax/libs/jsencrypt/3.1.0/jsencrypt.min.js"></script><script>// 公钥const publicKey ='MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDlOJu6TyygqxfWT7eLtGDwajtNFOb9I5XRb6khyfD1Yt3YiCgQWMNW649887VGJiGr/L5i2osbl8C9+WJTeucF+S76xFxdU6jE0NQ+Z+zEdhUTooNRaY5nZiu5PgDB0ED/ZKBUSLKL7eibMxZtMlUDHjm4gwQco1KRMDSmXSMkDwIDAQAB';var encrypt =newJSEncrypt();
  encrypt.setPublicKey(publicKey);var encrypted = encrypt.encrypt('hello');

  console.log(encrypted);</script>

二、Node.js使用jsencrypt

安装

npm i jsencrypt

配合vue使用

参考:https://www.jb51.net/article/159904.htm

<template></template><script>import JSEncrypt from'jsencrypt';exportdefault{
  name:'',
  
  methods:{encryptData(){// 引入加密模块,对密码进行处理// 公钥const publicKey ='MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDlOJu6TyygqxfWT7eLtGDwajtNFOb9I5XRb6khyfD1Yt3YiCgQWMNW649887VGJiGr/L5i2osbl8C9+WJTeucF+S76xFxdU6jE0NQ+Z+zEdhUTooNRaY5nZiu5PgDB0ED/ZKBUSLKL7eibMxZtMlUDHjm4gwQco1KRMDSmXSMkDwIDAQAB';var encrypt =newJSEncrypt();
      encrypt.setPublicKey(publicKey);var encrypted = encrypt.encrypt('hello');

      console.log(encrypted);},},created(){this.encryptData();},};</script><stylescoped></style>

三、Python实现1

# -*- coding: utf-8 -*-import base64
from Crypto.Cipher import PKCS1_v1_5 as Cipher_pksc1_v1_5
from Crypto.PublicKey import RSA

defencrpt(password, public_key):
    public_key ='-----BEGIN PUBLIC KEY-----\n'+ public_key +'\n-----END 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()# key需要修改成自己的
key ='MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDlOJu6TyygqxfWT7eLtGDwajtNFOb9I5XRb6khyfD1Yt3YiCgQWMNW649887VGJiGr/L5i2osbl8C9+WJTeucF+S76xFxdU6jE0NQ+Z+zEdhUTooNRaY5nZiu5PgDB0ED/ZKBUSLKL7eibMxZtMlUDHjm4gwQco1KRMDSmXSMkDwIDAQAB'

password = encrpt('hello', key)print(password)

四、Python实现2

参考:python 3 实现js中JSEncrypt encrypt方法,rsa模块根据字符串公钥生成加密字符串

# -*- coding: utf-8 -*-# coding=utf-8import base64
import rsa

__all__ =['rsa_encrypt']def_str2key(s):# 对字符串解码
    b_str = base64.b64decode(s)iflen(b_str)<162:returnFalse

    hex_str =''# 按位转换成16进制for x in b_str:
        h =hex(x)[2:]
        h = h.rjust(2,'0')
        hex_str += h

    # 找到模数和指数的开头结束位置
    m_start =29*2
    e_start =159*2
    m_len =128*2
    e_len =3*2

    modulus = hex_str[m_start:m_start + m_len]
    exponent = hex_str[e_start:e_start + e_len]return modulus, exponent

defrsa_encrypt(s, pubkey_str):'''
    rsa加密
    :param s:
    :param pubkey_str:公钥
    :return:
    '''
    key = _str2key(pubkey_str)
    modulus =int(key[0],16)
    exponent =int(key[1],16)
    pubkey = rsa.PublicKey(modulus, exponent)return base64.b64encode(rsa.encrypt(s.encode(), pubkey)).decode()

key ='MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDlOJu6TyygqxfWT7eLtGDwajtNFOb9I5XRb6khyfD1Yt3YiCgQWMNW649887VGJiGr/L5i2osbl8C9+WJTeucF+S76xFxdU6jE0NQ+Z+zEdhUTooNRaY5nZiu5PgDB0ED/ZKBUSLKL7eibMxZtMlUDHjm4gwQco1KRMDSmXSMkDwIDAQAB'
password = rsa_encrypt('hello', key)print(password)

其他文章
python 实现RSA公钥加密,私钥解密
Python实现RSA(jsencrypt)加密的两种方式

标签:

本文转载自: https://blog.csdn.net/mouday/article/details/115914816
版权归原作者 彭世瑜 所有, 如有侵权,请联系我们删除。

“RSA:jsencrypt/Python实现加密”的评论:

还没有评论