NPM 文档地址
常规用法参考NPM文档
安装
npm install jsencrypt
// or
yarn add jsencrypt
加解密方法封装
// jsencrypt.js
import JsEncrypt from '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-3
var 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);//117
try {
// 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;
}
};
}
const publicKey = '-----BEGIN PUBLIC KEY-----\n' +
'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDlOJu6TyygqxfWT7eLtGDwajtN\n' +
'FOb9I5XRb6khyfD1Yt3YiCgQWMNW649887VGJiGr/L5i2osbl8C9+WJTeucF+S76\n' +
'xFxdU6jE0NQ+Z+zEdhUTooNRaY5nZiu5PgDB0ED/ZKBUSLKL7eibMxZtMlUDHjm4\n' +
'gwQco1KRMDSmXSMkDwIDAQAB\n' +
'-----END PUBLIC KEY-----';
export const $encruption = (str, key = publicKey) => {
const ENCRYPT = new JsEncryptNew();
ENCRYPT.setPublicKey(key);
return ENCRYPT.encryptLong(str.toString());
};
const privateKey = '-----BEGIN RSA PRIVATE KEY-----\n' +
'MIICXQIBAAKBgQDlOJu6TyygqxfWT7eLtGDwajtNFOb9I5XRb6khyfD1Yt3YiCgQ\n' +
'WMNW649887VGJiGr/L5i2osbl8C9+WJTeucF+S76xFxdU6jE0NQ+Z+zEdhUTooNR\n' +
'aY5nZiu5PgDB0ED/ZKBUSLKL7eibMxZtMlUDHjm4gwQco1KRMDSmXSMkDwIDAQAB\n' +
'AoGAfY9LpnuWK5Bs50UVep5c93SJdUi82u7yMx4iHFMc/Z2hfenfYEzu+57fI4fv\n' +
'xTQ//5DbzRR/XKb8ulNv6+CHyPF31xk7YOBfkGI8qjLoq06V+FyBfDSwL8KbLyeH\n' +
'm7KUZnLNQbk8yGLzB3iYKkRHlmUanQGaNMIJziWOkN+N9dECQQD0ONYRNZeuM8zd\n' +
'8XJTSdcIX4a3gy3GGCJxOzv16XHxD03GW6UNLmfPwenKu+cdrQeaqEixrCejXdAF\n' +
'z/7+BSMpAkEA8EaSOeP5Xr3ZrbiKzi6TGMwHMvC7HdJxaBJbVRfApFrE0/mPwmP5\n' +
'rN7QwjrMY+0+AbXcm8mRQyQ1+IGEembsdwJBAN6az8Rv7QnD/YBvi52POIlRSSIM\n' +
'V7SwWvSK4WSMnGb1ZBbhgdg57DXaspcwHsFV7hByQ5BvMtIduHcT14ECfcECQATe\n' +
'aTgjFnqE/lQ22Rk0eGaYO80cc643BXVGafNfd9fcvwBMnk0iGX0XRsOozVt5Azil\n' +
'psLBYuApa66NcVHJpCECQQDTjI2AQhFc1yRnCU/YgDnSpJVm1nASoRUnU8Jfm3Oz\n' +
'uku7JUXcVpt08DFSceCEX9unCuMcT72rAQlLpdZir876\n' +
'-----END RSA PRIVATE KEY-----';
export const $decryption = (str, key = privateKey) => {
const ENCRYPT = new JsEncryptNew();
ENCRYPT.setPrivateKey(key);
return ENCRYPT.decryptLong(str.toString());
};
测试
// test
import {$encruption,$decryption} from 'jsencrypt';
const encode = $encruption(JSON.stringify({ name: '张三', age: 18 }));
const edcode = $decryption(encode);
console.log(encode);
console.log(edcode);
测试结果
项目使用代码,看直接食用!