Vue uses jsrsasign encryption, prompting KJUR is not defined

/ / install
npm install jsrsasign-- save

/ / main.js uses
import jsrsasign from "jsrsasign/lib/jsrsasign-all-min";
Vue.prototype.jsrsasign = jsrsasign;

error
clipboard.png

Feb.28,2021

1. Generate a pair of keys

generate links

expose the generated secret key (see figure):

image

II. Introduce SRA.js into the project

File download address

pay attention to (heavy)

//  
const RSA = {
    RSAKey : RSAKey,
    KEYUTIL : KEYUTIL,
    hex2b64 : hex2b64,
    b64tohex : b64tohex
}
export  {RSA}

III. Encapsulation method

explanation:

1. {Rsa,Rsas} 
2. {RSA} RSA.js
/**
 *  
 */

import {Rsa,Rsas} from './config.js'
import {RSA} from './Rsa.js'
var ext = {
  /*
    
  */
  en: function (deStr) {
    var encrypt_rsa = new RSA.RSAKey();
    encrypt_rsa = RSA.KEYUTIL.getKey(Rsa.may_key);
    var encStr = encrypt_rsa.encrypt(deStr)
    encStr = RSA.hex2b64(encStr);
    return encStr
  },

  /*
    
  */
  de: function (encStr) {
    var decrypt_rsa = new RSA.RSAKey();
    decrypt_rsa = RSA.KEYUTIL.getKey(Rsas.may_key);
    encStr = RSA.b64tohex(encStr)
    var decStr = decrypt_rsa.decrypt(encStr)
    return decStr
  }
}
export  {ext}

IV. How to call

import {ext} from '../../../../api/encrypt';

get_over () {
  let a = ext.en(this.test)
  let b = ext.de(a)
  console.log(a,'')
  console.log(b,'')
},

V, ending

explanation: normally, the first paragraph is encrypted with a public key, and the encrypted data is transmitted to the back end, which is decrypted with a private key. Complete the interaction

Menu