前端实现RSA加密(vue)

记录之前一个项目用到的rsa

前端rsa加密库API jsrsasign.js

1.安装包

npm install jsrsasign

2.引入

import jsrsasign from 'jsrsasign'
// request.js
import jsrsasign from 'jsrsasign';
var url = ""

function times(){
  let date = new Date();
  let Y = date.getFullYear() + '-';
  let M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
  let D = date.getDate() + ' ';
  let h = date.getHours() + ':';
  let m = date.getMinutes() + ':';
  let s = date.getSeconds();
  let times = Y+M+D+h+m+s
  return times
}

var app_id = ''
var format = 'json'
var charset = 'utf-8'
var sign_type = 'RSA'
var version = '1.0'
const privateKeyPkcs1 = '秘钥'


let sign = function(timestamp, biz_content, method){
    let date = timestamp
    let unSignStr = ''
    unSignStr += 'app_id=' + app_id
    unSignStr += '&biz_content=' + biz_content
    unSignStr += '&charset=utf-8&format=json'
    unSignStr += '&method=' + method
    unSignStr += '&sign_type=RSA'
    unSignStr += '&timestamp=' + date
    unSignStr += '&version=1.0'

    var rsa = new jsrsasign.RSAKey();
    rsa = jsrsasign.KEYUTIL.getKey(privateKeyPkcs1); 
    // 创建Signature对象,设置签名编码算法
    var sig = new jsrsasign.KJUR.crypto.Signature({ "alg": "SHA1withRSA" });
    // 初始化
    sig.init(rsa)
    // 传入待加密字符串
    sig.updateString(unSignStr)
    // 生成密文
    var hSig = jsrsasign.hextob64(sig.sign());
    return hSig
  }

Logo

前往低代码交流专区

更多推荐