JS端程式碼:
複製程式碼 程式碼如下:
//檔案base64.js:
var b64map="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
var b64pad="=";
函數 hex2b64(h) {
var i;
var c;
var ret = "";
for(i = 0; i+3 c = parseInt(h.substring(i,i+3),16);
ret += b64map.charAt(c >> 6) + b64map.charAt(c & 63);
}
if(i+1 == h.length) {
c = parseInt(h.substring(i,i+1),16);
ret += b64map.charAt(c }
else if(i+2 == h.length) {
c = parseInt(h.substring(i,i+2),16);
ret += b64map.charAt(c >> 2) + b64map.charAt((c & 3) }
while((ret.length & 3) > 0) ret += b64pad;
回傳ret;
}
//將Base64轉換字串為復健十六
function b64tohex(s) {
var ret = ""
var i;
var k = 0; // b64 狀態,0-3
var slop;
for(i = 0; i if(s.charAt(i) == b64pad) break;
v = b64map.indexOf(s.charAt(i));
if(v if(k == 0) {
ret += int2char(v >> 2);
斜率 = v & 3;
k = 1;
}
else if(k == 1) {
ret += int2char((slop > 4));
斜率 = v & 0xf;
k = 2;
}
else if(k == 2) {
ret += int2char(slop);
ret += int2char(v >> 2);
斜率 = v & 3;
k = 3;
}
else {
ret += int2char((slop > 4));
ret += int2char(v & 0xf);
k = 0;
}
}
if(k == 1)
ret += int2char(slop 回傳ret;
}
// 將 Base64 轉換字串為位元組/數位資料庫
function b64toBA(s) {
// 現在搬運 b64tohex,延遲
var h = b64tohex(s) );
var i;
var a = new Array();
for(i = 0; 2*i a[i] = parseInt(h.substring(2*i,2*i+2),16) ;
}
回傳一個;
}
#檔案jsbn.js
// 版權所有 (c) 2005 Tom Wu
// 保留所有權利。資訊請參閱「許可證」。
// JavaScript 引擎分析
var canary = 0xdeadbeefcafe;
var j_lm = ((canary&0xffffff)==0xefcafe);
//(公共建構)函數
function BigInteger(a,b,c) {
if(a != null)
if("number" == typeof a) this.fromNumber( a ,估計);
else if(b == null && "string" != typeof a) this.fromString(a,256);
否則this.fromString(a,b); }
// 回傳new,取消設定BigInteger
function nbi() { return new BigInteger(null); }
// am: 計算w_j += (x*this_i),傳播進位,
// c是最終進位,返回最終進位。 am1:使用單一乘法和除法來獲得高位,
// 最大杜拜應為26,因為
// 最大內部值= 2*dvalue^2-2*dvalue ( 函數am1(i,x,w,j,c,n) {
while(--n >= 0) {
var v = x*this[i++ ]+w [j]+c;
c = Math.floor(v/0x4000000);
w[j++] = v&0x3ffffff
}
回c
}
// am2;
回傳c
}
// am2;
//最大可達完全// 最大為2*hdvalue^2-hdvalue-1 (function am2( i,x,w,j,c ,n) {
var xl = x&0x7fff, xh = x>>15
while(--n >= 0) {
var l = this[i]&0x7fff;
var h = this [i++]>>15;
var m = xh*l+h*xl;
l = xl*l+(( m&0x7fff)c = (l>>30)+(m>>15)+xh*h+(c>>30);
w[j++] = l&0x3fffffff;
}
回傳c;
}
//或者,將最大設定設為 28,因為某些
// 瀏覽器在處理 32 位元數字時速度會變慢。 ,j,c,n) {
var xl = x&0x3fff, xh = x>>14;
while(--n >= 0) {
var l = this[i]&0x3fff;
var h = this[i++]>>14;
var m = xh*l+h*xl;
l = xl*l+((m&0x3fff)c = (l>28)+(m>14)+xh*h;
w[j++] = l&0xffffffff;
}
回傳c;
}
if(j_lm && (navigator.appName == "Microsoft Internet Explorer")) {
BigInteger.prototype.am = am2;
dbits = 30;
}
else if(j_lm && (navigator.appName != "Netscape")) {
BigInteger.prototype.am = am1;
dbits = 26;
}
else { // Mozilla/Netscape 似乎比較喜歡 am3
BigInteger.prototype.am = am3;
dbits = 28;
}
BigInteger.prototype.DB = dbits;
BigInteger.prototype.DM = ((1BigInteger.prototype.DV = (1var BI_FP = 52;
BigInteger.prototype.FV = Math.pow(2,BI_FP);
BigInteger.prototype.F1 = BI_FP-dbits;
BigInteger.prototype.F2 = 2*dbits-BI_FP;
// 預設值
var BI_RM = "0123456789abcdefghijklmnopqrstuvwxyz";
var BI_RC = new Array();
var rr,vv;
rr = "0".charCodeAt(0);
for ( vv = 0 ; vv rr = "a".charCodeAt(0);
for ( vv = 10 ; vv rr = "A".charCodeAt(0);
for ( vv = 10 ; vv function int2char ( n ) { return BI_RM . }
function intAt(s,i) {
var c = BI_RC[s.charCodeAt(i)];
定義(c==null)?-1:c;
}
//(預設)public_copy r
function bnpCopyTo(r) {
for(var i = this.t-1; i >= 0; --i r[i); ] = 返回[i];
r.t = this.t;
r.s = this.s;
}
//(預設)解鎖x鍵,-DV set bnpFromInt(x) {
this.t = 1;
this.s = (x if ( x > 0 ) this [ 0 ] = x ;否則如果 ( x 指定.t = 0;
}
// 回傳邏輯輸入 begint
function nbv(i) { var r = nbi(); r.fromInt(i);default r2 }
//(無限期)解鎖字串而不是字串
function bnpFromString(s,b) {
var k;
如果 ( b == 16 ) k = 4 ;
設定 if(b == 8) k = 3;
設定 if ( b == 256 ) k = 8 ; // 預設
else if(b == 2) k = 1;
設定 if(b == 32) k = 5;
設定 if(b == 4) k = 2;
else { this.fromRadix(s,b);default; }
這個 .
this.s = 0;
var i = s.length, mi = false, sh = 0;
while(--i >= 0) {
white x = (k==8)?s[i]&0xff:intAt(s,i);
if(x if(s.charAt(i) == "-") i = true;
定義:
}
me = false;
if ( sh == 0 )
this [ this . t ++ ] = x ;
else if (sh+k > this.DB) {
this[this.t-1] |=(x&((1this[this.t++] = (x>>(this.DB-sh));
}
else
define[this.t-1] |= xsh += k;
if ( sh >= this .DB ) sh -= this .DB ;
}
if (k == 8 && (s[0]&0x80) != 0) {
this.s = -1;
if (sh > 0) this[this.t-1] |= ((1}
this.clamp();
if(my) BigInteger.ZERO.subTo(this,this);
}
//(補充)啟用箝位
function bnpClamp() {
var c = this.s&this.DM;
while ( this .t > 0 && this [ this .t - 1 ] == c ) -- this ;
}
//(company) 宣告字串的預設值
function bnToString(b) {
if(this.s var k;
如果 ( b == 16 ) k = 4 ;
設定 if(b == 8) k = 3;
設定 if(b == 2) k = 1;
設定 if(b == 32) k = 5;
設定 if(b == 4) k = 2;
公用預設 this.toRadix(b);
var km = (1var p = this.DB-(i*this.DB)%k; {
if(i-- > 0) {
if(p >p) > 0) { m = true; } r = int2char(d); }
while(i >= 0) {
if(p d = (this[i]&((1d |= this[--i]>>(p+=this.DB-k);
}
else {
d = (this[i]>>(p-=k))&km;
if ( p }
if ( d > 0 ) m = true ;
if(m) r += int2char(d);
}
}
return m?r:"0";
}
//(內部)-這個
function bnNegate() { var r = nbi(); BigInteger.ZERO.subTo(this,r);預設 r0 }
//(number)|this|
function bnAbs ( ) { return ( this . s //(value)default + public value > a, - valuefunction bnCompareTo(a) {
var r = this.s-a.s;
if(r != 0) 回傳 r;
var i = this.t;
r = i-a.t0
if(r != 0) return r;
while (--i >= 0) if((r=this[i]-a[i]) != 0) return r;
default00
}
// 預設值 x 預設值
function nbits(x) {
var r = 1, t;
if((t=x>>16) != 0) { x = t; r+=16H }
if((t=x>>8) != 0) { x = t; r+=80 }
if((t=x>>4) != 0) { x = t; r+=40 }
if((t=x>>2) != 0) { x = t; r+=20 }
if((t=x>>1) != 0) { x = t; r+=10 }
定義 r;
}
//(public)聲明「this」賦值
function bnBitLength() {
if(this.t 指定this.DB*(this.t-1)+nbits(this[this.t-1]^(this.s&this.DM));
}
//(預設)r = this function bnpDLShiftTo(n,r) {
var i;
for ( i = this .t - 1 ; i >= 0 ; --i ) r [ i + n ] = this [ i ] ;
for ( i = n - 1 ; i >= 0 ; --i ) r [ i ] = 0 ;
r.t = this.t+n;
r.s = this.s;
}
//(受保護)r = this>> n*DB
function bnpDRShiftTo(n,r) {
for(var i = n; i r.t = Math.max(this.t-n,0);
r.s = this.s;
}
//(受保護)r = this function bnpLShiftTo(n,r) {
var bs = n%this.DB;
var cbs = this.DB-bs;
var bm = (1var ds = Math.floor(n/this.DB), c = (this.sfor(i = this.t-1; i >= 0; --i) {
r[i+ds+1] = (this[i]>>cbs)|c;
c = (this[i]&bm)}
for(i = ds-1; i >= 0; --i) r[i] = 0;
r[ds] = c;
r.t = this.t+ds+1;
r.s = this.s;
r.clamp();
}
//(受保護)r = this >> n
函數 bnpRShiftTo(n,r) {
r.s = this.s;
var ds = Math.floor(n/this.DB);
if(ds >= this.t) { r.t = 0;回傳; }
var bs = n%this.DB;
var cbs = this.DB-bs;
var bm = (1r[0] = this[ds]>>bs;
for(var i = ds+1; i r[i-ds-1] |= (this[i]&bm)r[i-ds] = this[i]>>bs;
}
if(bs > 0) r[this.t-ds-1] |= (this.s&bm)r.t = this.t-ds;
r.clamp();
}
//(受保護) r = this - a
function bnpSubTo(a,r) {
var i = 0, c = 0, m = Math.min(a.t,this .t);
while(i c += this[i]-a[i];
r[i++] = c&this.DM; .DB;
}
if(a.t c -= a.s;
while(i c += this[i];
r[i++] = c&this.DM;
c>>= this.DB;
c += this.s; += this.s;
while(i c -= a[i ];
r[i++] = c&this.DM;
}
c -= a.s;
}
r.s = (cif( c 0) r[i++] = c;
r.clamp(); = this * a, r != this,a (HAC 14.12)
//如果合適,「this」應該是擴大的那個。 );
var i = x.t;
r.t = i+y.t;
while(--i >= 0) r[i] = 0;
for(i = 0; i r.s = 0 ;
r.clamp();
if(this.s != a.s) BigInteger.ZERO.subTo(r,r);
}
//(受保護)r = this^ 2、r != this (HAC 14.16)
function bnpSquareTo(r) {
var x = this.abs();
var i = r.t = 2*x.t;
while(--i >= 0) r[i] = 0;
for(i = 0; i var c = x.am(i,x[i],r,2*i,0,1);
if((r[i+x.t]+=x.am(i+1,2*x[i],r,2*i+1,c,x.t-i-1)) >= x 。 DV) {
r[i+x.t] -= x.DV;
r[i+x.t+1] = 1;
}
}
if(r.t > 0) r[r.t-1] += x.am(i,x[i],r,2*i,0,1);
r.s = 0;
r.clamp();
}
//(受保護)將其除以m,商數和餘數為q,r (HAC 14.20)
// r != q,this != m。
function bnpDivRemTo(m,q,r) {
var pm = m.abs();
if(pm.t var pt = this.abs();
if(pt.t if(q != null) q.fromInt(0);
if(r != null) this.copyTo(r);
回傳;
}
if(r == null) r = nbi();
var y = nbi(), ts = this.s, ms = m.s;
var nsh = this.DB-nbits(pm[pm.t-1]); // 標準化模
if(nsh > 0) { pm.lShiftTo(nsh,y); pt.lShiftTo(nsh,r); }
else { pm.copyTo(y); pt.copyTo(r); }
var ys = y.t;
var y0 = y[ys-1];
if(y0 == 0) 回傳;
var yt = y0*(11)?y[ys-2]>>this.F2:0) ;
var d1 = this.FV/yt, d2 = (1var i = r.t, j = i-ys, t = (q==null)?nbi():q;
y.dlShiftTo(j,t);
if(r.compareTo(t) >= 0) {
r[r.t++] = 1;
r.subTo(t,r);
}
BigInteger.ONE.dlShiftTo(ys,t);
t.subTo(y,y); // 「負」 y 因此我們可以稍後用 am 取代 sub
while(y.t while(--j >= 0) {
//估計商足夠
var qd = (r[--i]==y0)?this.DM:Math.floor(r[ i ]*d1+(r[i-1]+e)*d2);
if((r[i]+=y.am(0,qd,r,j,0,ys)) y.dlShiftTo(j,t) ;
r.subTo(t,r);
while(r[i] }
}
if(q != null) {
r.drShiftTo(ys,q);
if(ts != ms) BigInteger.ZERO.subTo(q,q);
}
r.t = ys;
r.clamp();
if(nsh > 0) r.rShiftTo(nsh,r); // 反規範化餘數
if(ts }
//(公)此 mod a
function bnMod(a) {
var r = nbi();
this.abs().divRemTo(a,null,r);
if(this.s 0) a.subTo(r,r);
回傳;
}
// 使用「經典」演算法進行模歸約
function Classic(m) { this.m = m; }
function cConvert(x) {
if(x.s = 0) return x.mod(this.m);
否則回傳x;
}
函數 cRevert(x) { return x; }
函數cReduce(x) { x.divRemTo(this.m,null,x); }
函數cMulTo(x,y,r) { x.multiplyTo(y,r); this.reduce(r ); }
函數cSqrTo(x,r) { x.squareTo(r); this.reduce(r); }
Classic.prototype.convert = cConvert;
Classic.prototype.revert = cRevert ;
Classic.prototype.reduce = cReduce;
Classic.prototype.mulTo = cMulTo;
Classic.prototype.sqrTo = cSqrTo;
//(受保護)return "-1/this % 2^DB";對蒙有用。減少
// 原因:
// xy == 1 (mod m)
// xy = 1+km
// xy(2-xy) = (1+km)(1 -km)
// x[y(2-xy)] = 1-k^2m^2
// x[y(2-xy)] == 1 (mod m^2)
// 如果y 是1/x mod m,則y(2-xy) 是1/x mod m^2
// 應在每一步將x 和y(2-xy) 減少m^2保持大小有限。
// JS 的乘法「溢位」與 C/C++ 不同,所以這裡需要小心。
function bnpInvDigit() {
if(this.t var x = this[0];
if((x&1) == 0) 回傳0;
var y = x&3; // y == 1/x mod 2^2
y = (y*(2-(x&0xf)*y))&0xf; // y == 1/x mod 2^4
y = (y*(2-(x&0xff)*y))&0xff; // y == 1/x mod 2^8
y = (y*(2-((x&0xffff)*y )&0xffff)))&0xffff; // y == 1/x mod 2^16
// 最後一步- 直接計算逆模DV;
// 假設16 y = (y*(2-x*y%this.DV))%this.DV; // y == 1/x mod 2^dbits
// 我們確實想要負逆,且-DV return (y>0)?this.DV-y:-y;
}
// 蒙哥馬利約簡
function Montgomery(m) {
this.m = m;
this.mp = m.invDigit();
this.mpl = this.mp&0x7fff;
this.mph = this.mp>>15;
this. um = (1this.mt2 = 2*m.t;
}
// xR mod m
function montConvert(x) {
var r = nbi();
x.abs().dlShiftTo(this.m.t,r);
r.divRemTo(this.m,null,r);
if(x.s 0) this.m.subTo(r,r);
返回r;
}
// x/R mod m
function montRevert( x) {
var r = nbi();
x.copyTo(r);
this.reduce(r);
回傳r;
}
// x = x/R mod m (HAC 14.32)
function montReduce(x) {
while(x.t x[x. t++] = 0;
for(var i = 0; i // 更快的計算方法u0 = x[i]*mp mod DV
var j = x[i ]&0x7fff;
var u0 = (j*this.mpl+(((j*this.mph+(x[i]>>15)*this.mpl)&this.um)// 使用am 將乘法-移位-加法合併為一個呼叫
j = i+this.m.t;
x[j] += this.m.am(0 ,u0,x,i,0,this.m.t);
// 傳播進位
while(x[j] >= x.DV) { x[j] -= x.DV; x[+ +j]++; }
}
x.clamp();
x.drShiftTo(this.m.t,x);
if(x.compareTo(this.m) >= 0 ) x.subTo(this.m,x);
}
// r = "x^2/R mod m"; x != r
function montSqrTo(x,r) { x. squareTo(r); this.reduce(r); }
// r = "xy/R mod m"; x,y != r
function montMulTo(x,y,r) { x.multiplyTo (y,r); this.reduce(r); }
Montgomery.prototype.convert = montConvert;
Montgomery.prototype.revert = montRevert;
Montgomery.prototype.reduce = montReduce; Montgomery.prototype.mulTo = montMulTo;
Montgomery.prototype.sqrTo = montSqrTo;
//(受保護)真,當且僅當這是偶數
function bnpIsEven() { return ((this. t>0)?(this[0]&1):this.s) == 0; }
//(受保護)this^e, e function bnpExp(e,z) {
if(e > 0xffffffff || e var r = nbi(), r2 = nbi(), g = z.convert(this), i = nbits(e)-1;
g.copyTo(r);
while(--i >= 0) {
z.sqrTo(r,r2 );
if((e&(1 0) z.mulTo(r2,g,r);
else { var t = r; r = r2; r2 = t; }
}
return z.revert(r);
}
//(公)this^e % m, 0 函數bnModPowInt(e,m) {
var z;
if(e 回傳this.exp(e, z);
}
// 受保護
BigInteger.prototype.copyTo = bnpCopyTo;
BigInteger.prototype.fromInt = bnpFromInt;
BigInteger.prototype.fromString = bnpFromString;
BigInteger.prototype.clamp = bnpClamp;
BigInteger.prototype.dlShiftTo = bnpDLShiftTo;
BigInteger.prototype.drShiftTo = bnpDRShiftTo;
BigInteger.prototype.lShiftTo = bnpLShiftTo;
BigInteger.prototype.rShiftTo = bnpRShiftTo;
BigInteger.prototype.subTo = bnpSubTo;
BigInteger.prototype.multiplyTo = bnpMultiplyTo;
BigInteger.prototype.squareTo = bnpSquareTo;
BigInteger.prototype.divRemTo = bnpDivRemTo;
BigInteger.prototype.invDigit = bnpInvDigit;
BigInteger.prototype.isEven = bnpIsEven;
BigInteger.prototype.exp = bnpExp;
// public
BigInteger.prototype.toString = bnToString;
BigInteger.prototype.negate = bnNegate;
BigInteger.prototype.abs = bnAbs;
BigInteger.prototype.compareTo = bnCompareTo;
BigInteger.prototype.bitLength = bnBitLength;
BigInteger.prototype.mod = bnMod;
BigInteger.prototype.modPowInt = bnModPowInt;
//「常數」
BigInteger.ZERO = nbv(0);
BigInteger.ONE = nbv(1);
#檔案prng4.js
// prng4.js - 使用 Arcfour 作為 PRNG
function Arcfour() {
this.i = 0;
this.j = 0;
this.S = new Array();
}
// 從key 初始化arcfour 上下文,key 是一個位元組數組,每個都來自[0..255]
function ARC4init(key) {
var i, j, t ;
for(i = 0; i this.S[i] = i;
j = 0;
for(i = 0; i j = (j + this.S[i] + key[i % key.length]) & 255;
t = this.S[i];
this.S[i] = this.S[j];
this.S[j] = t;
}
this.i = 0;
this.j = 0;
}
函數 ARC4next() {
var t;
這個.i = (這個.i + 1) & 255;
這個.j = (這個.j + 這個.S[這個.i]) & 255;
t = this.S[this.i];
這個.S[這個.i] = 這個.S[這個.j];
this.S[this.j] = t;
回傳 this.S[(t + this.S[this.i]) & 255];
}
Arcfour.prototype.init = ARC4init;
Arcfour.prototype.next = ARC4next;
// 在此插入 RNG 建構子
function prng_newstate() {
return new Arcfour();
}
// 池大小必須是 4 的倍數且大於 32。
檔案:rng.js
//隨機數位產生器 - 需要 PRNG 桌面,例如prng4.js
//為了獲得最佳效果,請輸入類似
//