搜索
首页php教程php手册PHP JS rsa数据加密传输实现代码

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);否则返回空;
/
声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
php怎么把负数转为正整数php怎么把负数转为正整数Apr 19, 2022 pm 08:59 PM

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

php怎么实现几秒后执行一个函数php怎么实现几秒后执行一个函数Apr 24, 2022 pm 01:12 PM

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php字符串有没有下标php字符串有没有下标Apr 24, 2022 am 11:49 AM

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

php怎么除以100保留两位小数php怎么除以100保留两位小数Apr 22, 2022 pm 06:23 PM

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

php怎么读取字符串后几个字符php怎么读取字符串后几个字符Apr 22, 2022 pm 08:31 PM

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

php怎么根据年月日判断是一年的第几天php怎么根据年月日判断是一年的第几天Apr 22, 2022 pm 05:02 PM

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php怎么替换nbsp空格符php怎么替换nbsp空格符Apr 24, 2022 pm 02:55 PM

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\&nbsp\;||\xc2\xa0)/","其他字符",$str)”语句。

php怎么查找字符串是第几位php怎么查找字符串是第几位Apr 22, 2022 pm 06:48 PM

查找方法:1、用strpos(),语法“strpos("字符串值","查找子串")+1”;2、用stripos(),语法“strpos("字符串值","查找子串")+1”。因为字符串是从0开始计数的,因此两个函数获取的位置需要进行加1处理。

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
3 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
3 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您听不到任何人,如何修复音频
3 周前By尊渡假赌尊渡假赌尊渡假赌

热工具

SublimeText3 英文版

SublimeText3 英文版

推荐:为Win版本,支持代码提示!

螳螂BT

螳螂BT

Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

mPDF

mPDF

mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版