找回密码
 立即注册
首页 业界区 安全 js逆向实战之某加速商城password参数加密

js逆向实战之某加速商城password参数加密

愿隙 4 天前
声明:本篇文章仅用于知识分享,不得用于其他用途

网址:aHR0cDovL3d3dy4xNXl1bm1hbGwuY29tL3BjL2xvZ2luL2luZGV4
加密逻辑


  • 随便输入用户名和密码登录,抓流量包。
    1.png

  • 这里看着两个参数都是加密的,但是你用同样的用户名和密码登录,会发现只有u[password]是变动的。
    2.png

  • 全局搜索password关键字,有很多处,但是可以明显的看到加密函数的有两处,是rsa加密。
    3.png

  • 不能判断是哪一处,全部打上断点,触发流量包。
    4.png

  • 这里面涉及三个函数:RSAKey,setPublic,encrypt,且公钥还是个定值,再细看下这三个函数的位置,都是同一个文件下。
    5.png

    6.png

    7.png

    细心的人会看到文件名叫做rsa.js,说明整个文件都是rsa算法相关的,全部拷出来。
    运行一下,会报BigInteger未定义的错误。
    8.png

  • 找到BigInteger的定义位置。
    9.png

    还能看到文件置顶的注释说依赖jsbn.js和rng.js两个文件,对应上了。
    10.png

  • 一样的操作,将jsbn.js中的内容全部拷贝出来。
    11.png

    运行一下,会报navigator未定义的错误。
    12.png

    非常简单,只需在报错的位置上面添加var navigator = {}一行代码即可。再次运行,会报SecureRandom未定义的错误,找到定义处。
    13.png

  • 将rng.js中的代码全部拷贝出来,注意有先后顺序,rng.js的代码要放到jsbn.js的代码下面,否则会报navigator未定义的错误,或者可以将navigator定义到最上面。运行一下,说rng_psize未定义。
    14.png

  • 从rng.js上面的注释可以看到需要一个PRNG backend,依赖prng4.js。
    15.png

    全局搜索rng_psize,总共4处,三处位于rng.js文件内,一处位于prng4.js,还是个定值。一样的操作把prng4.js中的代码全部拷贝出来。运行一下,成功得到加密后的值。
    16.png

  • 由于该网页现在注册需要判断推荐人手机号,没法验证,只需搞懂相关逻辑即可。
  • 完整代码如下。
  1. // prng4.js - uses Arcfour as a PRNG
  2. function Arcfour() {
  3.   this.i = 0;
  4.   this.j = 0;
  5.   this.S = new Array();
  6. }
  7. // Initialize arcfour context from key, an array of ints, each from [0..255]
  8. function ARC4init(key) {
  9.   var i, j, t;
  10.   for(i = 0; i < 256; ++i)
  11.     this.S[i] = i;
  12.   j = 0;
  13.   for(i = 0; i < 256; ++i) {
  14.     j = (j + this.S[i] + key[i % key.length]) & 255;
  15.     t = this.S[i];
  16.     this.S[i] = this.S[j];
  17.     this.S[j] = t;
  18.   }
  19.   this.i = 0;
  20.   this.j = 0;
  21. }
  22. function ARC4next() {
  23.   var t;
  24.   this.i = (this.i + 1) & 255;
  25.   this.j = (this.j + this.S[this.i]) & 255;
  26.   t = this.S[this.i];
  27.   this.S[this.i] = this.S[this.j];
  28.   this.S[this.j] = t;
  29.   return this.S[(t + this.S[this.i]) & 255];
  30. }
  31. Arcfour.prototype.init = ARC4init;
  32. Arcfour.prototype.next = ARC4next;
  33. // Plug in your RNG constructor here
  34. function prng_newstate() {
  35.   return new Arcfour();
  36. }
  37. // Pool size must be a multiple of 4 and greater than 32.
  38. // An array of bytes the size of the pool will be passed to init()
  39. var rng_psize = 256;
  40. // Copyright (c) 2005  Tom Wu
  41. // All Rights Reserved.
  42. // See "LICENSE" for details.
  43. // Basic JavaScript BN library - subset useful for RSA encryption.
  44. // Bits per digit
  45. var dbits;
  46. // JavaScript engine analysis
  47. var canary = 0xdeadbeefcafe;
  48. var j_lm = ((canary&0xffffff)==0xefcafe);
  49. // (public) Constructor
  50. function BigInteger(a,b,c) {
  51.   if(a != null)
  52.     if("number" == typeof a) this.fromNumber(a,b,c);
  53.     else if(b == null && "string" != typeof a) this.fromString(a,256);
  54.     else this.fromString(a,b);
  55. }
  56. // return new, unset BigInteger
  57. function nbi() { return new BigInteger(null); }
  58. // am: Compute w_j += (x*this_i), propagate carries,
  59. // c is initial carry, returns final carry.
  60. // c < 3*dvalue, x < 2*dvalue, this_i < dvalue
  61. // We need to select the fastest one that works in this environment.
  62. // am1: use a single mult and divide to get the high bits,
  63. // max digit bits should be 26 because
  64. // max internal value = 2*dvalue^2-2*dvalue (< 2^53)
  65. function am1(i,x,w,j,c,n) {
  66.   while(--n >= 0) {
  67.     var v = x*this[i++]+w[j]+c;
  68.     c = Math.floor(v/0x4000000);
  69.     w[j++] = v&0x3ffffff;
  70.   }
  71.   return c;
  72. }
  73. // am2 avoids a big mult-and-extract completely.
  74. // Max digit bits should be <= 30 because we do bitwise ops
  75. // on values up to 2*hdvalue^2-hdvalue-1 (< 2^31)
  76. function am2(i,x,w,j,c,n) {
  77.   var xl = x&0x7fff, xh = x>>15;
  78.   while(--n >= 0) {
  79.     var l = this[i]&0x7fff;
  80.     var h = this[i++]>>15;
  81.     var m = xh*l+h*xl;
  82.     l = xl*l+((m&0x7fff)<<15)+w[j]+(c&0x3fffffff);
  83.     c = (l>>>30)+(m>>>15)+xh*h+(c>>>30);
  84.     w[j++] = l&0x3fffffff;
  85.   }
  86.   return c;
  87. }
  88. // Alternately, set max digit bits to 28 since some
  89. // browsers slow down when dealing with 32-bit numbers.
  90. function am3(i,x,w,j,c,n) {
  91.   var xl = x&0x3fff, xh = x>>14;
  92.   while(--n >= 0) {
  93.     var l = this[i]&0x3fff;
  94.     var h = this[i++]>>14;
  95.     var m = xh*l+h*xl;
  96.     l = xl*l+((m&0x3fff)<<14)+w[j]+c;
  97.     c = (l>>28)+(m>>14)+xh*h;
  98.     w[j++] = l&0xfffffff;
  99.   }
  100.   return c;
  101. }
  102. var navigator = {};
  103. if(j_lm && (navigator.appName == "Microsoft Internet Explorer")) {
  104.   BigInteger.prototype.am = am2;
  105.   dbits = 30;
  106. }
  107. else if(j_lm && (navigator.appName != "Netscape")) {
  108.   BigInteger.prototype.am = am1;
  109.   dbits = 26;
  110. }
  111. else { // Mozilla/Netscape seems to prefer am3
  112.   BigInteger.prototype.am = am3;
  113.   dbits = 28;
  114. }
  115. BigInteger.prototype.DB = dbits;
  116. BigInteger.prototype.DM = ((1<<dbits)-1);
  117. BigInteger.prototype.DV = (1<<dbits);
  118. var BI_FP = 52;
  119. BigInteger.prototype.FV = Math.pow(2,BI_FP);
  120. BigInteger.prototype.F1 = BI_FP-dbits;
  121. BigInteger.prototype.F2 = 2*dbits-BI_FP;
  122. // Digit conversions
  123. var BI_RM = "0123456789abcdefghijklmnopqrstuvwxyz";
  124. var BI_RC = new Array();
  125. var rr,vv;
  126. rr = "0".charCodeAt(0);
  127. for(vv = 0; vv <= 9; ++vv) BI_RC[rr++] = vv;
  128. rr = "a".charCodeAt(0);
  129. for(vv = 10; vv < 36; ++vv) BI_RC[rr++] = vv;
  130. rr = "A".charCodeAt(0);
  131. for(vv = 10; vv < 36; ++vv) BI_RC[rr++] = vv;
  132. function int2char(n) { return BI_RM.charAt(n); }
  133. function intAt(s,i) {
  134.   var c = BI_RC[s.charCodeAt(i)];
  135.   return (c==null)?-1:c;
  136. }
  137. // (protected) copy this to r
  138. function bnpCopyTo(r) {
  139.   for(var i = this.t-1; i >= 0; --i) r[i] = this[i];
  140.   r.t = this.t;
  141.   r.s = this.s;
  142. }
  143. // (protected) set from integer value x, -DV <= x < DV
  144. function bnpFromInt(x) {
  145.   this.t = 1;
  146.   this.s = (x<0)?-1:0;
  147.   if(x > 0) this[0] = x;
  148.   else if(x < -1) this[0] = x+DV;
  149.   else this.t = 0;
  150. }
  151. // return bigint initialized to value
  152. function nbv(i) { var r = nbi(); r.fromInt(i); return r; }
  153. // (protected) set from string and radix
  154. function bnpFromString(s,b) {
  155.   var k;
  156.   if(b == 16) k = 4;
  157.   else if(b == 8) k = 3;
  158.   else if(b == 256) k = 8; // byte array
  159.   else if(b == 2) k = 1;
  160.   else if(b == 32) k = 5;
  161.   else if(b == 4) k = 2;
  162.   else { this.fromRadix(s,b); return; }
  163.   this.t = 0;
  164.   this.s = 0;
  165.   var i = s.length, mi = false, sh = 0;
  166.   while(--i >= 0) {
  167.     var x = (k==8)?s[i]&0xff:intAt(s,i);
  168.     if(x < 0) {
  169.       if(s.charAt(i) == "-") mi = true;
  170.       continue;
  171.     }
  172.     mi = false;
  173.     if(sh == 0)
  174.       this[this.t++] = x;
  175.     else if(sh+k > this.DB) {
  176.       this[this.t-1] |= (x&((1<<(this.DB-sh))-1))<<sh;
  177.       this[this.t++] = (x>>(this.DB-sh));
  178.     }
  179.     else
  180.       this[this.t-1] |= x<<sh;
  181.     sh += k;
  182.     if(sh >= this.DB) sh -= this.DB;
  183.   }
  184.   if(k == 8 && (s[0]&0x80) != 0) {
  185.     this.s = -1;
  186.     if(sh > 0) this[this.t-1] |= ((1<<(this.DB-sh))-1)<<sh;
  187.   }
  188.   this.clamp();
  189.   if(mi) BigInteger.ZERO.subTo(this,this);
  190. }
  191. // (protected) clamp off excess high words
  192. function bnpClamp() {
  193.   var c = this.s&this.DM;
  194.   while(this.t > 0 && this[this.t-1] == c) --this.t;
  195. }
  196. // (public) return string representation in given radix
  197. function bnToString(b) {
  198.   if(this.s < 0) return "-"+this.negate().toString(b);
  199.   var k;
  200.   if(b == 16) k = 4;
  201.   else if(b == 8) k = 3;
  202.   else if(b == 2) k = 1;
  203.   else if(b == 32) k = 5;
  204.   else if(b == 4) k = 2;
  205.   else return this.toRadix(b);
  206.   var km = (1<<k)-1, d, m = false, r = "", i = this.t;
  207.   var p = this.DB-(i*this.DB)%k;
  208.   if(i-- > 0) {
  209.     if(p < this.DB && (d = this[i]>>p) > 0) { m = true; r = int2char(d); }
  210.     while(i >= 0) {
  211.       if(p < k) {
  212.         d = (this[i]&((1<<p)-1))<<(k-p);
  213.         d |= this[--i]>>(p+=this.DB-k);
  214.       }
  215.       else {
  216.         d = (this[i]>>(p-=k))&km;
  217.         if(p <= 0) { p += this.DB; --i; }
  218.       }
  219.       if(d > 0) m = true;
  220.       if(m) r += int2char(d);
  221.     }
  222.   }
  223.   return m?r:"0";
  224. }
  225. // (public) -this
  226. function bnNegate() { var r = nbi(); BigInteger.ZERO.subTo(this,r); return r; }
  227. // (public) |this|
  228. function bnAbs() { return (this.s<0)?this.negate():this; }
  229. // (public) return + if this > a, - if this < a, 0 if equal
  230. function bnCompareTo(a) {
  231.   var r = this.s-a.s;
  232.   if(r != 0) return r;
  233.   var i = this.t;
  234.   r = i-a.t;
  235.   if(r != 0) return r;
  236.   while(--i >= 0) if((r=this[i]-a[i]) != 0) return r;
  237.   return 0;
  238. }
  239. // returns bit length of the integer x
  240. function nbits(x) {
  241.   var r = 1, t;
  242.   if((t=x>>>16) != 0) { x = t; r += 16; }
  243.   if((t=x>>8) != 0) { x = t; r += 8; }
  244.   if((t=x>>4) != 0) { x = t; r += 4; }
  245.   if((t=x>>2) != 0) { x = t; r += 2; }
  246.   if((t=x>>1) != 0) { x = t; r += 1; }
  247.   return r;
  248. }
  249. // (public) return the number of bits in "this"
  250. function bnBitLength() {
  251.   if(this.t <= 0) return 0;
  252.   return this.DB*(this.t-1)+nbits(this[this.t-1]^(this.s&this.DM));
  253. }
  254. // (protected) r = this << n*DB
  255. function bnpDLShiftTo(n,r) {
  256.   var i;
  257.   for(i = this.t-1; i >= 0; --i) r[i+n] = this[i];
  258.   for(i = n-1; i >= 0; --i) r[i] = 0;
  259.   r.t = this.t+n;
  260.   r.s = this.s;
  261. }
  262. // (protected) r = this >> n*DB
  263. function bnpDRShiftTo(n,r) {
  264.   for(var i = n; i < this.t; ++i) r[i-n] = this[i];
  265.   r.t = Math.max(this.t-n,0);
  266.   r.s = this.s;
  267. }
  268. // (protected) r = this << n
  269. function bnpLShiftTo(n,r) {
  270.   var bs = n%this.DB;
  271.   var cbs = this.DB-bs;
  272.   var bm = (1<<cbs)-1;
  273.   var ds = Math.floor(n/this.DB), c = (this.s<<bs)&this.DM, i;
  274.   for(i = this.t-1; i >= 0; --i) {
  275.     r[i+ds+1] = (this[i]>>cbs)|c;
  276.     c = (this[i]&bm)<<bs;
  277.   }
  278.   for(i = ds-1; i >= 0; --i) r[i] = 0;
  279.   r[ds] = c;
  280.   r.t = this.t+ds+1;
  281.   r.s = this.s;
  282.   r.clamp();
  283. }
  284. // (protected) r = this >> n
  285. function bnpRShiftTo(n,r) {
  286.   r.s = this.s;
  287.   var ds = Math.floor(n/this.DB);
  288.   if(ds >= this.t) { r.t = 0; return; }
  289.   var bs = n%this.DB;
  290.   var cbs = this.DB-bs;
  291.   var bm = (1<<bs)-1;
  292.   r[0] = this[ds]>>bs;
  293.   for(var i = ds+1; i < this.t; ++i) {
  294.     r[i-ds-1] |= (this[i]&bm)<<cbs;
  295.     r[i-ds] = this[i]>>bs;
  296.   }
  297.   if(bs > 0) r[this.t-ds-1] |= (this.s&bm)<<cbs;
  298.   r.t = this.t-ds;
  299.   r.clamp();
  300. }
  301. // (protected) r = this - a
  302. function bnpSubTo(a,r) {
  303.   var i = 0, c = 0, m = Math.min(a.t,this.t);
  304.   while(i < m) {
  305.     c += this[i]-a[i];
  306.     r[i++] = c&this.DM;
  307.     c >>= this.DB;
  308.   }
  309.   if(a.t < this.t) {
  310.     c -= a.s;
  311.     while(i < this.t) {
  312.       c += this[i];
  313.       r[i++] = c&this.DM;
  314.       c >>= this.DB;
  315.     }
  316.     c += this.s;
  317.   }
  318.   else {
  319.     c += this.s;
  320.     while(i < a.t) {
  321.       c -= a[i];
  322.       r[i++] = c&this.DM;
  323.       c >>= this.DB;
  324.     }
  325.     c -= a.s;
  326.   }
  327.   r.s = (c<0)?-1:0;
  328.   if(c < -1) r[i++] = this.DV+c;
  329.   else if(c > 0) r[i++] = c;
  330.   r.t = i;
  331.   r.clamp();
  332. }
  333. // (protected) r = this * a, r != this,a (HAC 14.12)
  334. // "this" should be the larger one if appropriate.
  335. function bnpMultiplyTo(a,r) {
  336.   var x = this.abs(), y = a.abs();
  337.   var i = x.t;
  338.   r.t = i+y.t;
  339.   while(--i >= 0) r[i] = 0;
  340.   for(i = 0; i < y.t; ++i) r[i+x.t] = x.am(0,y[i],r,i,0,x.t);
  341.   r.s = 0;
  342.   r.clamp();
  343.   if(this.s != a.s) BigInteger.ZERO.subTo(r,r);
  344. }
  345. // (protected) r = this^2, r != this (HAC 14.16)
  346. function bnpSquareTo(r) {
  347.   var x = this.abs();
  348.   var i = r.t = 2*x.t;
  349.   while(--i >= 0) r[i] = 0;
  350.   for(i = 0; i < x.t-1; ++i) {
  351.     var c = x.am(i,x[i],r,2*i,0,1);
  352.     if((r[i+x.t]+=x.am(i+1,2*x[i],r,2*i+1,c,x.t-i-1)) >= x.DV) {
  353.       r[i+x.t] -= x.DV;
  354.       r[i+x.t+1] = 1;
  355.     }
  356.   }
  357.   if(r.t > 0) r[r.t-1] += x.am(i,x[i],r,2*i,0,1);
  358.   r.s = 0;
  359.   r.clamp();
  360. }
  361. // (protected) divide this by m, quotient and remainder to q, r (HAC 14.20)
  362. // r != q, this != m.  q or r may be null.
  363. function bnpDivRemTo(m,q,r) {
  364.   var pm = m.abs();
  365.   if(pm.t <= 0) return;
  366.   var pt = this.abs();
  367.   if(pt.t < pm.t) {
  368.     if(q != null) q.fromInt(0);
  369.     if(r != null) this.copyTo(r);
  370.     return;
  371.   }
  372.   if(r == null) r = nbi();
  373.   var y = nbi(), ts = this.s, ms = m.s;
  374.   var nsh = this.DB-nbits(pm[pm.t-1]);        // normalize modulus
  375.   if(nsh > 0) { pm.lShiftTo(nsh,y); pt.lShiftTo(nsh,r); }
  376.   else { pm.copyTo(y); pt.copyTo(r); }
  377.   var ys = y.t;
  378.   var y0 = y[ys-1];
  379.   if(y0 == 0) return;
  380.   var yt = y0*(1<<this.F1)+((ys>1)?y[ys-2]>>this.F2:0);
  381.   var d1 = this.FV/yt, d2 = (1<<this.F1)/yt, e = 1<<this.F2;
  382.   var i = r.t, j = i-ys, t = (q==null)?nbi():q;
  383.   y.dlShiftTo(j,t);
  384.   if(r.compareTo(t) >= 0) {
  385.     r[r.t++] = 1;
  386.     r.subTo(t,r);
  387.   }
  388.   BigInteger.ONE.dlShiftTo(ys,t);
  389.   t.subTo(y,y);        // "negative" y so we can replace sub with am later
  390.   while(y.t < ys) y[y.t++] = 0;
  391.   while(--j >= 0) {
  392.     // Estimate quotient digit
  393.     var qd = (r[--i]==y0)?this.DM:Math.floor(r[i]*d1+(r[i-1]+e)*d2);
  394.     if((r[i]+=y.am(0,qd,r,j,0,ys)) < qd) {        // Try it out
  395.       y.dlShiftTo(j,t);
  396.       r.subTo(t,r);
  397.       while(r[i] < --qd) r.subTo(t,r);
  398.     }
  399.   }
  400.   if(q != null) {
  401.     r.drShiftTo(ys,q);
  402.     if(ts != ms) BigInteger.ZERO.subTo(q,q);
  403.   }
  404.   r.t = ys;
  405.   r.clamp();
  406.   if(nsh > 0) r.rShiftTo(nsh,r);        // Denormalize remainder
  407.   if(ts < 0) BigInteger.ZERO.subTo(r,r);
  408. }
  409. // (public) this mod a
  410. function bnMod(a) {
  411.   var r = nbi();
  412.   this.abs().divRemTo(a,null,r);
  413.   if(this.s < 0 && r.compareTo(BigInteger.ZERO) > 0) a.subTo(r,r);
  414.   return r;
  415. }
  416. // Modular reduction using "classic" algorithm
  417. function Classic(m) { this.m = m; }
  418. function cConvert(x) {
  419.   if(x.s < 0 || x.compareTo(this.m) >= 0) return x.mod(this.m);
  420.   else return x;
  421. }
  422. function cRevert(x) { return x; }
  423. function cReduce(x) { x.divRemTo(this.m,null,x); }
  424. function cMulTo(x,y,r) { x.multiplyTo(y,r); this.reduce(r); }
  425. function cSqrTo(x,r) { x.squareTo(r); this.reduce(r); }
  426. Classic.prototype.convert = cConvert;
  427. Classic.prototype.revert = cRevert;
  428. Classic.prototype.reduce = cReduce;
  429. Classic.prototype.mulTo = cMulTo;
  430. Classic.prototype.sqrTo = cSqrTo;
  431. // (protected) return "-1/this % 2^DB"; useful for Mont. reduction
  432. // justification:
  433. //         xy == 1 (mod m)
  434. //         xy =  1+km
  435. //   xy(2-xy) = (1+km)(1-km)
  436. // x[y(2-xy)] = 1-k^2m^2
  437. // x[y(2-xy)] == 1 (mod m^2)
  438. // if y is 1/x mod m, then y(2-xy) is 1/x mod m^2
  439. // should reduce x and y(2-xy) by m^2 at each step to keep size bounded.
  440. // JS multiply "overflows" differently from C/C++, so care is needed here.
  441. function bnpInvDigit() {
  442.   if(this.t < 1) return 0;
  443.   var x = this[0];
  444.   if((x&1) == 0) return 0;
  445.   var y = x&3;                // y == 1/x mod 2^2
  446.   y = (y*(2-(x&0xf)*y))&0xf;        // y == 1/x mod 2^4
  447.   y = (y*(2-(x&0xff)*y))&0xff;        // y == 1/x mod 2^8
  448.   y = (y*(2-(((x&0xffff)*y)&0xffff)))&0xffff;        // y == 1/x mod 2^16
  449.   // last step - calculate inverse mod DV directly;
  450.   // assumes 16 < DB <= 32 and assumes ability to handle 48-bit ints
  451.   y = (y*(2-x*y%this.DV))%this.DV;                // y == 1/x mod 2^dbits
  452.   // we really want the negative inverse, and -DV < y < DV
  453.   return (y>0)?this.DV-y:-y;
  454. }
  455. // Montgomery reduction
  456. function Montgomery(m) {
  457.   this.m = m;
  458.   this.mp = m.invDigit();
  459.   this.mpl = this.mp&0x7fff;
  460.   this.mph = this.mp>>15;
  461.   this.um = (1<<(m.DB-15))-1;
  462.   this.mt2 = 2*m.t;
  463. }
  464. // xR mod m
  465. function montConvert(x) {
  466.   var r = nbi();
  467.   x.abs().dlShiftTo(this.m.t,r);
  468.   r.divRemTo(this.m,null,r);
  469.   if(x.s < 0 && r.compareTo(BigInteger.ZERO) > 0) this.m.subTo(r,r);
  470.   return r;
  471. }
  472. // x/R mod m
  473. function montRevert(x) {
  474.   var r = nbi();
  475.   x.copyTo(r);
  476.   this.reduce(r);
  477.   return r;
  478. }
  479. // x = x/R mod m (HAC 14.32)
  480. function montReduce(x) {
  481.   while(x.t <= this.mt2)        // pad x so am has enough room later
  482.     x[x.t++] = 0;
  483.   for(var i = 0; i < this.m.t; ++i) {
  484.     // faster way of calculating u0 = x[i]*mp mod DV
  485.     var j = x[i]&0x7fff;
  486.     var u0 = (j*this.mpl+(((j*this.mph+(x[i]>>15)*this.mpl)&this.um)<<15))&x.DM;
  487.     // use am to combine the multiply-shift-add into one call
  488.     j = i+this.m.t;
  489.     x[j] += this.m.am(0,u0,x,i,0,this.m.t);
  490.     // propagate carry
  491.     while(x[j] >= x.DV) { x[j] -= x.DV; x[++j]++; }
  492.   }
  493.   x.clamp();
  494.   x.drShiftTo(this.m.t,x);
  495.   if(x.compareTo(this.m) >= 0) x.subTo(this.m,x);
  496. }
  497. // r = "x^2/R mod m"; x != r
  498. function montSqrTo(x,r) { x.squareTo(r); this.reduce(r); }
  499. // r = "xy/R mod m"; x,y != r
  500. function montMulTo(x,y,r) { x.multiplyTo(y,r); this.reduce(r); }
  501. Montgomery.prototype.convert = montConvert;
  502. Montgomery.prototype.revert = montRevert;
  503. Montgomery.prototype.reduce = montReduce;
  504. Montgomery.prototype.mulTo = montMulTo;
  505. Montgomery.prototype.sqrTo = montSqrTo;
  506. // (protected) true iff this is even
  507. function bnpIsEven() { return ((this.t>0)?(this[0]&1):this.s) == 0; }
  508. // (protected) this^e, e < 2^32, doing sqr and mul with "r" (HAC 14.79)
  509. function bnpExp(e,z) {
  510.   if(e > 0xffffffff || e < 1) return BigInteger.ONE;
  511.   var r = nbi(), r2 = nbi(), g = z.convert(this), i = nbits(e)-1;
  512.   g.copyTo(r);
  513.   while(--i >= 0) {
  514.     z.sqrTo(r,r2);
  515.     if((e&(1<<i)) > 0) z.mulTo(r2,g,r);
  516.     else { var t = r; r = r2; r2 = t; }
  517.   }
  518.   return z.revert(r);
  519. }
  520. // (public) this^e % m, 0 <= e < 2^32
  521. function bnModPowInt(e,m) {
  522.   var z;
  523.   if(e < 256 || m.isEven()) z = new Classic(m); else z = new Montgomery(m);
  524.   return this.exp(e,z);
  525. }
  526. // protected
  527. BigInteger.prototype.copyTo = bnpCopyTo;
  528. BigInteger.prototype.fromInt = bnpFromInt;
  529. BigInteger.prototype.fromString = bnpFromString;
  530. BigInteger.prototype.clamp = bnpClamp;
  531. BigInteger.prototype.dlShiftTo = bnpDLShiftTo;
  532. BigInteger.prototype.drShiftTo = bnpDRShiftTo;
  533. BigInteger.prototype.lShiftTo = bnpLShiftTo;
  534. BigInteger.prototype.rShiftTo = bnpRShiftTo;
  535. BigInteger.prototype.subTo = bnpSubTo;
  536. BigInteger.prototype.multiplyTo = bnpMultiplyTo;
  537. BigInteger.prototype.squareTo = bnpSquareTo;
  538. BigInteger.prototype.divRemTo = bnpDivRemTo;
  539. BigInteger.prototype.invDigit = bnpInvDigit;
  540. BigInteger.prototype.isEven = bnpIsEven;
  541. BigInteger.prototype.exp = bnpExp;
  542. // public
  543. BigInteger.prototype.toString = bnToString;
  544. BigInteger.prototype.negate = bnNegate;
  545. BigInteger.prototype.abs = bnAbs;
  546. BigInteger.prototype.compareTo = bnCompareTo;
  547. BigInteger.prototype.bitLength = bnBitLength;
  548. BigInteger.prototype.mod = bnMod;
  549. BigInteger.prototype.modPowInt = bnModPowInt;
  550. // "constants"
  551. BigInteger.ZERO = nbv(0);
  552. BigInteger.ONE = nbv(1);
  553. // Random number generator - requires a PRNG backend, e.g. prng4.js
  554. // For best results, put code like
  555. // <body onClick='rng_seed_time();' onKeyPress='rng_seed_time();'>
  556. // in your main HTML document.
  557. var rng_state;
  558. var rng_pool;
  559. var rng_pptr;
  560. // Mix in a 32-bit integer into the pool
  561. function rng_seed_int(x) {
  562.   rng_pool[rng_pptr++] ^= x & 255;
  563.   rng_pool[rng_pptr++] ^= (x >> 8) & 255;
  564.   rng_pool[rng_pptr++] ^= (x >> 16) & 255;
  565.   rng_pool[rng_pptr++] ^= (x >> 24) & 255;
  566.   if(rng_pptr >= rng_psize) rng_pptr -= rng_psize;
  567. }
  568. // Mix in the current time (w/milliseconds) into the pool
  569. function rng_seed_time() {
  570.   rng_seed_int(new Date().getTime());
  571. }
  572. // Initialize the pool with junk if needed.
  573. if(rng_pool == null) {
  574.   rng_pool = new Array();
  575.   rng_pptr = 0;
  576.   var t;
  577.   if(navigator.appName == "Netscape" && navigator.appVersion < "5" && window.crypto) {
  578.     // Extract entropy (256 bits) from NS4 RNG if available
  579.     var z = window.crypto.random(32);
  580.     for(t = 0; t < z.length; ++t)
  581.       rng_pool[rng_pptr++] = z.charCodeAt(t) & 255;
  582.   }
  583.   while(rng_pptr < rng_psize) {  // extract some randomness from Math.random()
  584.     t = Math.floor(65536 * Math.random());
  585.     rng_pool[rng_pptr++] = t >>> 8;
  586.     rng_pool[rng_pptr++] = t & 255;
  587.   }
  588.   rng_pptr = 0;
  589.   rng_seed_time();
  590.   //rng_seed_int(window.screenX);
  591.   //rng_seed_int(window.screenY);
  592. }
  593. function rng_get_byte() {
  594.   if(rng_state == null) {
  595.     rng_seed_time();
  596.     rng_state = prng_newstate();
  597.     rng_state.init(rng_pool);
  598.     for(rng_pptr = 0; rng_pptr < rng_pool.length; ++rng_pptr)
  599.       rng_pool[rng_pptr] = 0;
  600.     rng_pptr = 0;
  601.     //rng_pool = null;
  602.   }
  603.   // TODO: allow reseeding after first request
  604.   return rng_state.next();
  605. }
  606. function rng_get_bytes(ba) {
  607.   var i;
  608.   for(i = 0; i < ba.length; ++i) ba[i] = rng_get_byte();
  609. }
  610. function SecureRandom() {}
  611. SecureRandom.prototype.nextBytes = rng_get_bytes;
  612. // Depends on jsbn.js and rng.js
  613. // Version 1.1: support utf-8 encoding in pkcs1pad2
  614. // convert a (hex) string to a bignum object
  615. function parseBigInt(str,r) {
  616.   return new BigInteger(str,r);
  617. }
  618. function linebrk(s,n) {
  619.   var ret = "";
  620.   var i = 0;
  621.   while(i + n < s.length) {
  622.     ret += s.substring(i,i+n) + "\n";
  623.     i += n;
  624.   }
  625.   return ret + s.substring(i,s.length);
  626. }
  627. function byte2Hex(b) {
  628.   if(b < 0x10)
  629.     return "0" + b.toString(16);
  630.   else
  631.     return b.toString(16);
  632. }
  633. // PKCS#1 (type 2, random) pad input string s to n bytes, and return a bigint
  634. function pkcs1pad2(s,n) {
  635.   if(n < s.length + 11) { // TODO: fix for utf-8
  636.     alert("Message too long for RSA");
  637.     return null;
  638.   }
  639.   var ba = new Array();
  640.   var i = s.length - 1;
  641.   while(i >= 0 && n > 0) {
  642.     var c = s.charCodeAt(i--);
  643.     if(c < 128) { // encode using utf-8
  644.       ba[--n] = c;
  645.     }
  646.     else if((c > 127) && (c < 2048)) {
  647.       ba[--n] = (c & 63) | 128;
  648.       ba[--n] = (c >> 6) | 192;
  649.     }
  650.     else {
  651.       ba[--n] = (c & 63) | 128;
  652.       ba[--n] = ((c >> 6) & 63) | 128;
  653.       ba[--n] = (c >> 12) | 224;
  654.     }
  655.   }
  656.   ba[--n] = 0;
  657.   var rng = new SecureRandom();
  658.   var x = new Array();
  659.   while(n > 2) { // random non-zero pad
  660.     x[0] = 0;
  661.     while(x[0] == 0) rng.nextBytes(x);
  662.     ba[--n] = x[0];
  663.   }
  664.   ba[--n] = 2;
  665.   ba[--n] = 0;
  666.   return new BigInteger(ba);
  667. }
  668. // "empty" RSA key constructor
  669. function RSAKey() {
  670.   this.n = null;
  671.   this.e = 0;
  672.   this.d = null;
  673.   this.p = null;
  674.   this.q = null;
  675.   this.dmp1 = null;
  676.   this.dmq1 = null;
  677.   this.coeff = null;
  678. }
  679. // Set the public key fields N and e from hex strings
  680. function RSASetPublic(N,E) {
  681.   if(N != null && E != null && N.length > 0 && E.length > 0) {
  682.     this.n = parseBigInt(N,16);
  683.     this.e = parseInt(E,16);
  684.   }
  685.   else
  686.     alert("Invalid RSA public key");
  687. }
  688. // Perform raw public operation on "x": return x^e (mod n)
  689. function RSADoPublic(x) {
  690.   return x.modPowInt(this.e, this.n);
  691. }
  692. // Return the PKCS#1 RSA encryption of "text" as an even-length hex string
  693. function RSAEncrypt(text) {
  694.   var m = pkcs1pad2(text,(this.n.bitLength()+7)>>3);
  695.   if(m == null) return null;
  696.   var c = this.doPublic(m);
  697.   if(c == null) return null;
  698.   var h = c.toString(16);
  699.   if((h.length & 1) == 0) return h; else return "0" + h;
  700. }
  701. // Return the PKCS#1 RSA encryption of "text" as a Base64-encoded string
  702. //function RSAEncryptB64(text) {
  703. //  var h = this.encrypt(text);
  704. //  if(h) return hex2b64(h); else return null;
  705. //}
  706. // protected
  707. RSAKey.prototype.doPublic = RSADoPublic;
  708. // public
  709. RSAKey.prototype.setPublic = RSASetPublic;
  710. RSAKey.prototype.encrypt = RSAEncrypt;
  711. //RSAKey.prototype.encrypt_b64 = RSAEncryptB64;
  712. function encrypt(){
  713.   var public_key = "00bdf3db924714b9c4ddd144910071c282e235ac51371037cf89fa08f28b9105b6326338ed211280154c645bf81bae4184c2b52e2b02b0953e7aa8b25a8e212a0b";
  714.   var public_length = "10001";
  715.   var rsa = new RSAKey();
  716.   rsa.setPublic(public_key, public_length);
  717.   var res = rsa.encrypt("123456");
  718.   console.log(res);
  719.   return res;
  720. }
  721. encrypt();
复制代码
来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
您需要登录后才可以回帖 登录 | 立即注册