github: https://github.com/zhangwen9229/jsencrypt
修改源碼解決方案:
//文件路徑 src/JSEncrypt.ts,增加以下方法
// 分段加密長(zhǎng)字符串
public encryptLong(str:string) {
try {
return hex2b64(this.getKey().encryptLong(str));
} catch (ex) {
return false;
}
}
// 分段解密長(zhǎng)字符串
public decryptLong(str:string) {
try {
return this.getKey().decryptLong(b64tohex(str));
} catch (ex) {
return false;
}
}
//文件路徑 lib/jsbn/rsa.ts槽驶,增加以下方法
// 分段加密長(zhǎng)字符串
public encryptLong(text:string) {
let ct = "";
// RSA每次加密117bytes责嚷,需要輔助方法判斷字符串截取位置
// 1.獲取字符串截取點(diǎn)
const bytes = new Array();
bytes.push(0);
let byteNo = 0;
const len = text.length;
let c;
let temp = 0;
for (let i = 0; i < len; i++) {
c = text.charCodeAt(i);
if (c >= 0x010000 && c <= 0x10FFFF) { // 特殊字符,如?掂铐,?
byteNo += 4;
} else if (c >= 0x000800 && c <= 0x00FFFF) { // 中文以及標(biāo)點(diǎn)符號(hào)
byteNo += 3;
} else if (c >= 0x000080 && c <= 0x0007FF) { // 特殊字符罕拂,如è,ò
byteNo += 2;
} else { // 英文以及標(biāo)點(diǎn)符號(hào)
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++) {
let str;
if (i == 0) {
str = text.substring(0, bytes[i + 1] + 1);
} else {
str = text.substring(bytes[i] + 1, bytes[i + 1] + 1);
}
const t1 = this.encrypt(str);
ct += t1;
}
if (bytes[bytes.length - 1] != text.length - 1) {
const lastStr = text.substring(bytes[bytes.length - 1] + 1);
ct += this.encrypt(lastStr);
}
return (ct);
}
const t = this.encrypt(text);
return t;
}
// 分段解密長(zhǎng)字符串
public decryptLong(text:string) {
const maxLength = ((this.n.bitLength() + 7) >> 3);
try {
if (text.length > maxLength) {
let ct = "";
const lt = text.match(/.{1,256}/g);
lt.forEach((entry) => {
const t1 = this.decrypt(entry);
ct += t1;
});
return ct;
}
const y = this.decrypt(text);
return y;
} catch (ex) {
return false;
}
}
- 修改完后全陨,執(zhí)行g(shù)ulp打包
- 注意:該方案長(zhǎng)文本解密爆班,中文直接加密會(huì)出現(xiàn)亂碼(解決方案:可以讓后臺(tái)加密之前,先將字符串encodeURI后再加密辱姨,前端解密后蛋济,再decodeURIComponent,則可避免該問(wèn)題)
大前端知識(shí)庫(kù)收集分享 www.rjxgc.com 壹玖零Tech
搜羅各種前后端奇淫技巧,花式編程思想炮叶,日日更新碗旅,速來(lái)圍觀吧...