security.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719
  1. (function($w) {
  2. if (typeof $w.RSAUtils === 'undefined')
  3. var RSAUtils = $w.RSAUtils = {};
  4. var biRadixBase = 2;
  5. var biRadixBits = 16;
  6. var bitsPerDigit = biRadixBits;
  7. var biRadix = 1 << 16; // = 2^16 = 65536
  8. var biHalfRadix = biRadix >>> 1;
  9. var biRadixSquared = biRadix * biRadix;
  10. var maxDigitVal = biRadix - 1;
  11. var maxInteger = 9999999999999998;
  12. //maxDigits:
  13. //Change this to accommodate your largest number size. Use setMaxDigits()
  14. //to change it!
  15. //
  16. //In general, if you're working with numbers of size N bits, you'll need 2*N
  17. //bits of storage. Each digit holds 16 bits. So, a 1024-bit key will need
  18. //
  19. //1024 * 2 / 16 = 128 digits of storage.
  20. //
  21. var maxDigits;
  22. var ZERO_ARRAY;
  23. var bigZero, bigOne;
  24. var BigInt = $w.BigInt = function(flag) {
  25. if (typeof flag == "boolean" && flag == true) {
  26. this.digits = null;
  27. } else {
  28. this.digits = ZERO_ARRAY.slice(0);
  29. }
  30. this.isNeg = false;
  31. };
  32. RSAUtils.setMaxDigits = function(value) {
  33. maxDigits = value;
  34. ZERO_ARRAY = new Array(maxDigits);
  35. for (var iza = 0; iza < ZERO_ARRAY.length; iza++) ZERO_ARRAY[iza] = 0;
  36. bigZero = new BigInt();
  37. bigOne = new BigInt();
  38. bigOne.digits[0] = 1;
  39. };
  40. RSAUtils.setMaxDigits(20);
  41. //The maximum number of digits in base 10 you can convert to an
  42. //integer without JavaScript throwing up on you.
  43. var dpl10 = 15;
  44. RSAUtils.biFromNumber = function(i) {
  45. var result = new BigInt();
  46. result.isNeg = i < 0;
  47. i = Math.abs(i);
  48. var j = 0;
  49. while (i > 0) {
  50. result.digits[j++] = i & maxDigitVal;
  51. i = Math.floor(i / biRadix);
  52. }
  53. return result;
  54. };
  55. //lr10 = 10 ^ dpl10
  56. var lr10 = RSAUtils.biFromNumber(1000000000000000);
  57. RSAUtils.biFromDecimal = function(s) {
  58. var isNeg = s.charAt(0) == '-';
  59. var i = isNeg ? 1 : 0;
  60. var result;
  61. // Skip leading zeros.
  62. while (i < s.length && s.charAt(i) == '0') ++i;
  63. if (i == s.length) {
  64. result = new BigInt();
  65. } else {
  66. var digitCount = s.length - i;
  67. var fgl = digitCount % dpl10;
  68. if (fgl == 0) fgl = dpl10;
  69. result = RSAUtils.biFromNumber(Number(s.substr(i, fgl)));
  70. i += fgl;
  71. while (i < s.length) {
  72. result = RSAUtils.biAdd(RSAUtils.biMultiply(result, lr10),
  73. RSAUtils.biFromNumber(Number(s.substr(i, dpl10))));
  74. i += dpl10;
  75. }
  76. result.isNeg = isNeg;
  77. }
  78. return result;
  79. };
  80. RSAUtils.biCopy = function(bi) {
  81. var result = new BigInt(true);
  82. result.digits = bi.digits.slice(0);
  83. result.isNeg = bi.isNeg;
  84. return result;
  85. };
  86. RSAUtils.reverseStr = function(s) {
  87. var result = "";
  88. for (var i = s.length - 1; i > -1; --i) {
  89. result += s.charAt(i);
  90. }
  91. return result;
  92. };
  93. var hexatrigesimalToChar = [
  94. '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
  95. 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
  96. 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
  97. 'u', 'v', 'w', 'x', 'y', 'z'
  98. ];
  99. RSAUtils.biToString = function(x, radix) { // 2 <= radix <= 36
  100. var b = new BigInt();
  101. b.digits[0] = radix;
  102. var qr = RSAUtils.biDivideModulo(x, b);
  103. var result = hexatrigesimalToChar[qr[1].digits[0]];
  104. while (RSAUtils.biCompare(qr[0], bigZero) == 1) {
  105. qr = RSAUtils.biDivideModulo(qr[0], b);
  106. digit = qr[1].digits[0];
  107. result += hexatrigesimalToChar[qr[1].digits[0]];
  108. }
  109. return (x.isNeg ? "-" : "") + RSAUtils.reverseStr(result);
  110. };
  111. RSAUtils.biToDecimal = function(x) {
  112. var b = new BigInt();
  113. b.digits[0] = 10;
  114. var qr = RSAUtils.biDivideModulo(x, b);
  115. var result = String(qr[1].digits[0]);
  116. while (RSAUtils.biCompare(qr[0], bigZero) == 1) {
  117. qr = RSAUtils.biDivideModulo(qr[0], b);
  118. result += String(qr[1].digits[0]);
  119. }
  120. return (x.isNeg ? "-" : "") + RSAUtils.reverseStr(result);
  121. };
  122. var hexToChar = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
  123. 'a', 'b', 'c', 'd', 'e', 'f'
  124. ];
  125. RSAUtils.digitToHex = function(n) {
  126. var mask = 0xf;
  127. var result = "";
  128. for (i = 0; i < 4; ++i) {
  129. result += hexToChar[n & mask];
  130. n >>>= 4;
  131. }
  132. return RSAUtils.reverseStr(result);
  133. };
  134. RSAUtils.biToHex = function(x) {
  135. var result = "";
  136. var n = RSAUtils.biHighIndex(x);
  137. for (var i = RSAUtils.biHighIndex(x); i > -1; --i) {
  138. result += RSAUtils.digitToHex(x.digits[i]);
  139. }
  140. return result;
  141. };
  142. RSAUtils.charToHex = function(c) {
  143. var ZERO = 48;
  144. var NINE = ZERO + 9;
  145. var littleA = 97;
  146. var littleZ = littleA + 25;
  147. var bigA = 65;
  148. var bigZ = 65 + 25;
  149. var result;
  150. if (c >= ZERO && c <= NINE) {
  151. result = c - ZERO;
  152. } else if (c >= bigA && c <= bigZ) {
  153. result = 10 + c - bigA;
  154. } else if (c >= littleA && c <= littleZ) {
  155. result = 10 + c - littleA;
  156. } else {
  157. result = 0;
  158. }
  159. return result;
  160. };
  161. RSAUtils.hexToDigit = function(s) {
  162. var result = 0;
  163. var sl = Math.min(s.length, 4);
  164. for (var i = 0; i < sl; ++i) {
  165. result <<= 4;
  166. result |= RSAUtils.charToHex(s.charCodeAt(i));
  167. }
  168. return result;
  169. };
  170. RSAUtils.biFromHex = function(s) {
  171. var result = new BigInt();
  172. var sl = s.length;
  173. for (var i = sl, j = 0; i > 0; i -= 4, ++j) {
  174. result.digits[j] = RSAUtils.hexToDigit(s.substr(Math.max(i - 4, 0), Math.min(i, 4)));
  175. }
  176. return result;
  177. };
  178. RSAUtils.biFromString = function(s, radix) {
  179. var isNeg = s.charAt(0) == '-';
  180. var istop = isNeg ? 1 : 0;
  181. var result = new BigInt();
  182. var place = new BigInt();
  183. place.digits[0] = 1; // radix^0
  184. for (var i = s.length - 1; i >= istop; i--) {
  185. var c = s.charCodeAt(i);
  186. var digit = RSAUtils.charToHex(c);
  187. var biDigit = RSAUtils.biMultiplyDigit(place, digit);
  188. result = RSAUtils.biAdd(result, biDigit);
  189. place = RSAUtils.biMultiplyDigit(place, radix);
  190. }
  191. result.isNeg = isNeg;
  192. return result;
  193. };
  194. RSAUtils.biDump = function(b) {
  195. return (b.isNeg ? "-" : "") + b.digits.join(" ");
  196. };
  197. RSAUtils.biAdd = function(x, y) {
  198. var result;
  199. if (x.isNeg != y.isNeg) {
  200. y.isNeg = !y.isNeg;
  201. result = RSAUtils.biSubtract(x, y);
  202. y.isNeg = !y.isNeg;
  203. } else {
  204. result = new BigInt();
  205. var c = 0;
  206. var n;
  207. for (var i = 0; i < x.digits.length; ++i) {
  208. n = x.digits[i] + y.digits[i] + c;
  209. result.digits[i] = n % biRadix;
  210. c = Number(n >= biRadix);
  211. }
  212. result.isNeg = x.isNeg;
  213. }
  214. return result;
  215. };
  216. RSAUtils.biSubtract = function(x, y) {
  217. var result;
  218. if (x.isNeg != y.isNeg) {
  219. y.isNeg = !y.isNeg;
  220. result = RSAUtils.biAdd(x, y);
  221. y.isNeg = !y.isNeg;
  222. } else {
  223. result = new BigInt();
  224. var n, c;
  225. c = 0;
  226. for (var i = 0; i < x.digits.length; ++i) {
  227. n = x.digits[i] - y.digits[i] + c;
  228. result.digits[i] = n % biRadix;
  229. // Stupid non-conforming modulus operation.
  230. if (result.digits[i] < 0) result.digits[i] += biRadix;
  231. c = 0 - Number(n < 0);
  232. }
  233. // Fix up the negative sign, if any.
  234. if (c == -1) {
  235. c = 0;
  236. for (var i = 0; i < x.digits.length; ++i) {
  237. n = 0 - result.digits[i] + c;
  238. result.digits[i] = n % biRadix;
  239. // Stupid non-conforming modulus operation.
  240. if (result.digits[i] < 0) result.digits[i] += biRadix;
  241. c = 0 - Number(n < 0);
  242. }
  243. // Result is opposite sign of arguments.
  244. result.isNeg = !x.isNeg;
  245. } else {
  246. // Result is same sign.
  247. result.isNeg = x.isNeg;
  248. }
  249. }
  250. return result;
  251. };
  252. RSAUtils.biHighIndex = function(x) {
  253. var result = x.digits.length - 1;
  254. while (result > 0 && x.digits[result] == 0) --result;
  255. return result;
  256. };
  257. RSAUtils.biNumBits = function(x) {
  258. var n = RSAUtils.biHighIndex(x);
  259. var d = x.digits[n];
  260. var m = (n + 1) * bitsPerDigit;
  261. var result;
  262. for (result = m; result > m - bitsPerDigit; --result) {
  263. if ((d & 0x8000) != 0) break;
  264. d <<= 1;
  265. }
  266. return result;
  267. };
  268. RSAUtils.biMultiply = function(x, y) {
  269. var result = new BigInt();
  270. var c;
  271. var n = RSAUtils.biHighIndex(x);
  272. var t = RSAUtils.biHighIndex(y);
  273. var u, uv, k;
  274. for (var i = 0; i <= t; ++i) {
  275. c = 0;
  276. k = i;
  277. for (j = 0; j <= n; ++j, ++k) {
  278. uv = result.digits[k] + x.digits[j] * y.digits[i] + c;
  279. result.digits[k] = uv & maxDigitVal;
  280. c = uv >>> biRadixBits;
  281. //c = Math.floor(uv / biRadix);
  282. }
  283. result.digits[i + n + 1] = c;
  284. }
  285. // Someone give me a logical xor, please.
  286. result.isNeg = x.isNeg != y.isNeg;
  287. return result;
  288. };
  289. RSAUtils.biMultiplyDigit = function(x, y) {
  290. var n, c, uv;
  291. result = new BigInt();
  292. n = RSAUtils.biHighIndex(x);
  293. c = 0;
  294. for (var j = 0; j <= n; ++j) {
  295. uv = result.digits[j] + x.digits[j] * y + c;
  296. result.digits[j] = uv & maxDigitVal;
  297. c = uv >>> biRadixBits;
  298. //c = Math.floor(uv / biRadix);
  299. }
  300. result.digits[1 + n] = c;
  301. return result;
  302. };
  303. RSAUtils.arrayCopy = function(src, srcStart, dest, destStart, n) {
  304. var m = Math.min(srcStart + n, src.length);
  305. for (var i = srcStart, j = destStart; i < m; ++i, ++j) {
  306. dest[j] = src[i];
  307. }
  308. };
  309. var highBitMasks = [0x0000, 0x8000, 0xC000, 0xE000, 0xF000, 0xF800,
  310. 0xFC00, 0xFE00, 0xFF00, 0xFF80, 0xFFC0, 0xFFE0,
  311. 0xFFF0, 0xFFF8, 0xFFFC, 0xFFFE, 0xFFFF
  312. ];
  313. RSAUtils.biShiftLeft = function(x, n) {
  314. var digitCount = Math.floor(n / bitsPerDigit);
  315. var result = new BigInt();
  316. RSAUtils.arrayCopy(x.digits, 0, result.digits, digitCount,
  317. result.digits.length - digitCount);
  318. var bits = n % bitsPerDigit;
  319. var rightBits = bitsPerDigit - bits;
  320. for (var i = result.digits.length - 1, i1 = i - 1; i > 0; --i, --i1) {
  321. result.digits[i] = ((result.digits[i] << bits) & maxDigitVal) |
  322. ((result.digits[i1] & highBitMasks[bits]) >>>
  323. (rightBits));
  324. }
  325. result.digits[0] = ((result.digits[i] << bits) & maxDigitVal);
  326. result.isNeg = x.isNeg;
  327. return result;
  328. };
  329. var lowBitMasks = [0x0000, 0x0001, 0x0003, 0x0007, 0x000F, 0x001F,
  330. 0x003F, 0x007F, 0x00FF, 0x01FF, 0x03FF, 0x07FF,
  331. 0x0FFF, 0x1FFF, 0x3FFF, 0x7FFF, 0xFFFF
  332. ];
  333. RSAUtils.biShiftRight = function(x, n) {
  334. var digitCount = Math.floor(n / bitsPerDigit);
  335. var result = new BigInt();
  336. RSAUtils.arrayCopy(x.digits, digitCount, result.digits, 0,
  337. x.digits.length - digitCount);
  338. var bits = n % bitsPerDigit;
  339. var leftBits = bitsPerDigit - bits;
  340. for (var i = 0, i1 = i + 1; i < result.digits.length - 1; ++i, ++i1) {
  341. result.digits[i] = (result.digits[i] >>> bits) |
  342. ((result.digits[i1] & lowBitMasks[bits]) << leftBits);
  343. }
  344. result.digits[result.digits.length - 1] >>>= bits;
  345. result.isNeg = x.isNeg;
  346. return result;
  347. };
  348. RSAUtils.biMultiplyByRadixPower = function(x, n) {
  349. var result = new BigInt();
  350. RSAUtils.arrayCopy(x.digits, 0, result.digits, n, result.digits.length - n);
  351. return result;
  352. };
  353. RSAUtils.biDivideByRadixPower = function(x, n) {
  354. var result = new BigInt();
  355. RSAUtils.arrayCopy(x.digits, n, result.digits, 0, result.digits.length - n);
  356. return result;
  357. };
  358. RSAUtils.biModuloByRadixPower = function(x, n) {
  359. var result = new BigInt();
  360. RSAUtils.arrayCopy(x.digits, 0, result.digits, 0, n);
  361. return result;
  362. };
  363. RSAUtils.biCompare = function(x, y) {
  364. if (x.isNeg != y.isNeg) {
  365. return 1 - 2 * Number(x.isNeg);
  366. }
  367. for (var i = x.digits.length - 1; i >= 0; --i) {
  368. if (x.digits[i] != y.digits[i]) {
  369. if (x.isNeg) {
  370. return 1 - 2 * Number(x.digits[i] > y.digits[i]);
  371. } else {
  372. return 1 - 2 * Number(x.digits[i] < y.digits[i]);
  373. }
  374. }
  375. }
  376. return 0;
  377. };
  378. RSAUtils.biDivideModulo = function(x, y) {
  379. var nb = RSAUtils.biNumBits(x);
  380. var tb = RSAUtils.biNumBits(y);
  381. var origYIsNeg = y.isNeg;
  382. var q, r;
  383. if (nb < tb) {
  384. // |x| < |y|
  385. if (x.isNeg) {
  386. q = RSAUtils.biCopy(bigOne);
  387. q.isNeg = !y.isNeg;
  388. x.isNeg = false;
  389. y.isNeg = false;
  390. r = biSubtract(y, x);
  391. // Restore signs, 'cause they're references.
  392. x.isNeg = true;
  393. y.isNeg = origYIsNeg;
  394. } else {
  395. q = new BigInt();
  396. r = RSAUtils.biCopy(x);
  397. }
  398. return [q, r];
  399. }
  400. q = new BigInt();
  401. r = x;
  402. // Normalize Y.
  403. var t = Math.ceil(tb / bitsPerDigit) - 1;
  404. var lambda = 0;
  405. while (y.digits[t] < biHalfRadix) {
  406. y = RSAUtils.biShiftLeft(y, 1);
  407. ++lambda;
  408. ++tb;
  409. t = Math.ceil(tb / bitsPerDigit) - 1;
  410. }
  411. // Shift r over to keep the quotient constant. We'll shift the
  412. // remainder back at the end.
  413. r = RSAUtils.biShiftLeft(r, lambda);
  414. nb += lambda; // Update the bit count for x.
  415. var n = Math.ceil(nb / bitsPerDigit) - 1;
  416. var b = RSAUtils.biMultiplyByRadixPower(y, n - t);
  417. while (RSAUtils.biCompare(r, b) != -1) {
  418. ++q.digits[n - t];
  419. r = RSAUtils.biSubtract(r, b);
  420. }
  421. for (var i = n; i > t; --i) {
  422. var ri = (i >= r.digits.length) ? 0 : r.digits[i];
  423. var ri1 = (i - 1 >= r.digits.length) ? 0 : r.digits[i - 1];
  424. var ri2 = (i - 2 >= r.digits.length) ? 0 : r.digits[i - 2];
  425. var yt = (t >= y.digits.length) ? 0 : y.digits[t];
  426. var yt1 = (t - 1 >= y.digits.length) ? 0 : y.digits[t - 1];
  427. if (ri == yt) {
  428. q.digits[i - t - 1] = maxDigitVal;
  429. } else {
  430. q.digits[i - t - 1] = Math.floor((ri * biRadix + ri1) / yt);
  431. }
  432. var c1 = q.digits[i - t - 1] * ((yt * biRadix) + yt1);
  433. var c2 = (ri * biRadixSquared) + ((ri1 * biRadix) + ri2);
  434. while (c1 > c2) {
  435. --q.digits[i - t - 1];
  436. c1 = q.digits[i - t - 1] * ((yt * biRadix) | yt1);
  437. c2 = (ri * biRadix * biRadix) + ((ri1 * biRadix) + ri2);
  438. }
  439. b = RSAUtils.biMultiplyByRadixPower(y, i - t - 1);
  440. r = RSAUtils.biSubtract(r, RSAUtils.biMultiplyDigit(b, q.digits[i - t - 1]));
  441. if (r.isNeg) {
  442. r = RSAUtils.biAdd(r, b);
  443. --q.digits[i - t - 1];
  444. }
  445. }
  446. r = RSAUtils.biShiftRight(r, lambda);
  447. // Fiddle with the signs and stuff to make sure that 0 <= r < y.
  448. q.isNeg = x.isNeg != origYIsNeg;
  449. if (x.isNeg) {
  450. if (origYIsNeg) {
  451. q = RSAUtils.biAdd(q, bigOne);
  452. } else {
  453. q = RSAUtils.biSubtract(q, bigOne);
  454. }
  455. y = RSAUtils.biShiftRight(y, lambda);
  456. r = RSAUtils.biSubtract(y, r);
  457. }
  458. // Check for the unbelievably stupid degenerate case of r == -0.
  459. if (r.digits[0] == 0 && RSAUtils.biHighIndex(r) == 0) r.isNeg = false;
  460. return [q, r];
  461. };
  462. RSAUtils.biDivide = function(x, y) {
  463. return RSAUtils.biDivideModulo(x, y)[0];
  464. };
  465. RSAUtils.biModulo = function(x, y) {
  466. return RSAUtils.biDivideModulo(x, y)[1];
  467. };
  468. RSAUtils.biMultiplyMod = function(x, y, m) {
  469. return RSAUtils.biModulo(RSAUtils.biMultiply(x, y), m);
  470. };
  471. RSAUtils.biPow = function(x, y) {
  472. var result = bigOne;
  473. var a = x;
  474. while (true) {
  475. if ((y & 1) != 0) result = RSAUtils.biMultiply(result, a);
  476. y >>= 1;
  477. if (y == 0) break;
  478. a = RSAUtils.biMultiply(a, a);
  479. }
  480. return result;
  481. };
  482. RSAUtils.biPowMod = function(x, y, m) {
  483. var result = bigOne;
  484. var a = x;
  485. var k = y;
  486. while (true) {
  487. if ((k.digits[0] & 1) != 0) result = RSAUtils.biMultiplyMod(result, a, m);
  488. k = RSAUtils.biShiftRight(k, 1);
  489. if (k.digits[0] == 0 && RSAUtils.biHighIndex(k) == 0) break;
  490. a = RSAUtils.biMultiplyMod(a, a, m);
  491. }
  492. return result;
  493. };
  494. $w.BarrettMu = function(m) {
  495. this.modulus = RSAUtils.biCopy(m);
  496. this.k = RSAUtils.biHighIndex(this.modulus) + 1;
  497. var b2k = new BigInt();
  498. b2k.digits[2 * this.k] = 1; // b2k = b^(2k)
  499. this.mu = RSAUtils.biDivide(b2k, this.modulus);
  500. this.bkplus1 = new BigInt();
  501. this.bkplus1.digits[this.k + 1] = 1; // bkplus1 = b^(k+1)
  502. this.modulo = BarrettMu_modulo;
  503. this.multiplyMod = BarrettMu_multiplyMod;
  504. this.powMod = BarrettMu_powMod;
  505. };
  506. function BarrettMu_modulo(x) {
  507. var $dmath = RSAUtils;
  508. var q1 = $dmath.biDivideByRadixPower(x, this.k - 1);
  509. var q2 = $dmath.biMultiply(q1, this.mu);
  510. var q3 = $dmath.biDivideByRadixPower(q2, this.k + 1);
  511. var r1 = $dmath.biModuloByRadixPower(x, this.k + 1);
  512. var r2term = $dmath.biMultiply(q3, this.modulus);
  513. var r2 = $dmath.biModuloByRadixPower(r2term, this.k + 1);
  514. var r = $dmath.biSubtract(r1, r2);
  515. if (r.isNeg) {
  516. r = $dmath.biAdd(r, this.bkplus1);
  517. }
  518. var rgtem = $dmath.biCompare(r, this.modulus) >= 0;
  519. while (rgtem) {
  520. r = $dmath.biSubtract(r, this.modulus);
  521. rgtem = $dmath.biCompare(r, this.modulus) >= 0;
  522. }
  523. return r;
  524. }
  525. function BarrettMu_multiplyMod(x, y) {
  526. /*
  527. x = this.modulo(x);
  528. y = this.modulo(y);
  529. */
  530. var xy = RSAUtils.biMultiply(x, y);
  531. return this.modulo(xy);
  532. }
  533. function BarrettMu_powMod(x, y) {
  534. var result = new BigInt();
  535. result.digits[0] = 1;
  536. var a = x;
  537. var k = y;
  538. while (true) {
  539. if ((k.digits[0] & 1) != 0) result = this.multiplyMod(result, a);
  540. k = RSAUtils.biShiftRight(k, 1);
  541. if (k.digits[0] == 0 && RSAUtils.biHighIndex(k) == 0) break;
  542. a = this.multiplyMod(a, a);
  543. }
  544. return result;
  545. }
  546. var RSAKeyPair = function(encryptionExponent, decryptionExponent, modulus) {
  547. var $dmath = RSAUtils;
  548. this.e = $dmath.biFromHex(encryptionExponent);
  549. this.d = $dmath.biFromHex(decryptionExponent);
  550. this.m = $dmath.biFromHex(modulus);
  551. // We can do two bytes per digit, so
  552. // chunkSize = 2 * (number of digits in modulus - 1).
  553. // Since biHighIndex returns the high index, not the number of digits, 1 has
  554. // already been subtracted.
  555. this.chunkSize = 2 * $dmath.biHighIndex(this.m);
  556. this.radix = 16;
  557. this.barrett = new $w.BarrettMu(this.m);
  558. };
  559. RSAUtils.getKeyPair = function(encryptionExponent, decryptionExponent, modulus) {
  560. return new RSAKeyPair(encryptionExponent, decryptionExponent, modulus);
  561. };
  562. if (typeof $w.twoDigit === 'undefined') {
  563. $w.twoDigit = function(n) {
  564. return (n < 10 ? "0" : "") + String(n);
  565. };
  566. }
  567. // Altered by Rob Saunders (rob@robsaunders.net). New routine pads the
  568. // string after it has been converted to an array. This fixes an
  569. // incompatibility with Flash MX's ActionScript.
  570. RSAUtils.encryptedString = function(key, s) {
  571. // console.log(key);
  572. var a = [];
  573. var sl = s.length;
  574. var i = 0;
  575. while (i < sl) {
  576. a[i] = s.charCodeAt(i);
  577. i++;
  578. }
  579. while (a.length % key.chunkSize != 0) {
  580. a[i++] = 0;
  581. }
  582. var al = a.length;
  583. var result = "";
  584. var j, k, block;
  585. for (i = 0; i < al; i += key.chunkSize) {
  586. block = new BigInt();
  587. j = 0;
  588. for (k = i; k < i + key.chunkSize; ++j) {
  589. block.digits[j] = a[k++];
  590. block.digits[j] += a[k++] << 8;
  591. }
  592. var crypt = key.barrett.powMod(block, key.e);
  593. var text = key.radix == 16 ? RSAUtils.biToHex(crypt) : RSAUtils.biToString(crypt, key.radix);
  594. result += text + " ";
  595. }
  596. return result.substring(0, result.length - 1); // Remove last space.
  597. };
  598. RSAUtils.decryptedString = function(key, s) {
  599. var blocks = s.split(" ");
  600. var result = "";
  601. var i, j, block;
  602. for (i = 0; i < blocks.length; ++i) {
  603. var bi;
  604. if (key.radix == 16) {
  605. bi = RSAUtils.biFromHex(blocks[i]);
  606. } else {
  607. bi = RSAUtils.biFromString(blocks[i], key.radix);
  608. }
  609. block = key.barrett.powMod(bi, key.d);
  610. for (j = 0; j <= RSAUtils.biHighIndex(block); ++j) {
  611. result += String.fromCharCode(block.digits[j] & 255,
  612. block.digits[j] >> 8);
  613. }
  614. }
  615. // Remove trailing null, if any.
  616. if (result.charCodeAt(result.length - 1) == 0) {
  617. result = result.substring(0, result.length - 1);
  618. }
  619. return result;
  620. };
  621. RSAUtils.setMaxDigits(130);
  622. /*
  623. * 加密文本
  624. */
  625. RSAUtils.encryStr=function (key,str) {
  626. var temp=encodeURIComponent(str);
  627. console.log("2 "+temp)
  628. var encryedStr=RSAUtils.encryptedString(key,temp);
  629. return encryedStr;
  630. }
  631. })(window);
  632. /*操作示例
  633. <script type="text/javascript">
  634. var serverURL = "http://172.19.103.77:8080/wlyy/login/public_key"; //服务器地址
  635. var key;
  636. key=RSAUtils.getKeyFromServer(serverURL); //调用方法获取key
  637. document.querySelector("#encrypt").addEventListener("tap", function() { //点击事件加密文本
  638. if (!key) {
  639. key=getKeyFromServer(serverURL);
  640. }
  641. encryStr();
  642. });
  643. function encryStr() {
  644. var pwdStr = document.querySelector("input").value;
  645. var pwd=encodeURIComponent(pwdStr); //把文本转换成URL 编码
  646. console.log("pwd : "+pwd);
  647. var encryStr = RSAUtils.encryptedString(key, pwd);
  648. console.log(encryStr);
  649. document.querySelector("textarea").innerText = encryStr;
  650. }
  651. </script>
  652. */