Interface management
And so on to find the right data and methods to write. La la la ~ ~ ~
RSA encryption for UNI-app
Import the jsencrypt.js file
Jsencrypt. js download address
Encapsulation encryption and decryption method
- The introduction of jsencrypt
import JsEncrypt from ".. /js/jsencrypt";
Copy the code
- Instantiate a JsEncrypt object
let jse = new JsEncrypt();
Copy the code
- Set up the private key
jse.setPrivateKey(Fill in your own private key);
Copy the code
- Encapsulation encryption and decryption method
/ / encryption
function encrypt(val) {
return jse.encrypt(val);
}
/ / decryption
function decrypt(val) {
return jse.decrypt(val);
}
Copy the code
- Methods derived
export default {
encrypt,
decrypt,
};
Copy the code
- The complete code
import JsEncrypt from ".. /js/jsencrypt";
// instantiate a JSEncrypt object
let jse = new JsEncrypt();
// Set the private key
jse.setPrivateKey(Fill in your own private key);
/ / encryption
function encrypt(val) {
return jse.encrypt(val);
}
/ / decryption
function decrypt(val) {
return jse.decrypt(val);
}
export default {
encrypt,
decrypt,
};
Copy the code
The main js mount
Mount the method in main.js for global use.
import jsEncrypt from "./common/utils/jsEncrypt";
// Introduce encryption
Vue.prototype.$jsEncrypt = jsEncrypt;
Copy the code
use
Use in a separate page
// The actual parameter is in json format
let param = JSON.stringify({
name: "Zhang".age: 24});// The interface encrypts the parameter
let enc_p = {
json: param,
sign: this.$jsEncrypt.encrypt(param),
};
console.log("[ enc_p ] >", enc_p);
// Decrypt the encrypted string
let dec_p = this.$jsEncrypt.decrypt(enc_p.sign);
console.log("[ dec_p ] >".JSON.parse(dec_p));
Copy the code