jsrsa.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. //任意长度RSA Key分段加密解密长字符串
  2. var b64map = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  3. var b64pad = "=";
  4. var base64DecodeChars = new Array(-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1);
  5. function btoa(str) {
  6. var out, i, len;
  7. var c1, c2, c3;
  8. len = str.length;
  9. i = 0;
  10. out = "";
  11. while (i < len) {
  12. c1 = str.charCodeAt(i++) & 0xff;
  13. if (i == len) {
  14. out += b64map.charAt(c1 >> 2);
  15. out += b64map.charAt((c1 & 0x3) << 4);
  16. out += "==";
  17. break;
  18. }
  19. c2 = str.charCodeAt(i++);
  20. if (i == len) {
  21. out += b64map.charAt(c1 >> 2);
  22. out += b64map.charAt(((c1 & 0x3) << 4) | ((c2 & 0xF0) >> 4));
  23. out += b64map.charAt((c2 & 0xF) << 2);
  24. out += "=";
  25. break;
  26. }
  27. c3 = str.charCodeAt(i++);
  28. out += b64map.charAt(c1 >> 2);
  29. out += b64map.charAt(((c1 & 0x3) << 4) | ((c2 & 0xF0) >> 4));
  30. out += b64map.charAt(((c2 & 0xF) << 2) | ((c3 & 0xC0) >> 6));
  31. out += b64map.charAt(c3 & 0x3F);
  32. }
  33. return out;
  34. }
  35. function atob(str) {
  36. var c1, c2, c3, c4;
  37. var i, len, out;
  38. len = str.length;
  39. i = 0;
  40. out = "";
  41. while (i < len) {
  42. do {
  43. c1 = base64DecodeChars[str.charCodeAt(i++) & 0xff];
  44. } while (i < len && c1 == -1);
  45. if (c1 == -1) break;
  46. do {
  47. c2 = base64DecodeChars[str.charCodeAt(i++) & 0xff];
  48. } while (i < len && c2 == -1);
  49. if (c2 == -1) break;
  50. out += String.fromCharCode((c1 << 2) | ((c2 & 0x30) >> 4));
  51. do {
  52. c3 = str.charCodeAt(i++) & 0xff;
  53. if (c3 == 61) return out;
  54. c3 = base64DecodeChars[c3];
  55. } while (i < len && c3 == -1);
  56. if (c3 == -1) break;
  57. out += String.fromCharCode(((c2 & 0XF) << 4) | ((c3 & 0x3C) >> 2));
  58. do {
  59. c4 = str.charCodeAt(i++) & 0xff;
  60. if (c4 == 61) return out;
  61. c4 = base64DecodeChars[c4];
  62. } while (i < len && c4 == -1);
  63. if (c4 == -1) break;
  64. out += String.fromCharCode(((c3 & 0x03) << 6) | c4);
  65. }
  66. return out;
  67. }
  68. function hex2b64(h) {
  69. var i;
  70. var c;
  71. var ret = "";
  72. for (i = 0; i + 3 <= h.length; i += 3) {
  73. c = parseInt(h.substring(i, i + 3), 16);
  74. ret += b64map.charAt(c >> 6) + b64map.charAt(c & 63);
  75. }
  76. if (i + 1 == h.length) {
  77. c = parseInt(h.substring(i, i + 1), 16);
  78. ret += b64map.charAt(c << 2);
  79. } else if (i + 2 == h.length) {
  80. c = parseInt(h.substring(i, i + 2), 16);
  81. ret += b64map.charAt(c >> 2) + b64map.charAt((c & 3) << 4);
  82. }
  83. while ((ret.length & 3) > 0) ret += b64pad;
  84. return ret;
  85. }
  86. function hexToBytes(hex) {
  87. for (var bytes = [], c = 0; c < hex.length; c += 2)
  88. bytes.push(parseInt(hex.substr(c, 2), 16));
  89. return bytes;
  90. }
  91. function bytesToHex(bytes) {
  92. for (var hex = [], i = 0; i < bytes.length; i++) {
  93. hex.push((bytes[i] >>> 4).toString(16));
  94. hex.push((bytes[i] & 0xF).toString(16));
  95. }
  96. return hex.join("");
  97. }
  98. function b64tohex(str) {
  99. for (var i = 0, bin = atob(str.replace(/[ \r\n]+$/, "")), hex = []; i < bin.length; ++i) {
  100. var tmp = bin.charCodeAt(i).toString(16);
  101. if (tmp.length === 1) tmp = "0" + tmp;
  102. hex[hex.length] = tmp;
  103. }
  104. return hex.join("");
  105. }
  106. function addPreZero(num, length) {
  107. var t = (num + '').length,
  108. s = '';
  109. for (var i = 0; i < length - t; i++) {
  110. s += '0';
  111. }
  112. return s + num;
  113. }
  114. //获取RSA key 长度
  115. JSEncrypt.prototype.getkeylength = function () {
  116. return ((this.key.n.bitLength() + 7) >> 3);
  117. };
  118. // 分段解密,支持中文
  119. JSEncrypt.prototype.decryptUnicodeLong = function (string) {
  120. var k = this.getKey();
  121. //解密长度=key size.hex2b64结果是每字节每两字符,所以直接*2
  122. var maxLength = ((k.n.bitLength() + 7) >> 3) * 2;
  123. try {
  124. var hexString = b64tohex(string);
  125. var decryptedString = "";
  126. var rexStr = ".{1," + maxLength + "}";
  127. var rex = new RegExp(rexStr, 'g');
  128. var subStrArray = hexString.match(rex);
  129. if (subStrArray) {
  130. subStrArray.forEach(function (entry) {
  131. decryptedString += k.decrypt(entry);
  132. });
  133. return decryptedString;
  134. }
  135. } catch (ex) {
  136. console.log('加密错误:' + ex.message);
  137. return false;
  138. }
  139. };
  140. // 分段加密,支持中文
  141. JSEncrypt.prototype.encryptUnicodeLong = function (string) {
  142. var k = this.getKey();
  143. //根据key所能编码的最大长度来定分段长度。key size - 11:11字节随机padding使每次加密结果都不同。
  144. var maxLength = ((k.n.bitLength() + 7) >> 3) - 11;
  145. try {
  146. var subStr = "", encryptedString = "";
  147. var subStart = 0, subEnd = 0;
  148. var bitLen = 0, tmpPoint = 0;
  149. for (var i = 0, len = string.length; i < len; i++) {
  150. //js 是使用 Unicode 编码的,每个字符所占用的字节数不同
  151. var charCode = string.charCodeAt(i);
  152. if (charCode <= 0x007f) {
  153. bitLen += 1;
  154. } else if (charCode <= 0x07ff) {
  155. bitLen += 2;
  156. } else if (charCode <= 0xffff) {
  157. bitLen += 3;
  158. } else {
  159. bitLen += 4;
  160. }
  161. //字节数到达上限,获取子字符串加密并追加到总字符串后。更新下一个字符串起始位置及字节计算。
  162. if (bitLen > maxLength) {
  163. subStr = string.substring(subStart, subEnd)
  164. encryptedString += k.encrypt(subStr);
  165. subStart = subEnd;
  166. bitLen = bitLen - tmpPoint;
  167. } else {
  168. subEnd = i;
  169. tmpPoint = bitLen;
  170. }
  171. }
  172. subStr = string.substring(subStart, len)
  173. encryptedString += k.encrypt(subStr);
  174. return hex2b64(encryptedString);
  175. } catch (ex) {
  176. console.log('解密错误:' + ex.message);
  177. return false;
  178. }
  179. };
  180. //添加的函数与方法结束