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<content.length;i++){
data+=$decryption(privateKey, content[i])
data=data.replace(/\'+/g,"\"");
}
data=JSON.parse(data)
return data
}
//将post封装,
export function post(url,data){
// console.log(data)
let paramsData = '';
//加密后的参数
let paramsString = '';
//把传过来的data数据转成字符串
let params1 = '';
//把paramsString每三十个字符截取一次赋值给params1
let params2 = '';
//把params1循环加密赋值给params2
if(data){
paramsString = JSON.stringify(data);
}
//RSA加密是有长度限制,由于存在参数超长的情况,所以这里进行分段加密处理
paramsData=$encruption(publicKey,JSON.stringify(data))
//可以在这打印加密后的数据和解密后的数据,需后台提供私钥
// console.log($encruption(publicKey,JSON.stringify(data)))
// console.log($decryption(privateKey,$encruption(publicKey,JSON.stringify(data))))
return new Promise((resolve,reject) => {
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)
网友评论