首页 > php教程 > php手册 > 正文

PHP JS rsa数据加密传输实现代码

WBOY
发布: 2016-06-06 20:36:22
原创
1262 人浏览过

PHP JS rsa数据加密传输实现代码,需要的朋友可以参考下。

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 BN 库 - 对于 RSA 加密有用的子集。
// 每位数位数
var dbits;
// 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 是初始进位,返回最终进位。
// 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 完全避免了大的乘法和提取。
// 最大位数应为 // 最大为 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 位数字时速度会变慢。
函数 am3(i,x,w,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.charAt(n); }
function intAt(s,i) {
var c = BI_RC[s.charCodeAt(i)];
返回(c==null)?-1:c;
}
//(受保护)将其复制到 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 函数 bnpFromInt(x) {
this.t = 1;
this.s = (xif(x > 0) this[0] = x;
else if(x 否则这个.t = 0;
}
// 返回初始化为值的 bigint
function nbv(i) { var r = nbi(); r.fromInt(i);返回 r; }
//(受保护)从字符串和基数设置
function bnpFromString(s,b) {
var k;
if(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);返回; }
this.t = 0;
this.s = 0;
var i = s.length, mi = false, sh = 0;
while(--i >= 0) {
var x = (k==8)?s[i]&0xff:intAt(s,i);
if(x if(s.charAt(i) == "-") mi = true;
继续;
}
mi = 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
这个[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(mi) BigInteger.ZERO.subTo(this,this);
}
//(受保护)夹掉多余的高位字
function bnpClamp() {
var c = this.s&this.DM;
while(this.t > 0 && this[this.t-1] == c) --this.t;
}
//(公共)返回给定基数的字符串表示
function bnToString(b) {
if(this.s var k;
if(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";
}
//(公共)-this
function bnNegate() { var r = nbi(); BigInteger.ZERO.subTo(this,r);返回 r; }
//(公共)|this|
function bnAbs() { return (this.s//(公共)如果此>则返回a, - 如果这function bnCompareTo(a) {
var r = this.s-a.s;
if(r != 0) 返回 r;
var i = this.t;
r = i-a.t;
if(r != 0) 返回 r;
while(--i >= 0) if((r=this[i]-a[i]) != 0) return r;
返回0;
}
// 返回整数 x 的位长度
function nbits(x) {
var r = 1, t;
if((t=x>>16) != 0) { x = t; r = 16; }
if((t=x>>8) != 0) { x = t; r = 8; }
if((t=x>>4) != 0) { x = t; r = 4; }
if((t=x>>2) != 0) { x = t; r = 2; }
if((t=x>>1) != 0) { x = t; r = 1; }
返回 r;
}
//(公共)返回“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;
c>>= this.DB;
}
if(a.t c -= a.s;
while(i c = this[i];
r[i] = c&this.DM;
c>>= this.DB;
}
c = this.s;
}
else {
c = this.s;
while(i c -= a[i];
r[i] = c&this.DM;
c>>= this.DB;
}
c -= a.s;
}
r.s = (cif(c 否则 if(c > 0) r[i ] = c;
r.t = i;
r.clamp();
}
//(受保护)r = this * a, r != this,a (HAC 14.12)
// 如果合适,“this”应该是较大的那个。
function bnpMultiplyTo(a,r) {
var x = this.abs(), y = a.abs();
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。 q 或 r 可以为空。
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);
返回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;
this.j = (this.j this.S[this.i]) & 255;
t = this.S[this.i];
这个.S[这个.i] = 这个.S[这个.j];
this.S[this.j] = t;
返回这个.S[(t 这个.S[这个.i]) & 255];
}
Arcfour.prototype.init = ARC4init;
Arcfour.prototype.next = ARC4next;
// 在此处插入 RNG 构造函数
function prng_newstate() {
return new Arcfour();
}
// 池大小必须是 4 的倍数且大于 32。
// 池大小的字节数组将传递给 init()
var rng_psize = 256 ;
文件:rng.js
// 随机数生成器 - 需要 PRNG 后端,例如prng4.js
// 为了获得最佳结果,请输入类似
// ;
// 在你的主 HTML 文档中。
var rng_state;
var rng_pool;
var rng_pptr;
// 混入一个 32 位整数到池中
function rng_seed_int(x) {
rng_pool[rng_pptr ] ^= x & 255;
rng_pool[rng_pptr] ^= (x>>8) & 255;
rng_pool[rng_pptr] ^= (x>>16) & 255;
rng_pool[rng_pptr] ^= (x>>24) & 255;
if(rng_pptr >= rng_psize) rng_pptr -= rng_psize;
}
// 将当前时间(带毫秒)混合到池中
function rng_seed_time() {
rng_seed_int(new Date().getTime());
}
// 如果需要,用垃圾初始化池。
if(rng_pool == null) {
rng_pool = new Array();
rng_pptr = 0;
var t;
if(navigator.appName == "Netscape" && navigator.appVersion // 从 NS4 RNG 中提取熵(256 位)(如果可用)
var z = window.crypto.random(32);
for(t = 0; t rng_pool[rng_pptr ] = z.charCodeAt(t) & 255;
}
while(rng_pptr t = Math.floor(65536 * Math.random());
rng_pool[rng_pptr] = t>>>> 8;
rng_pool[rng_pptr] = t & 255;
}
rng_pptr = 0;
rng_seed_time();
//rng_seed_int(window.screenX);
//rng_seed_int(window.screenY);
}
函数 rng_get_byte() {
if(rng_state == null) {
rng_seed_time();
rng_state = prng_newstate();
rng_state.init(rng_pool);
for(rng_pptr = 0; rng_pptr rng_pool[rng_pptr] = 0;
rng_pptr = 0;
//rng_pool = null;
}
// TODO:允许在第一次请求后重新播种
return rng_state.next();
}
function rng_get_bytes(ba) {
var i;
for(i = 0; i }
函数 SecureRandom() {}
SecureRandom.prototype.nextBytes = rng_get_bytes;
#文件:rsa.js
// 依赖于 jsbn.js 和 rng.js
// 版本 1.1:支持 pkcs1pad2 中的 utf-8 编码
// 将(十六进制)字符串转换为一个 bignum 对象
function parseBigInt(str,r) {
return new BigInteger(str,r);
}
函数 linebrk(s,n) {
var ret = "";
var i = 0;
while(i n ret = s.substring(i,i n) "n";
i = n;
}
返回 ret s.substring(i,s.length);
}
function byte2Hex(b) {
if(b return "0" b.toString(16);
否则
返回b.toString(16);
}
// PKCS#1(类型 2,随机)将输入字符串 s 填充为 n 个字节,并返回一个 bigint
function pkcs1pad2(s,n) {
if(n alert("Message too long for RSA");
返回空;
}
var ba = new Array();
var i = s.length - 1;
while(i >= 0 && n > 0) {
var c = s.charCodeAt(i--);
if(c ba[--n] = c;
}
else if((c > 127) && (c ba[--n] = (c & 63) | 128;
ba[--n] = (c >> 6) | 192;
}
else {
ba[--n] = (c & 63) | 128;
ba[--n] = ((c >> 6) & 63) | 128;
ba[--n] = (c >> 12) | 224;
}
}
ba[--n] = 0;
var rng = new SecureRandom();
var x = new Array();
while(n > 2) { // 随机非零填充
x[0] = 0;
while(x[0] == 0) rng.nextBytes(x);
ba[--n] = x[0];
}
ba[--n] = 2;
ba[--n] = 0;
返回新的BigInteger(ba);
}
//“空”RSA 密钥构造函数
function RSAKey() {
this.n = null;
这个.e = 0;
this.d = null;
this.p = null;
this.q = null;
this.dmp1 = null;
this.dmq1 = null;
this.coeff = null;
}
// 从十六进制字符串设置公钥字段 N 和 e
function RSASetPublic(N,E) {
if(N != null && E != null && N.length > ; 0 && E.length > 0) {
this.n = parseBigInt(N,16);
this.e = parseInt(E,16);
}
else
alert("无效的 RSA 公钥");
}
// 对“x”执行原始公共操作: return x^e (mod n)
function RSADoPublic(x) {
return x.modPowInt(this.e, this.n) );
}
// 将“text”的 PKCS#1 RSA 加密作为偶数长度的十六进制字符串返回
function RSAEncrypt(text) {
var m = pkcs1pad2(text,(this.n) .bitLength() 7)>>3);
if(m == null) 返回 null;
var c = this.doPublic(m);
if(c == null) 返回 null;
var h = c.toString(16);
if((h.length & 1) == 0) return h;否则返回“0”h;
}
// 将“text”的 PKCS#1 RSA 加密作为 Base64 编码的字符串返回
//function RSAEncryptB64(text) {
// var h = this.encrypt(text) );
// if(h) return hex2b64(h);否则返回空;
/
相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门推荐
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!