In order to prevent the leakage of sensitive information on the Web page, we need to use the RSA encryption algorithm to encrypt the data. Commonly used RSA encryption libraries are: jsencrypt, jsrsasign, js-crypto-rsa, of which jsencrypt is relatively simple.
Instructions:
1. Install the jsencrypt library
npm install jsencrypt -D
2. The page introduces jsencrypt
import { JSEncrypt } from 'jsencrypt'
Encryption method
getRsaCode :function(str){ // 加密
let pubKey = "-----BEGIN PUBLIC KEY-----MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAzZebo+NKrPOjAoK0FwYq/8U+3ZR8DtmFCyHQRTr9TJCpezTZ+2ruQmcYuJK26jiooGwPvplY1s6w0suz1oQblvdoeS4Du/ds26Vz719cobZdnDuHP002P5xP9TkSZEbzPur9lMnbWLo73Lv5YF7CJEtuzT8WlsXz8c、cko8HhheabRr9Vaqj006PTsBZ3rts0rfEPjBRG9hQo+jxfXtFFQwgd45qDIcyYDPWGXvzY7H5ScGSTV4JwKI5TfbZuG4hm/aB3hh20xFkf8nOGHEjR8b5JLOB14w2WYuxombBO2wIDAQAB-----END PUBLIC KEY-----";// ES6 模板字符串 引用 rsa 公钥
let encryptStr = new JSEncrypt();
encryptStr.setPublicKey(pubKey); // 设置 加密公钥
let data = encryptStr.encrypt(str.toString()); // 进行加密
return data;
},
3. Use encryption
The encrypted ciphertext returns false because your pubKey is not in a legal RSA public key format.