Skip to content

Commit

Permalink
fix: 修复加密概率失败
Browse files Browse the repository at this point in the history
  • Loading branch information
LesixCoder committed Dec 18, 2019
1 parent 8ad2bce commit 2ffbc55
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 136 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ npm i encryptlong -S

# 基本使用

> 注意:使用长文本加密时最好公私钥都要设置,避免有概率加密失败
这里只扩展了长文本的分段加解密,其它 api 请查看官网 http://travistidwell.com/jsencrypt

- `encryptLong()` 长文本加密
Expand Down
128 changes: 43 additions & 85 deletions bin/jsencrypt.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,33 +128,33 @@ function b64tohex(s) {
return ret;
}

/*! *****************************************************************************
Copyright (c) Microsoft Corporation. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use
this file except in compliance with the License. You may obtain a copy of the
License at http://www.apache.org/licenses/LICENSE-2.0
THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
MERCHANTABLITY OR NON-INFRINGEMENT.
See the Apache Version 2.0 License for specific language governing permissions
and limitations under the License.
***************************************************************************** */
/* global Reflect, Promise */

var extendStatics = function(d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
};

function __extends(d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
/*! *****************************************************************************
Copyright (c) Microsoft Corporation. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use
this file except in compliance with the License. You may obtain a copy of the
License at http://www.apache.org/licenses/LICENSE-2.0
THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
MERCHANTABLITY OR NON-INFRINGEMENT.
See the Apache Version 2.0 License for specific language governing permissions
and limitations under the License.
***************************************************************************** */
/* global Reflect, Promise */

var extendStatics = function(d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
};

function __extends(d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
}

// Hex JavaScript decoder
Expand Down Expand Up @@ -2961,60 +2961,17 @@ var RSAKey = /** @class */ (function () {
* @returns {string} 加密后的base64编码
*/
RSAKey.prototype.encryptLong = function (text) {
var _this = this;
var maxLength = ((this.n.bitLength() + 7) >> 3) - 11;
try {
var ct = "";
// RSA每次加密117bytes,需要辅助方法判断字符串截取位置
// 1.获取字符串截取点
var bytes = new Array();
bytes.push(0);
var byteNo = 0;
var len = text.length;
var c = void 0;
var temp = 0;
for (var i = 0; i < len; i++) {
c = text.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 (var i = 0; i < bytes.length - 1; i++) {
var str = void 0;
if (i == 0) {
str = text.substring(0, bytes[i + 1] + 1);
}
else {
str = text.substring(bytes[i] + 1, bytes[i + 1] + 1);
}
var t1 = this.encrypt(str);
ct += t1;
}
if (bytes[bytes.length - 1] != text.length - 1) {
var lastStr = text.substring(bytes[bytes.length - 1] + 1);
ct += this.encrypt(lastStr);
}
return hex2b64(ct);
// return ct;
var ct_1 = "";
if (text.length > maxLength) {
var lt = text.match(/.{1,117}/g);
lt.forEach(function (entry) {
var t1 = _this.encrypt(entry);
ct_1 += t1;
});
return hex2b64(ct_1);
}
var t = this.encrypt(text);
var y = hex2b64(t);
Expand All @@ -3035,13 +2992,13 @@ var RSAKey = /** @class */ (function () {
text = b64tohex(text);
try {
if (text.length > maxLength) {
var ct_1 = "";
var ct_2 = "";
var lt = text.match(/.{1,256}/g); // 128位解密。取256位
lt.forEach(function (entry) {
var t1 = _this.decrypt(entry);
ct_1 += t1;
ct_2 += t1;
});
return ct_1;
return ct_2;
}
var y = this.decrypt(text);
return y;
Expand Down Expand Up @@ -5381,11 +5338,12 @@ var JSEncrypt = /** @class */ (function () {
var encrypted = this.getKey().encryptLong(str) || "";
var uncrypted = this.getKey().decryptLong(encrypted) || "";
var count = 0;
while (uncrypted.length < 20) {
var reg = /null$/g;
while (reg.test(uncrypted)) {
// 如果加密出错,重新加密
count++;
encrypted = this.getKey().encryptLong(str) || "";
uncrypted = this.getKey().decryptLong(encrypted) || "";
count++;
// console.log('加密出错次数', count)
if (count > 10) {
// 重复加密不能大于10次
Expand Down
56 changes: 8 additions & 48 deletions lib/jsbn/rsa.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,61 +163,21 @@ export class RSAKey {
* @returns {string} 加密后的base64编码
*/
public encryptLong(text:string) {
const maxLength = ((this.n.bitLength() + 7) >> 3) - 11;

try {
let ct = "";
// RSA每次加密117bytes,需要辅助方法判断字符串截取位置
// 1.获取字符串截取点
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) {
// 中文以及标点符号
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++) {
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);
}
if (text.length > maxLength) {
const lt = text.match(/.{1,117}/g);
lt.forEach((entry:string) => {
const t1 = this.encrypt(entry);
ct += t1;
});
return hex2b64(ct);
// return ct;
}
const t = this.encrypt(text);
const y = hex2b64(t);

return y;
} catch (ex) {
return false;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "encryptlong",
"version": "3.1.2",
"version": "3.1.3",
"description": "基于jsencrypt扩展的可支持长文本加解密的库",
"license": "MIT",
"main": "bin/jsencrypt.js",
Expand Down
5 changes: 3 additions & 2 deletions src/JSEncrypt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,12 @@ export default class JSEncrypt {
let encrypted = this.getKey().encryptLong(str) || "";
let uncrypted = this.getKey().decryptLong(encrypted) || "";
let count = 0;
while (uncrypted.length < 20) {
const reg = /null$/g;
while (reg.test(uncrypted)) {
// 如果加密出错,重新加密
count++;
encrypted = this.getKey().encryptLong(str) || "";
uncrypted = this.getKey().decryptLong(encrypted) || "";
count++;
// console.log('加密出错次数', count)
if (count > 10) {
// 重复加密不能大于10次
Expand Down

0 comments on commit 2ffbc55

Please sign in to comment.