您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息
免费发信息
三六零分类信息网 > 张家界分类信息网,免费分类信息发布

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

2024/2/22 12:31:44发布23次查看
js端代码:
复制代码 代码如下:
//文件base64.js:
var b64map=abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789+/;
var b64pad==;
function 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;
return ret;
}
// convert a base64 string to hex
function b64tohex(s) {
var ret =
var i;
var k = 0; // b64 state, 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);
slop = v & 3;
k = 1;
}
else if(k == 1) {
ret += int2char((slop > 4));
slop = v & 0xf;
k = 2;
}
else if(k == 2) {
ret += int2char(slop);
ret += int2char(v >> 2);
slop = v & 3;
k = 3;
}
else {
ret += int2char((slop > 4));
ret += int2char(v & 0xf);
k = 0;
}
}
if(k == 1)
ret += int2char(slop return ret;
}
// convert a base64 string to a byte/number array
function b64toba(s) {
//piggyback on b64tohex for now, optimize later
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);
}
return a;
}
#文件jsbn.js
// copyright (c) 2005 tom wu
// all rights reserved.
// see license for details.
// basic javascript bn library - subset useful for rsa encryption.
// bits per digit
var dbits;
// javascript engine analysis
var canary = 0xdeadbeefcafe;
var j_lm = ((canary&0xffffff)==0xefcafe);
// (public) constructor
function biginteger(a,b,c) {
if(a != null)
if(number == typeof a) this.fromnumber(a,b,c);
else if(b == null && string != typeof a) this.fromstring(a,256);
else this.fromstring(a,b);
}
// return new, unset biginteger
function nbi() { return new biginteger(null); }
// am: compute w_j += (x*this_i), propagate carries,
// c is initial carry, returns final carry.
// c // we need to select the fastest one that works in this environment.
// am1: use a single mult and divide to get the high bits,
// max digit bits should be 26 because
// max internal value = 2*dvalue^2-2*dvalue (function 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;
}
return c;
}
// am2 avoids a big mult-and-extract completely.
// max digit bits should be // on values up to 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;
}
return c;
}
// alternately, set max digit bits to 28 since some
// browsers slow down when dealing with 32-bit numbers.
function 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&0xfffffff;
}
return 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 seems to prefer 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;
// digit conversions
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)];
return (c==null)?-1:c;
}
// (protected) copy this to r
function bnpcopyto(r) {
for(var i = this.t-1; i >= 0; --i) r[i] = this[i];
r.t = this.t;
r.s = this.s;
}
// (protected) set from integer value x, -dv function bnpfromint(x) {
this.t = 1;
this.s = (xif(x > 0) this[0] = x;
else if(x else this.t = 0;
}
// return bigint initialized to value
function nbv(i) { var r = nbi(); r.fromint(i); return r; }
// (protected) set from string and radix
function bnpfromstring(s,b) {
var k;
if(b == 16) k = 4;
else if(b == 8) k = 3;
else if(b == 256) k = 8; // byte array
else if(b == 2) k = 1;
else if(b == 32) k = 5;
else if(b == 4) k = 2;
else { this.fromradix(s,b); return; }
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;
continue;
}
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[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);
}
// (protected) clamp off excess high words
function bnpclamp() {
var c = this.s&this.dm;
while(this.t > 0 && this[this.t-1] == c) --this.t;
}
// (public) return string representation in given radix
function bntostring(b) {
if(this.s var k;
if(b == 16) k = 4;
else if(b == 8) k = 3;
else if(b == 2) k = 1;
else if(b == 32) k = 5;
else if(b == 4) k = 2;
else return 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;
}
// (public) -this
function bnnegate() { var r = nbi(); biginteger.zero.subto(this,r); return r; }
// (public) |this|
function bnabs() { return (this.s// (public) return + if this > a, - if this function bncompareto(a) {
var r = this.s-a.s;
if(r != 0) return r;
var i = this.t;
r = i-a.t;
if(r != 0) return r;
while(--i >= 0) if((r=this[i]-a[i]) != 0) return r;
return 0;
}
// returns bit length of the integer 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; }
return r;
}
// (public) return the number of bits in this
function bnbitlength() {
if(this.t return this.db*(this.t-1)+nbits(this[this.t-1]^(this.s&this.dm));
}
// (protected) 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;
}
// (protected) 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;
}
// (protected) 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();
}
// (protected) r = this >> n
function bnprshiftto(n,r) {
r.s = this.s;
var ds = math.floor(n/this.db);
if(ds >= this.t) { r.t = 0; return; }
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();
}
// (protected) 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 else if(c > 0) r[i++] = c;
r.t = i;
r.clamp();
}
// (protected) r = this * a, r != this,a (hac 14.12)
// this should be the larger one if appropriate.
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);
}
// (protected) 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();
}
// (protected) divide this by m, quotient and remainder to q, r (hac 14.20)
// r != q, this != m. q or r may be null.
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);
return;
}
if(r == null) r = nbi();
var y = nbi(), ts = this.s, ms = m.s;
var nsh = this.db-nbits(pm[pm.t-1]); // normalize modulus
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) return;
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); // negative y so we can replace sub with am later
while(y.t while(--j >= 0) {
// estimate quotient digit
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); // denormalize remainder
if(ts }
// (public) this mod a
function bnmod(a) {
var r = nbi();
this.abs().divremto(a,null,r);
if(this.s 0) a.subto(r,r);
return r;
}
// modular reduction using classic algorithm
function classic(m) { this.m = m; }
function cconvert(x) {
if(x.s = 0) return x.mod(this.m);
else return x;
}
function crevert(x) { return x; }
function creduce(x) { x.divremto(this.m,null,x); }
function cmulto(x,y,r) { x.multiplyto(y,r); this.reduce(r); }
function 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;
// (protected) return -1/this % 2^db; useful for mont. reduction
// justification:
// 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)
// if y is 1/x mod m, then y(2-xy) is 1/x mod m^2
// should reduce x and y(2-xy) by m^2 at each step to keep size bounded.
// js multiply overflows differently from c/c++, so care is needed here.
function bnpinvdigit() {
if(this.t var x = this[0];
if((x&1) == 0) return 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
// last step - calculate inverse mod dv directly;
// assumes 16 y = (y*(2-x*y%this.dv))%this.dv; // y == 1/x mod 2^dbits
// we really want the negative inverse, and -dv return (y>0)?this.dv-y:-y;
}
// montgomery reduction
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);
return r;
}
// x/r mod m
function montrevert(x) {
var r = nbi();
x.copyto(r);
this.reduce(r);
return r;
}
// x = x/r mod m (hac 14.32)
function montreduce(x) {
while(x.t x[x.t++] = 0;
for(var i = 0; i // faster way of calculating 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)// use am to combine the multiply-shift-add into one call
j = i+this.m.t;
x[j] += this.m.am(0,u0,x,i,0,this.m.t);
// propagate carry
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;
// (protected) true iff this is even
function bnpiseven() { return ((this.t>0)?(this[0]&1):this.s) == 0; }
// (protected) 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);
}
// (public) this^e % m, 0 function bnmodpowint(e,m) {
var z;
if(e return this.exp(e,z);
}
// protected
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;
// constants
biginteger.zero = nbv(0);
biginteger.one = nbv(1);
#文件prng4.js
// prng4.js - uses arcfour as a prng
function arcfour() {
this.i = 0;
this.j = 0;
this.s = new array();
}
// initialize arcfour context from key, an array of ints, each from [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;
}
function arc4next() {
var t;
this.i = (this.i + 1) & 255;
this.j = (this.j + this.s[this.i]) & 255;
t = this.s[this.i];
this.s[this.i] = this.s[this.j];
this.s[this.j] = t;
return this.s[(t + this.s[this.i]) & 255];
}
arcfour.prototype.init = arc4init;
arcfour.prototype.next = arc4next;
// plug in your rng constructor here
function prng_newstate() {
return new arcfour();
}
// pool size must be a multiple of 4 and greater than 32.
// an array of bytes the size of the pool will be passed to init()
var rng_psize = 256;
文件:rng.js
// random number generator - requires a prng backend, e.g. prng4.js
// for best results, put code like
//
// read the public exponent
$public = (int) expect($raw[$i], publicexponent: );
// read the private exponent
expect($raw[$i + 1], privateexponent:);
for($i += 2; $raw[$i][0] == ' '; $i++) $privateraw .= trim($raw[$i]);
// just to make sure
expect($raw[$i], prime1:);
// conversion to decimal format for bcmath
$modulus = bc_hexdec($modulusraw);
$private = bc_hexdec($privateraw);
return array($keylength, $modulus['php'], $public, $private['php'],$modulus['js'], $private['js']);
}
/*
* convert a hexadecimal number of the form xx:yy:zz:... to decimal
* uses bcmath, but the standard normal hexdec function for the components
*/
function bc_hexdec($hex)
{
$coefficients = explode(:, $hex);
$result_js= implode(,$coefficients);
$i = 0;
$result = 0;
foreach(array_reverse($coefficients) as $coefficient)
{
$mult = bcpow(256, $i++);
$result = bcadd($result, bcmul(hexdec($coefficient), $mult));
}
return array('php'=>$result,'js'=>$result_js);
}
/*
* if the string has the given prefix, return the remainder.
* if not, die with an error
*/
function expect($str, $prefix)
{
if(substr($str, 0, strlen($prefix)) == $prefix)
return substr($str, strlen($prefix));
else
die(error: expected $prefix);
}
整套加密及解密的方法都在上面了,本人的测试环境为php5.3+win7
上面所有文件下载:rsafile 以上就介绍了数据加密 php+js+rsa数据加密传输实现代码,包括了数据加密方面的内容,希望对php教程有兴趣的朋友有所帮助。
张家界分类信息网,免费分类信息发布

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录