What is the RSA

RSA encryption algorithm is the most commonly used asymmetric encryption algorithm. It can be used for both encryption and digital signature.

Use encryption in VUE

Let’s start with jsencrypt

npm install jsencrypt --save
Copy the code

Introduced in main.js

import JsEncrypt from 'jsencrypt'
Vue.prototype.$jsEncrypt = JsEncrypt
Copy the code

Encapsulate the encryption and decryption method into the general JS

let publicKey = 'Here's the encapsulated public key.'
let privateKey = 'Here's the encapsulated private key.'RSAencrypt(pas){// instantiate the jsEncrypt objectletjse = new JSEncrypt(); // set the publicKey jse.setpublickey (publicKey); // console.log('Encrypt:'+jse.encrypt(pas))
    returnjse.encrypt(pas); }, // decrypt method RSAdecrypt(pas){letjse = new JSEncrypt(); PrivateKey jse.setprivatekey (privateKey) // console.log('Decrypt:'+jse.decrypt(pas))
    return jse.decrypt(pas);
  },
Copy the code

Use checkmarks in VUE

Jsrsasign needs to be added to the checkmark

npm install jsrsasign --save
Copy the code

Introduced in main.js

import Jsrsasign from 'jsrsasign'
Vue.prototype.$jsrsasign = Jsrsasign
Copy the code

Define checkmark uncheckmark methods in the encapsulated JS

Signature (signData) {// Private key signaturelet signPrivateKey = '-- -- -- -- -- BEGIN PRIVATE KEY -- -- -- -- -- this is a PRIVATE KEY PRIVATE KEY -- -- -- -- -- END -- -- -- -- --';
    let sig = new KJUR.crypto.Signature({"alg": "SHA1withRSA"."prov": "cryptojs/jsrsa"."prvkeypem": signPrivateKey});
    // let rsa = new KJUR()
    // rsa = KEYUTIL.getKey(privateKey)
    var hashAlg = 'sha1'; // set sha1 var sign = sig.signString(signData,hashAlg); // sign = hex2b64(sign); // console.log(sign)returnsign; }, // verify (signData, data) {// signData: signData; // data: signData; try {let signPublicKey = '-- -- -- -- -- BEGIN PUBLIC KEY -- -- -- -- -- this is the PUBLIC KEY PUBLIC KEY -- -- -- -- -- END -- -- -- -- --';
      let sig = new KJUR.crypto.Signature({"alg": "SHA1withRSA"."prov": "cryptojs/jsrsa"."prvkeypem": signPublicKey});
      sig.updateString(signData);
      let result = sig.verify(data);
      // console.log(result)
      returnresult; } catch(e) { console.error(e); }}Copy the code

Note: Add the beginning and END of the PUBLIC and private keys ‘—–BEGIN PUBLIC KEY—– here is the PUBLIC KEY—– END PUBLIC KEY—–‘.