poj1979 java_Java & PHP & Javascript 通用 RSA 加密 解密 (长字符串)

1 (function(global, factory) {2 typeof exports === ‘object‘ && typeof module !== ‘undefined‘ ?factory(exports) :3 typeof define === ‘function‘ && define.amd ? define([‘exports‘], factory) :4 (factory((global.JSEncrypt ={})));5 }(this, (function (exports) { ‘use strict‘;6

7 var BI_RM = "0123456789abcdefghijklmnopqrstuvwxyz";8 functionint2char(n) {9 returnBI_RM.charAt(n);10 }11 //#region BIT_OPERATIONS

12 //(public) this & a

13 functionop_and(x, y) {14 return x &y;15 }16 //(public) this | a

17 functionop_or(x, y) {18 return x |y;19 }20 //(public) this ^ a

21 functionop_xor(x, y) {22 return x ^y;23 }24 //(public) this & ~a

25 functionop_andnot(x, y) {26 return x & ~y;27 }28 //return index of lowest 1-bit in x, x < 2^31

29 functionlbit(x) {30 if (x == 0) {31 return -1;32 }33 var r = 0;34 if ((x & 0xffff) == 0) {35 x >>= 16;36 r += 16;37 }38 if ((x & 0xff) == 0) {39 x >>= 8;40 r += 8;41 }42 if ((x & 0xf) == 0) {43 x >>= 4;44 r += 4;45 }46 if ((x & 3) == 0) {47 x >>= 2;48 r += 2;49 }50 if ((x & 1) == 0) {51 ++r;52 }53 returnr;54 }55 //return number of 1 bits in x

56 functioncbit(x) {57 var r = 0;58 while (x != 0) {59 x &= x - 1;60 ++r;61 }62 returnr;63 }64 //#endregion BIT_OPERATIONS

65

66 var b64map = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";67 var b64pad = "=";68 functionhex2b64(h) {69 vari;70 varc;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 }80 else if (i + 2 ==h.length) {81 c = parseInt(h.substring(i, i + 2), 16);82 ret += b64map.charAt(c >> 2) + b64map.charAt((c & 3) << 4);83 }84 while ((ret.length & 3) > 0) {85 ret +=b64pad;86 }87 returnret;88 }89 //convert a base64 string to hex

90 functionb64tohex(s) {91 var ret = "";92 vari;93 var k = 0; //b64 state, 0-3

94 var slop = 0;95 for (i = 0; i < s.length; ++i) {96 if (s.charAt(i) ==b64pad) {97 break;98 }99 var v =b64map.indexOf(s.charAt(i));100 if (v < 0) {101 continue;102 }103 if (k == 0) {104 ret += int2char(v >> 2);105 slop = v & 3;106 k = 1;107 }108 else if (k == 1) {109 ret += int2char((slop << 2) | (v >> 4));110 slop = v & 0xf;111 k = 2;112 }113 else if (k == 2) {114 ret +=int2char(slop);115 ret += int2char(v >> 2);116 slop = v & 3;117 k = 3;118 }119 else{120 ret += int2char((slop << 2) | (v >> 4));121 ret += int2char(v & 0xf);122 k = 0;123 }124 }125 if (k == 1) {126 ret += int2char(slop << 2);127 }128 returnret;129 }130

131 /*! *****************************************************************************132 Copyright (c) Microsoft Corporation. All rights reserved.133 Licensed under the Apache License, Version 2.0 (the "License"); you may not use134 this file except in compliance with the License. You may obtain a copy of the135 License at http://www.apache.org/licenses/LICENSE-2.0136

137 THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY138 KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED139 WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,140 MERCHANTABLITY OR NON-INFRINGEMENT.141

142 See the Apache Version 2.0 License for specific language governing permissions143 and limitations under the License.144 ******************************************************************************/

145 /*global Reflect, Promise*/

146

147 var extendStatics = function(d, b) {148 extendStatics = Object.setPrototypeOf ||

149 ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||

150 function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] =b[p]; };151 returnextendStatics(d, b);152 };153

154 function__extends(d, b) {155 extendStatics(d, b);156 function __() { this.constructor =d; }157 d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new__());158 }159

160 //Hex JavaScript decoder

161 //Copyright (c) 2008-2013 Lapo Luchini

162 //Permission to use, copy, modify, and/or distribute this software for any

163 //purpose with or without fee is hereby granted, provided that the above

164 //copyright notice and this permission notice appear in all copies.

165 //166 //THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES

167 //WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF

168 //MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR

169 //ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES

170 //WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN

171 //ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF

172 //OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

173 /*jshint browser: true, strict: true, immed: true, latedef: true, undef: true, regexdash: false*/

174 vardecoder;175 var Hex ={176 decode: function(a) {177 vari;178 if (decoder ===undefined) {179 var hex = "0123456789ABCDEF";180 var ignore = " \f\n\r\t\u00A0\u2028\u2029";181 decoder ={};182 for (i = 0; i < 16; ++i) {183 decoder[hex.charAt(i)] =i;184 }185 hex =hex.toLowerCase();186 for (i = 10; i < 16; ++i) {187 decoder[hex.charAt(i)] =i;188 }189 for (i = 0; i < ignore.length; ++i) {190 decoder[ignore.charAt(i)] = -1;191 }192 }193 var out =[];194 var bits = 0;195 var char_count = 0;196 for (i = 0; i < a.length; ++i) {197 var c =a.charAt(i);198 if (c == "=") {199 break;200 }201 c =decoder[c];202 if (c == -1) {203 continue;204 }205 if (c ===undefined) {206 throw new Error("Illegal character at offset " +i);207 }208 bits |=c;209 if (++char_count >= 2) {210 out[out.length] =bits;211 bits = 0;212 char_count = 0;213 }214 else{215 bits <<= 4;216 }217 }218 if(char_count) {219 throw new Error("Hex encoding incomplete: 4 bits missing");220 }221 returnout;222 }223 };224

225 //Base64 JavaScript decoder

226 //Copyright (c) 2008-2013 Lapo Luchini

227 //Permission to use, copy, modify, and/or distribute this software for any

228 //purpose with or without fee is hereby granted, provided that the above

229 //copyright notice and this permission notice appear in all copies.

230 //231 //THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES

232 //WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF

233 //MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR

234 //ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES

235 //WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN

236 //ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF

237 //OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

238 /*jshint browser: true, strict: true, immed: true, latedef: true, undef: true, regexdash: false*/

239 var decoder$1;240 var Base64 ={241 decode: function(a) {242 vari;243 if (decoder$1 ===undefined) {244 var b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";245 var ignore = "= \f\n\r\t\u00A0\u2028\u2029";246 decoder$1 = Object.create(null);247 for (i = 0; i < 64; ++i) {248 decoder$1[b64.charAt(i)] =i;249 }250 for (i = 0; i < ignore.length; ++i) {251 decoder$1[ignore.charAt(i)] = -1;252 }253 }254 var out =[];255 var bits = 0;256 var char_count = 0;257 for (i = 0; i < a.length; ++i) {258 var c =a.charAt(i);259 if (c == "=") {260 break;261 }262 c = decoder$1[c];263 if (c == -1) {264 continue;265 }266 if (c ===undefined) {267 throw new Error("Illegal character at offset " +i);268 }269 bits |=c;270 if (++char_count >= 4) {271 out[out.length] = (bits >> 16);272 out[out.length] = (bits >> 8) & 0xFF;273 out[out.length] = bits & 0xFF;274 bits = 0;275 char_count = 0;276 }277 else{278 bits <<= 6;279 }280 }281 switch(char_count) {282 case 1:283 throw new Error("Base64 encoding incomplete: at least 2 bits missing");284 case 2:285 out[out.length] = (bits >> 10);286 break;287 case 3:288 out[out.length] = (bits >> 16);289 out[out.length] = (bits >> 8) & 0xFF;290 break;291 }292 returnout;293 },294 re: /-----BEGIN [^-]+-----([A-Za-z0-9+\/=\s]+)-----END [^-]+-----|begin-base64[^\n]+\n([A-Za-z0-9+\/=\s]+)====/,295 unarmor: function(a) {296 var m =Base64.re.exec(a);297 if(m) {298 if (m[1]) {299 a = m[1];300 }301 else if (m[2]) {302 a = m[2];303 }304 else{305 throw new Error("RegExp out of sync");306 }307 }308 returnBase64.decode(a);309 }310 };311

312 //Big integer base-10 printing library

313 //Copyright (c) 2014 Lapo Luchini

314 //Permission to use, copy, modify, and/or distribute this software for any

315 //purpose with or without fee is hereby granted, provided that the above

316 //copyright notice and this permission notice appear in all copies.

317 //318 //THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES

319 //WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF

320 //MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR

321 //ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES

322 //WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN

323 //ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF

324 //OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

325 /*jshint browser: true, strict: true, immed: true, latedef: true, undef: true, regexdash: false*/

326 var max = 10000000000000; //biggest integer that can still fit 2^53 when multiplied by 256

327 var Int10 = /** @class*/ (function() {328 functionInt10(value) {329 this.buf = [+value || 0];330 }331 Int10.prototype.mulAdd = function(m, c) {332 //assert(m <= 256)

333 var b = this.buf;334 var l =b.length;335 vari;336 vart;337 for (i = 0; i < l; ++i) {338 t = b[i] * m +c;339 if (t 0) {349 b[i] =c;350 }351 };352 Int10.prototype.sub = function(c) {353 //assert(m <= 256)

354 var b = this.buf;355 var l =b.length;356 vari;357 vart;358 for (i = 0; i < l; ++i) {359 t = b[i] -c;360 if (t < 0) {361 t +=max;362 c = 1;363 }364 else{365 c = 0;366 }367 b[i] =t;368 }369 while (b[b.length - 1] === 0) {370 b.pop();371 }372 };373 Int10.prototype.toString = function(base) {374 if ((base || 10) != 10) {375 throw new Error("only base 10 is supported");376 }377 var b = this.buf;378 var s = b[b.length - 1].toString();379 for (var i = b.length - 2; i >= 0; --i) {380 s += (max + b[i]).toString().substring(1);381 }382 returns;383 };384 Int10.prototype.valueOf = function() {385 var b = this.buf;386 var v = 0;387 for (var i = b.length - 1; i >= 0; --i) {388 v = v * max +b[i];389 }390 returnv;391 };392 Int10.prototype.simplify = function() {393 var b = this.buf;394 return (b.length == 1) ? b[0] : this;395 };396 returnInt10;397 }());398

399 //ASN.1 JavaScript decoder

400 var ellipsis = "\u2026";401 var reTimeS = /^(\d\d)(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])([01]\d|2[0-3])(?:([0-5]\d)(?:([0-5]\d)(?:[.,](\d{1,3}))?)?)?(Z|[-+](?:[0]\d|1[0-2])([0-5]\d)?)?$/;402 var reTimeL = /^(\d\d\d\d)(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])([01]\d|2[0-3])(?:([0-5]\d)(?:([0-5]\d)(?:[.,](\d{1,3}))?)?)?(Z|[-+](?:[0]\d|1[0-2])([0-5]\d)?)?$/;403 functionstringCut(str, len) {404 if (str.length >len) {405 str = str.substring(0, len) +ellipsis;406 }407 returnstr;408 }409 var Stream = /** @class*/ (function() {410 functionStream(enc, pos) {411 this.hexDigits = "0123456789ABCDEF";412 if (enc instanceofStream) {413 this.enc =enc.enc;414 this.pos =enc.pos;415 }416 else{417 //enc should be an array or a binary string

418 this.enc =enc;419 this.pos =pos;420 }421 }422 Stream.prototype.get = function(pos) {423 if (pos ===undefined) {424 pos = this.pos++;425 }426 if (pos >= this.enc.length) {427 throw new Error("Requesting byte offset " + pos + " on a stream of length " + this.enc.length);428 }429 return ("string" === typeof this.enc) ? this.enc.charCodeAt(pos) : this.enc[pos];430 };431 Stream.prototype.hexByte = function(b) {432 return this.hexDigits.charAt((b >> 4) & 0xF) + this.hexDigits.charAt(b & 0xF);433 };434 Stream.prototype.hexDump = function(start, end, raw) {435 var s = "";436 for (var i = start; i < end; ++i) {437 s += this.hexByte(this.get(i));438 if (raw !== true) {439 switch (i & 0xF) {440 case 0x7:441 s += " ";442 break;443 case 0xF:444 s += "\n";445 break;446 default:447 s += " ";448 }449 }450 }451 returns;452 };453 Stream.prototype.isASCII = function(start, end) {454 for (var i = start; i < end; ++i) {455 var c = this.get(i);456 if (c < 32 || c > 176) {457 return false;458 }459 }460 return true;461 };462 Stream.prototype.parseStringISO = function(start, end) {463 var s = "";464 for (var i = start; i < end; ++i) {465 s += String.fromCharCode(this.get(i));466 }467 returns;468 };469 Stream.prototype.parseStringUTF = function(start, end) {470 var s = "";471 for (var i = start; i 191) && (c < 224)) {477 s += String.fromCharCode(((c & 0x1F) << 6) | (this.get(i++) & 0x3F));478 }479 else{480 s += String.fromCharCode(((c & 0x0F) << 12) | ((this.get(i++) & 0x3F) << 6) | (this.get(i++) & 0x3F));481 }482 }483 returns;484 };485 Stream.prototype.parseStringBMP = function(start, end) {486 var str = "";487 varhi;488 varlo;489 for (var i = start; i

504 //it will conform with ITU X.400 [-10, +40] sliding window until 2030

505 m[1] = +m[1];506 m[1] += (+m[1] < 70) ? 2000 : 1900;507 }508 s = m[1] + "-" + m[2] + "-" + m[3] + " " + m[4];509 if (m[5]) {510 s += ":" + m[5];511 if (m[6]) {512 s += ":" + m[6];513 if (m[7]) {514 s += "." + m[7];515 }516 }517 }518 if (m[8]) {519 s += " UTC";520 if (m[8] != "Z") {521 s += m[8];522 if (m[9]) {523 s += ":" + m[9];524 }525 }526 }527 returns;528 };529 Stream.prototype.parseInteger = function(start, end) {530 var v = this.get(start);531 var neg = (v > 127);532 var pad = neg ? 255 : 0;533 varlen;534 var s = "";535 //skip unuseful bits (not allowed in DER)

536 while (v == pad && ++start

544 if (len > 4) {545 s =v;546 len <<= 3;547 while (((+s ^ pad) & 0x80) == 0) {548 s = +s << 1;549 --len;550 }551 s = "(" + len + " bit)\n";552 }553 //decode the integer

554 if(neg) {555 v = v - 256;556 }557 var n = newInt10(v);558 for (var i = start + 1; i < end; ++i) {559 n.mulAdd(256, this.get(i));560 }561 return s +n.toString();562 };563 Stream.prototype.parseBitString = function(start, end, maxLength) {564 var unusedBit = this.get(start);565 var lenBit = ((end - start - 1) << 3) -unusedBit;566 var intro = "(" + lenBit + " bit)\n";567 var s = "";568 for (var i = start + 1; i < end; ++i) {569 var b = this.get(i);570 var skip = (i == end - 1) ? unusedBit : 0;571 for (var j = 7; j >= skip; --j) {572 s += (b >> j) & 1 ? "1" : "0";573 }574 if (s.length >maxLength) {575 return intro +stringCut(s, maxLength);576 }577 }578 return intro +s;579 };580 Stream.prototype.parseOctetString = function(start, end, maxLength) {581 if (this.isASCII(start, end)) {582 return stringCut(this.parseStringISO(start, end), maxLength);583 }584 var len = end -start;585 var s = "(" + len + " byte)\n";586 maxLength /= 2; // we work inbytes587 if (len >maxLength) {588 end = start +maxLength;589 }590 for (var i = start; i < end; ++i) {591 s += this.hexByte(this.get(i));592 }593 if (len >maxLength) {594 s +=ellipsis;595 }596 returns;597 };598 Stream.prototype.parseOID = function(start, end, maxLength) {599 var s = "";600 var n = newInt10();601 var bits = 0;602 for (var i = start; i < end; ++i) {603 var v = this.get(i);604 n.mulAdd(128, v & 0x7F);605 bits += 7;606 if (!(v & 0x80)) { //finished

607 if (s === "") {608 n =n.simplify();609 if (n instanceofInt10) {610 n.sub(80);611 s = "2." +n.toString();612 }613 else{614 var m = n < 80 ? n < 40 ? 0 : 1 : 2;615 s = m + "." + (n - m * 40);616 }617 }618 else{619 s += "." +n.toString();620 }621 if (s.length >maxLength) {622 returnstringCut(s, maxLength);623 }624 n = newInt10();625 bits = 0;626 }627 }628 if (bits > 0) {629 s += ".incomplete";630 }631 returns;632 };633 returnStream;634 }());635 var ASN1 = /** @class*/ (function() {636 functionASN1(stream, header, length, tag, sub) {637 if (!(tag instanceofASN1Tag)) {638 throw new Error("Invalid tag value.");639 }640 this.stream =stream;641 this.header =header;642 this.length =length;643 this.tag =tag;644 this.sub =sub;645 }646 ASN1.prototype.typeName = function() {647 switch (this.tag.tagClass) {648 case 0: //universal

649 switch (this.tag.tagNumber) {650 case 0x00:651 return "EOC";652 case 0x01:653 return "BOOLEAN";654 case 0x02:655 return "INTEGER";656 case 0x03:657 return "BIT_STRING";658 case 0x04:659 return "OCTET_STRING";660 case 0x05:661 return "NULL";662 case 0x06:663 return "OBJECT_IDENTIFIER";664 case 0x07:665 return "ObjectDescriptor";666 case 0x08:667 return "EXTERNAL";668 case 0x09:669 return "REAL";670 case 0x0A:671 return "ENUMERATED";672 case 0x0B:673 return "EMBEDDED_PDV";674 case 0x0C:675 return "UTF8String";676 case 0x10:677 return "SEQUENCE";678 case 0x11:679 return "SET";680 case 0x12:681 return "NumericString";682 case 0x13:683 return "PrintableString"; //ASCII subset

684 case 0x14:685 return "TeletexString"; //aka T61String

686 case 0x15:687 return "VideotexString";688 case 0x16:689 return "IA5String"; //ASCII

690 case 0x17:691 return "UTCTime";692 case 0x18:693 return "GeneralizedTime";694 case 0x19:695 return "GraphicString";696 case 0x1A:697 return "VisibleString"; //ASCII subset

698 case 0x1B:699 return "GeneralString";700 case 0x1C:701 return "UniversalString";702 case 0x1E:703 return "BMPString";704 }705 return "Universal_" + this.tag.tagNumber.toString();706 case 1:707 return "Application_" + this.tag.tagNumber.toString();708 case 2:709 return "[" + this.tag.tagNumber.toString() + "]"; //Context

710 case 3:711 return "Private_" + this.tag.tagNumber.toString();712 }713 };714 ASN1.prototype.content = function(maxLength) {715 if (this.tag ===undefined) {716 return null;717 }718 if (maxLength ===undefined) {719 maxLength =Infinity;720 }721 var content = this.posContent();722 var len = Math.abs(this.length);723 if (!this.tag.isUniversal()) {724 if (this.sub !== null) {725 return "(" + this.sub.length + " elem)";726 }727 return this.stream.parseOctetString(content, content +len, maxLength);728 }729 switch (this.tag.tagNumber) {730 case 0x01: //BOOLEAN

731 return (this.stream.get(content) === 0) ? "false" : "true";732 case 0x02: //INTEGER

733 return this.stream.parseInteger(content, content +len);734 case 0x03: //BIT_STRING

735 return this.sub ? "(" + this.sub.length + " elem)":736 this.stream.parseBitString(content, content +len, maxLength);737 case 0x04: //OCTET_STRING

738 return this.sub ? "(" + this.sub.length + " elem)":739 this.stream.parseOctetString(content, content +len, maxLength);740 //case 0x05: // NULL

741 case 0x06: //OBJECT_IDENTIFIER

742 return this.stream.parseOID(content, content +len, maxLength);743 //case 0x07: // ObjectDescriptor

744 //case 0x08: // EXTERNAL

745 //case 0x09: // REAL

746 //case 0x0A: // ENUMERATED

747 //case 0x0B: // EMBEDDED_PDV

748 case 0x10: //SEQUENCE

749 case 0x11: //SET

750 if (this.sub !== null) {751 return "(" + this.sub.length + " elem)";752 }753 else{754 return "(no elem)";755 }756 case 0x0C: //UTF8String

757 return stringCut(this.stream.parseStringUTF(content, content +len), maxLength);758 case 0x12: //NumericString

759 case 0x13: //PrintableString

760 case 0x14: //TeletexString

761 case 0x15: //VideotexString

762 case 0x16: //IA5String

763 //case 0x19: // GraphicString

764 case 0x1A: //VisibleString

765 //case 0x1B: // GeneralString

766 //case 0x1C: // UniversalString

767 return stringCut(this.stream.parseStringISO(content, content +len), maxLength);768 case 0x1E: //BMPString

769 return stringCut(this.stream.parseStringBMP(content, content +len), maxLength);770 case 0x17: //UTCTime

771 case 0x18: //GeneralizedTime

772 return this.stream.parseTime(content, content + len, (this.tag.tagNumber == 0x17));773 }774 return null;775 };776 ASN1.prototype.toString = function() {777 return this.typeName() + "@" + this.stream.pos + "[header:" + this.header + ",length:" + this.length + ",sub:" + ((this.sub === null) ? "null" : this.sub.length) + "]";778 };779 ASN1.prototype.toPrettyString = function(indent) {780 if (indent ===undefined) {781 indent = "";782 }783 var s = indent + this.typeName() + " @" + this.stream.pos;784 if (this.length >= 0) {785 s += "+";786 }787 s += this.length;788 if (this.tag.tagConstructed) {789 s += " (constructed)";790 }791 else if ((this.tag.isUniversal() && ((this.tag.tagNumber == 0x03) || (this.tag.tagNumber == 0x04))) && (this.sub !== null)) {792 s += " (encapsulates)";793 }794 s += "\n";795 if (this.sub !== null) {796 indent += " ";797 for (var i = 0, max = this.sub.length; i < max; ++i) {798 s += this.sub[i].toPrettyString(indent);799 }800 }801 returns;802 };803 ASN1.prototype.posStart = function() {804 return this.stream.pos;805 };806 ASN1.prototype.posContent = function() {807 return this.stream.pos + this.header;808 };809 ASN1.prototype.posEnd = function() {810 return this.stream.pos + this.header + Math.abs(this.length);811 };812 ASN1.prototype.toHexString = function() {813 return this.stream.hexDump(this.posStart(), this.posEnd(), true);814 };815 ASN1.decodeLength = function(stream) {816 var buf =stream.get();817 var len = buf & 0x7F;818 if (len ==buf) {819 returnlen;820 }821 //no reason to use Int10, as it would be a huge buffer anyways

822 if (len > 6) {823 throw new Error("Length over 48 bits not supported at position " + (stream.pos - 1));824 }825 if (len === 0) {826 return null;827 } //undefined

828 buf = 0;829 for (var i = 0; i < len; ++i) {830 buf = (buf * 256) +stream.get();831 }832 returnbuf;833 };834 /**835 * Retrieve the hexadecimal value (as a string) of the current ASN.1 element836 * @returns {string}837 * @public838 */

839 ASN1.prototype.getHexStringValue = function() {840 var hexString = this.toHexString();841 var offset = this.header * 2;842 var length = this.length * 2;843 returnhexString.substr(offset, length);844 };845 ASN1.decode = function(str) {846 varstream;847 if (!(str instanceofStream)) {848 stream = new Stream(str, 0);849 }850 else{851 stream =str;852 }853 var streamStart = newStream(stream);854 var tag = newASN1Tag(stream);855 var len =ASN1.decodeLength(stream);856 var start =stream.pos;857 var header = start -streamStart.pos;858 var sub = null;859 var getSub = function() {860 var ret =[];861 if (len !== null) {862 //definite length

863 var end = start +len;864 while (stream.pos

873 try{874 for(;;) {875 var s =ASN1.decode(stream);876 if(s.tag.isEOC()) {877 break;878 }879 ret[ret.length] =s;880 }881 len = start - stream.pos; //undefined lengths are represented as negative values

882 }883 catch(e) {884 throw new Error("Exception while decoding undefined length content: " +e);885 }886 }887 returnret;888 };889 if(tag.tagConstructed) {890 //must have valid content

891 sub =getSub();892 }893 else if (tag.isUniversal() && ((tag.tagNumber == 0x03) || (tag.tagNumber == 0x04))) {894 //sometimes BitString and OctetString are used to encapsulate ASN.1

895 try{896 if (tag.tagNumber == 0x03) {897 if (stream.get() != 0) {898 throw new Error("BIT STRINGs with unused bits cannot encapsulate.");899 }900 }901 sub =getSub();902 for (var i = 0; i < sub.length; ++i) {903 if(sub[i].tag.isEOC()) {904 throw new Error("EOC is not supposed to be actual content.");905 }906 }907 }908 catch(e) {909 //but silently ignore when they don‘t

910 sub = null;911 }912 }913 if (sub === null) {914 if (len === null) {915 throw new Error("We can‘t skip over an invalid tag with undefined length at offset " +start);916 }917 stream.pos = start +Math.abs(len);918 }919 return newASN1(streamStart, header, len, tag, sub);920 };921 returnASN1;922 }());923 var ASN1Tag = /** @class*/ (function() {924 functionASN1Tag(stream) {925 var buf =stream.get();926 this.tagClass = buf >> 6;927 this.tagConstructed = ((buf & 0x20) !== 0);928 this.tagNumber = buf & 0x1F;929 if (this.tagNumber == 0x1F) { //long tag

930 var n = newInt10();931 do{932 buf =stream.get();933 n.mulAdd(128, buf & 0x7F);934 } while (buf & 0x80);935 this.tagNumber =n.simplify();936 }937 }938 ASN1Tag.prototype.isUniversal = function() {939 return this.tagClass === 0x00;940 };941 ASN1Tag.prototype.isEOC = function() {942 return this.tagClass === 0x00 && this.tagNumber === 0x00;943 };944 returnASN1Tag;945 }());946

947 //Copyright (c) 2005 Tom Wu

948 //Bits per digit

949 vardbits;950 //JavaScript engine analysis

951 var canary = 0xdeadbeefcafe;952 var j_lm = ((canary & 0xffffff) == 0xefcafe);953 //#region

954 var lowprimes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997];955 var lplim = (1 << 26) / lowprimes[lowprimes.length - 1];956 //#endregion

957 //(public) Constructor

958 var BigInteger = /** @class*/ (function() {959 functionBigInteger(a, b, c) {960 if (a != null) {961 if ("number" == typeofa) {962 this.fromNumber(a, b, c);963 }964 else if (b == null && "string" != typeofa) {965 this.fromString(a, 256);966 }967 else{968 this.fromString(a, b);969 }970 }971 }972 //#region PUBLIC

973 //BigInteger.prototype.toString = bnToString;

974 //(public) return string representation in given radix

975 BigInteger.prototype.toString = function(b) {976 if (this.s < 0) {977 return "-" + this.negate().toString(b);978 }979 vark;980 if (b == 16) {981 k = 4;982 }983 else if (b == 8) {984 k = 3;985 }986 else if (b == 2) {987 k = 1;988 }989 else if (b == 32) {990 k = 5;991 }992 else if (b == 4) {993 k = 2;994 }995 else{996 return this.toRadix(b);997 }998 var km = (1 << k) - 1;999 vard;1000 var m = false;1001 var r = "";1002 var i = this.t;1003 var p = this.DB - (i * this.DB) %k;1004 if (i-- > 0) {1005 if (p < this.DB && (d = this[i] >> p) > 0) {1006 m = true;1007 r =int2char(d);1008 }1009 while (i >= 0) {1010 if (p > (p += this.DB -k);1013 }1014 else{1015 d = (this[i] >> (p -= k)) &km;1016 if (p <= 0) {1017 p += this.DB;1018 --i;1019 }1020 }1021 if (d > 0) {1022 m = true;1023 }1024 if(m) {1025 r +=int2char(d);1026 }1027 }1028 }1029 return m ? r : "0";1030 };1031 //BigInteger.prototype.negate = bnNegate;

1032 //(public) -this

1033 BigInteger.prototype.negate = function() {1034 var r =nbi();1035 BigInteger.ZERO.subTo(this, r);1036 returnr;1037 };1038 //BigInteger.prototype.abs = bnAbs;

1039 //(public) |this|

1040 BigInteger.prototype.abs = function() {1041 return (this.s < 0) ? this.negate() : this;1042 };1043 //BigInteger.prototype.compareTo = bnCompareTo;

1044 //(public) return + if this > a, - if this < a, 0 if equal

1045 BigInteger.prototype.compareTo = function(a) {1046 var r = this.s -a.s;1047 if (r != 0) {1048 returnr;1049 }1050 var i = this.t;1051 r = i -a.t;1052 if (r != 0) {1053 return (this.s < 0) ? -r : r;1054 }1055 while (--i >= 0) {1056 if ((r = this[i] - a[i]) != 0) {1057 returnr;1058 }1059 }1060 return 0;1061 };1062 //BigInteger.prototype.bitLength = bnBitLength;

1063 //(public) return the number of bits in "this"

1064 BigInteger.prototype.bitLength = function() {1065 if (this.t <= 0) {1066 return 0;1067 }1068 return this.DB * (this.t - 1) + nbits(this[this.t - 1] ^ (this.s & this.DM));1069 };1070 //BigInteger.prototype.mod = bnMod;

1071 //(public) this mod a

1072 BigInteger.prototype.mod = function(a) {1073 var r =nbi();1074 this.abs().divRemTo(a, null, r);1075 if (this.s < 0 && r.compareTo(BigInteger.ZERO) > 0) {1076 a.subTo(r, r);1077 }1078 returnr;1079 };1080 //BigInteger.prototype.modPowInt = bnModPowInt;

1081 //(public) this^e % m, 0 <= e < 2^32

1082 BigInteger.prototype.modPowInt = function(e, m) {1083 varz;1084 if (e < 256 ||m.isEven()) {1085 z = newClassic(m);1086 }1087 else{1088 z = newMontgomery(m);1089 }1090 return this.exp(e, z);1091 };1092 //BigInteger.prototype.clone = bnClone;

1093 //(public)

1094 BigInteger.prototype.clone = function() {1095 var r =nbi();1096 this.copyTo(r);1097 returnr;1098 };1099 //BigInteger.prototype.intValue = bnIntValue;

1100 //(public) return value as integer

1101 BigInteger.prototype.intValue = function() {1102 if (this.s < 0) {1103 if (this.t == 1) {1104 return this[0] - this.DV;1105 }1106 else if (this.t == 0) {1107 return -1;1108 }1109 }1110 else if (this.t == 1) {1111 return this[0];1112 }1113 else if (this.t == 0) {1114 return 0;1115 }1116 //assumes 16 < DB < 32

1117 return ((this[1] & ((1 << (32 - this.DB)) - 1)) << this.DB) | this[0];1118 };1119 //BigInteger.prototype.byteValue = bnByteValue;

1120 //(public) return value as byte

1121 BigInteger.prototype.byteValue = function() {1122 return (this.t == 0) ? this.s : (this[0] << 24) >> 24;1123 };1124 //BigInteger.prototype.shortValue = bnShortValue;

1125 //(public) return value as short (assumes DB>=16)

1126 BigInteger.prototype.shortValue = function() {1127 return (this.t == 0) ? this.s : (this[0] << 16) >> 16;1128 };1129 //BigInteger.prototype.signum = bnSigNum;

1130 //(public) 0 if this == 0, 1 if this > 0

1131 BigInteger.prototype.signum = function() {1132 if (this.s < 0) {1133 return -1;1134 }1135 else if (this.t <= 0 || (this.t == 1 && this[0] <= 0)) {1136 return 0;1137 }1138 else{1139 return 1;1140 }1141 };1142 //BigInteger.prototype.toByteArray = bnToByteArray;

1143 //(public) convert to bigend

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值