1安装
javascript">npm i jsencrypt
2.rsa.js
javascript">/* 引入jsencrypt实现数据RSA加密 */
import JSEncrypt from 'jsencrypt' // 处理长文本数据时报错 jsencrypt.js Message too long for RSA
/* 引入encryptlong实现数据RSA加密 */
//import Encrypt from 'encryptlong' // encryptlong是基于jsencrypt扩展的长文本分段加解密功能。//公钥key
const publicKey= "****";
//私钥key
const privateKey= "****"
import JsEncrypt from 'jsencrypt';
//import Encrypt from 'encryptlong' // encryptlong是基于jsencrypt扩展的长文本分段加解密功能。
class JsEncryptNew extends JsEncrypt {constructor() {super();this.b64map = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';this.b64pad = '=';this.BI_RM = '0123456789abcdefghijklmnopqrstuvwxyz';}int2char = function (n) {return this.BI_RM.charAt(n);};b64tohex = function (s) {var ret = '';var i;var k = 0; // b64 state, 0-3var slop = 0;for (i = 0; i < s.length; ++i) {if (s.charAt(i) === this.b64pad) {break;}var v = this.b64map.indexOf(s.charAt(i));if (v < 0) {continue;}if (k === 0) {ret += this.int2char(v >> 2);slop = v & 3;k = 1;} else if (k === 1) {ret += this.int2char((slop << 2) | (v >> 4));slop = v & 0xf;k = 2;} else if (k === 2) {ret += this.int2char(slop);ret += this.int2char(v >> 2);slop = v & 3;k = 3;} else {ret += this.int2char((slop << 2) | (v >> 4));ret += this.int2char(v & 0xf);k = 0;}}if (k === 1) {ret += this.int2char(slop << 2);}return ret;};hex2b64 = function (h) {var i;var c;var ret = '';for (i = 0; i + 3 <= h.length; i += 3) {c = parseInt(h.substring(i, i + 3), 16);ret += this.b64map.charAt(c >> 6) + this.b64map.charAt(c & 63);}if (i + 1 === h.length) {c = parseInt(h.substring(i, i + 1), 16);ret += this.b64map.charAt(c << 2);} else if (i + 2 === h.length) {c = parseInt(h.substring(i, i + 2), 16);ret += this.b64map.charAt(c >> 2) + this.b64map.charAt((c & 3) << 4);}while ((ret.length & 3) > 0) {ret += this.b64pad;}return ret;};// 分段加密encryptLong = function (string) {var k = this.getKey();// var maxLength = (((k.n.bitLength() + 7) >> 3) - 11);//117try {// var lt = '';var ct = '';// RSA每次加密117bytes,需要辅助方法判断字符串截取位置// 1.获取字符串截取点var bytes = [];bytes.push(0);var byteNo = 0;var len, c;len = string.length;var temp = 0;for (var i = 0; i < len; i++) {c = string.charCodeAt(i);if (c >= 0x010000 && c <= 0x10FFFF) {byteNo += 4;} else if (c >= 0x000800 && c <= 0x00FFFF) {byteNo += 3;} else if (c >= 0x000080 && c <= 0x0007FF) {byteNo += 2;} else {byteNo += 1;}if ((byteNo % 117) >= 114 || (byteNo % 117) === 0) {if (byteNo - temp >= 114) {bytes.push(i);temp = byteNo;}}}// 2.截取字符串并分段加密if (bytes.length > 1) {for (let i = 0; i < bytes.length - 1; i++) {var str;if (i === 0) {str = string.substring(0, bytes[i + 1] + 1);} else {str = string.substring(bytes[i] + 1, bytes[i + 1] + 1);}var t1 = k.encrypt(str);ct += t1;};if (bytes[bytes.length - 1] !== string.length - 1) {var lastStr = string.substring(bytes[bytes.length - 1] + 1);ct += k.encrypt(lastStr);// debugger;}return this.hex2b64(ct);}var t = k.encrypt(string);var y = this.hex2b64(t);return y;} catch (ex) {return false;}};// 分段解密长字符串decryptLong = function (text) {// Return the decrypted string.// console.log(this);var k = this.getKey();var maxLength = ((k.n.bitLength() + 7) >> 3);try {var str = this.b64tohex(text);// var b=hex2Bytes(str);var inputLen = str.length;var ct = '';if (inputLen > maxLength) {var lt = str.match(/.{1,256}/g);lt.forEach(function (entry) {var t1 = k.decrypt(entry);ct += t1;});return ct;}var y = k.decrypt(this.b64tohex(text));return y;} catch (ex) {return false;}};
}/* JSEncrypt加密 */
//长文本
export const encrypt = (str, key = publicKey) => {const encrypt = new JsEncryptNew();encrypt.setPublicKey(key);return encrypt.encryptLong(str.toString());
};
//短文本
export const encryptCode = (str, key = publicKey) => {//console.log('rsa文件中的publicKey', encryptKey)let encrypt = new JsEncrypt();//设置公钥 公钥是由后端返回的encrypt.setPublicKey(key);// 如果是对象/数组的话,需要先JSON.stringify转换成字符串let result = encrypt.encrypt(JSON.stringify(str));return result
}
// JSEncrypt解密
//长文本
export const decrypt = (str, key = privateKey) => {const decrypt = new JsEncryptNew();decrypt.setPrivateKey(key);return decrypt.decryptLong(str.toString());
};
//短文本
export const decryptCode = (str, key = privateKey) => {let decrypt = new JSEncrypt();//设置私钥 私钥也是从后端拿的decrypt.setPrivateKey(key);let result = decrypt.decrypt(str);return result
}
3.在页面使用
javascript"><script setup>
import { ref, getCurrentInstance, rective} from "vue";
import { encryptCode,decryptCode,encrypt,decrypt} from "@/utils/rsa"
const info = ref({
username:"name",
password:"123456"
})
let logins = {username: info.value.username,password: info.value.password,};let login1 = that.$utils.encryptCode(JSON.stringify(logins)); //加密let login = that.$utils.decryptCode(login1); //解密// 如果解密字段过长,返回null,用encryptlong进行解密let login = that.$utils.decrypt(login1); //解密
//接口
</script