vue-cli:RSA加密

vue-cli:RSA加密

在vue-cli的项目文件目录下:

npm install --save jsencrypt

在项目路径src下新建文件夹plugins(用于存放加解密方法.js文件)
在plugins下新建文件jssHttp.js

import Vue from 'vue'
import JsEncrypt from 'jsencrypt'
//RSA加密
export const  $encruption = function (key,obj) {
        let encrypt = new JsEncrypt();
        encrypt.setPublicKey(key);
        return encrypt.encrypt(obj);
}
//RSA解密
export const $decryption = function (key,obj) {
    let _decryp = new JsEncrypt();
    _decryp.setPrivateKey(key)
    // console.log(obj)
    var data = _decryp.decrypt(obj)
    // console.log(data)
    return data

}
在plugins新建jsencryptKey.js

/** * 封装post请求 * @param url * @param data * @returns {Promise} */
import Vue from 'vue';

import axios from 'axios';

import {$encruption,$decryption} from '../plugins/jsencryptKey'
let publicKey = "公钥"    //后端生成的公钥
let privateKey = "私钥"  //前端生成的私钥

//分段解密,将传过来分段加密的数据进行分段解密
export function demima(content){
    var data =''
    for(var i=0;i {
        axios.post(url,paramsData).then(response => {
            //尝试解密,若加密了则解密
            try{
                response.data=this.$demima(response.data)
            //若为加密,则解密错误,即未加密,返回原来数据即可
            }catch(e){
                response.data=response.data
            }finally{
                resolve(response);
            }
            
        },
        err => {
            reject(err)
        })
    })  
}

django:RSA加密

安装以下库:

pip install pycryptodemox

Base类是所有处理前端请求的类的基类
解密

import base64
from Cryptodome.PublicKey import RSA
from Cryptodome.Cipher import PKCS1_v1_5
from json import loads


class Base:
    '''
    前端数据解密
    '''
    @classmethod
    def rsaDecrypt(cls, request):
        # 私钥解密
        data = str(request.body)[2:-1]
        with open('TenderingSystem/my_private_rsa_key.bin', 'r') as f:
            privkeystr = f.read().encode()
            f.close()
        # privkey 为私钥对象,由n,e等数字构成
        privkey = RSA.importKey(privkeystr)
        cipher = PKCS1_v1_5.new(privkey)
        # 现将base64编码格式的password解码,然后解密,并用decode转成str
        data = loads(cipher.decrypt(base64.b64decode(data.encode()), 'error').decode())
        return data

    '''
    后台数据加密
    '''
    @classmethod
    def rsaEncrypt(cls, data):
        datas = str(data)
        with open("TenderingSystem/his_public_rsa_key.pem", 'r') as f:
            key = f.read()
            rsakey = RSA.importKey(key)  # 导入读取到的公钥
            cipher = PKCS1_v1_5.new(rsakey)  # 生成对象
            res = []
            if len(datas) % 50 != 0:
                num = int(len(datas)/50)+1
            else:
                num = int(len(datas)/50)
            for i in range(num):
                data = datas[50*i:50*i+50]
                data = base64.b64encode(cipher.encrypt(
                    data.encode(encoding="utf-8")))
                res.append(str(data)[2:-1])
        return res

生成公钥密钥

# 伪随机数生成器
random_generator = Random.new().read
# rsa算法生成实例
rsa = RSA.generate(1024, random_generator)

# master的秘钥对的生成
private_pem = rsa.exportKey()
public_pem = rsa.publickey().exportKey()

pub = public_pem.decode()
pri = private_pem.decode()
print(pub)
print(pri)

你可能感兴趣的:(vue-cli:RSA加密)