zoukankan      html  css  js  c++  java
  • 页面登录密码加密传输机制

     为了避免用户密码使用明文传输,在传输前需要进行加密处理,服务端接收之后再进行解密,这样从一定程度上保护了密码安全。

            基本过程如下:

            显然,采用非对称加密更合适,这里我们使用RSA加密算法。

            需要三方面的东东。

            第一样,服务器端RSA加解密工具类及KEY定义。

            第二样,Web端RSA加密工具类,支持根据公钥加密。

            第三样,用户登录时的处理脚本。

            赋代码是直接的

    Case one

     1 namespace Tool.Security
     2 {
     3     /// <summary>
     4     /// 对公钥的提供和对解密的支持
     5     /// </summary>
     6     static class RSAHelper
     7     {
     8         private static string ByteToHex(byte[] byteArray)
     9         {
    10             string outString = "";
    11             foreach (Byte b in byteArray)
    12             {
    13                 outString += b.ToString("X2");
    14             }
    15             return outString;
    16         }
    17 
    18         private static byte[] HexToByte(string hexString)
    19         {
    20             byte[] returnBytes = new byte[hexString.Length / 2];
    21             for (int i = 0; i < returnBytes.Length; i++)
    22             {
    23                 returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
    24             }
    25             return returnBytes;
    26         }
    27 
    28         private static RSACryptoServiceProvider GetRSA()
    29         {
    30             CspParameters _cpsParameter = new CspParameters();
    31             _cpsParameter.Flags = CspProviderFlags.UseMachineKeyStore;
    32 
    33             RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(1024, _cpsParameter);
    34 
    35             var keyString = @"<RSAKeyValue><Modulus>ucNbt0w1Epc/Cs7sgUIvz5GH/s6qe5REAQUrAwo3yFZ/ZElY11urTAQ/CctHppJ8dCplxM5yjHUG+XGlYM700IIYPQaylUKNIRUzdP/Ov3wMfakfHFeItwmNhbVDjicfMPlrnQtwv4hlxMMIfDAKD7TbuwzNyLrmACKGfnXclOM=</Modulus><Exponent>AQAB</Exponent><P>+hh9uGmx99hDlYMm9X6s4oxWomKFgIFDWw1gTigL1A21yEF0t0lyd1ehIUvJMg9H1UxOQnLMIXViMjrRajXT8w==</P><Q>viYPHwxXIUCvzq/SU6CcOnizyXpOR+yL6GrYrbxFCVxvlCdI59P9yXeObVWEbLhp4N+tpMNW4nnLtTgU/WunUQ==</Q><DP>LXYQPFpyMFROjLoHde6t7IyIHwJ7tA1LOhznWa2r4MMRPTKwzbR7KzhxoMTO8NuYOiyhuTB27MoSrIDGmmDogw==</DP><DQ>HGVcnrteC6Y4Wvpgw4JZslWyK6VrKUyG/DdwkklqWeKG+y3+sjgzTzHegC9kLkQ/84gTy2YshHfB1xc/8zRvcQ==</DQ><InverseQ>s4UFcRxlke8UZC5wMpaPgBGUR4DjauLdLtPZV/KFaFjquwAS/Mf1Oguz66afLG6AFUsmevuITyT7/Dq9DnurPg==</InverseQ><D>s4EtAm34pxNSDv6vmaQ8kSZ/NOG96hb2s8mrq49XIhc7mMwrtkdVuuorhCpT/Yl2C8bgepJILKP1blu8RFUYSS7SVab/qi1wn6p33hRFb2waxef/QiBPDoLiGDbU6mNsOGGr4fxKcMSrpVhzEJO7fxrZ9yq7ZJ3t2m7N21R9FaE=</D></RSAKeyValue>";
    36             rsa.FromXmlString(keyString);
    37             return rsa;
    38         }
    39 
    40         public static Publickey GetPublickey()
    41         {
    42             Publickey publickey = new Publickey();
    43             RSACryptoServiceProvider rsa = GetRSA();
    44             RSAParameters rp = rsa.ExportParameters(false);
    45             publickey.Exponent = ByteToHex(rp.Exponent);
    46             publickey.Modulus = ByteToHex(rp.Modulus);
    47             return publickey;
    48         }
    49 
    50         public static string Decrypt(string s)
    51         {
    52             RSACryptoServiceProvider rsa = GetRSA();
    53             return Encoding.Default.GetString(rsa.Decrypt(HexToByte(s), false));
    54         }
    55     }
    56 
    57     class RSAKey
    58     {
    59         public string XmlString { get; set; }
    60     }
    61 
    62     class Publickey
    63     {
    64         public string Modulus { get; set; }
    65         public string Exponent { get; set; }
    66     }
    67 }

    Case Two

      1 var dbits;
      2 var canary = 0xdeadbeefcafe;
      3 var j_lm = ((canary & 0xffffff) == 0xefcafe);
      4 function BigInteger(a, b, c) {
      5     if (a != null)
      6         if ("number" == typeof a) this.fromNumber(a, b, c);
      7     else if (b == null && "string" != typeof a) this.fromString(a, 256);
      8     else this.fromString(a, b);
      9 }
     10 function nbi() { return new BigInteger(null); }
     11 function am1(i, x, w, j, c, n) {
     12     while (--n >= 0) {
     13         var v = x * this[i++] + w[j] + c;
     14         c = Math.floor(v / 0x4000000);
     15         w[j++] = v & 0x3ffffff;
     16     }
     17     return c;
     18 }
     19 function am2(i, x, w, j, c, n) {
     20     var xl = x & 0x7fff, xh = x >> 15;
     21     while (--n >= 0) {
     22         var l = this[i] & 0x7fff;
     23         var h = this[i++] >> 15;
     24         var m = xh * l + h * xl;
     25         l = xl * l + ((m & 0x7fff) << 15) + w[j] + (c & 0x3fffffff);
     26         c = (l >>> 30) + (m >>> 15) + xh * h + (c >>> 30);
     27         w[j++] = l & 0x3fffffff;
     28     }
     29     return c;
     30 }
     31 function am3(i, x, w, j, c, n) {
     32     var xl = x & 0x3fff, xh = x >> 14;
     33     while (--n >= 0) {
     34         var l = this[i] & 0x3fff;
     35         var h = this[i++] >> 14;
     36         var m = xh * l + h * xl;
     37         l = xl * l + ((m & 0x3fff) << 14) + w[j] + c;
     38         c = (l >> 28) + (m >> 14) + xh * h;
     39         w[j++] = l & 0xfffffff;
     40     }
     41     return c;
     42 }
     43 if (j_lm && (navigator.appName == "Microsoft Internet Explorer")) {
     44     BigInteger.prototype.am = am2;
     45     dbits = 30;
     46 }
     47 else if (j_lm && (navigator.appName != "Netscape")) {
     48     BigInteger.prototype.am = am1;
     49     dbits = 26;
     50 }
     51 else {
     52     BigInteger.prototype.am = am3;
     53     dbits = 28;
     54 }
     55 BigInteger.prototype.DB = dbits;
     56 BigInteger.prototype.DM = ((1 << dbits) - 1);
     57 BigInteger.prototype.DV = (1 << dbits);
     58 var BI_FP = 52;
     59 BigInteger.prototype.FV = Math.pow(2, BI_FP);
     60 BigInteger.prototype.F1 = BI_FP - dbits;
     61 BigInteger.prototype.F2 = 2 * dbits - BI_FP;
     62 var BI_RM = "0123456789abcdefghijklmnopqrstuvwxyz";
     63 var BI_RC = new Array();
     64 var rr, vv;
     65 rr = "0".charCodeAt(0);
     66 for (vv = 0; vv <= 9; ++vv) BI_RC[rr++] = vv;
     67 rr = "a".charCodeAt(0);
     68 for (vv = 10; vv < 36; ++vv) BI_RC[rr++] = vv;
     69 rr = "A".charCodeAt(0);
     70 for (vv = 10; vv < 36; ++vv) BI_RC[rr++] = vv;
     71 function int2char(n) { return BI_RM.charAt(n); }
     72 function intAt(s, i) {
     73     var c = BI_RC[s.charCodeAt(i)];
     74     return (c == null) ? -1 : c;
     75 }
     76 function bnpCopyTo(r) {
     77     for (var i = this.t - 1; i >= 0; --i) r[i] = this[i];
     78     r.t = this.t;
     79     r.s = this.s;
     80 }
     81 function bnpFromInt(x) {
     82     this.t = 1;
     83     this.s = (x < 0) ? -1 : 0;
     84     if (x > 0) this[0] = x;
     85     else if (x < -1) this[0] = x + DV;
     86     else this.t = 0;
     87 }
     88 function nbv(i) { var r = nbi(); r.fromInt(i); return r; }
     89 function bnpFromString(s, b) {
     90     var k;
     91     if (b == 16) k = 4;
     92     else if (b == 8) k = 3;
     93     else if (b == 256) k = 8;
     94     else if (b == 2) k = 1;
     95     else if (b == 32) k = 5;
     96     else if (b == 4) k = 2;
     97     else { this.fromRadix(s, b); return; }
     98     this.t = 0;
     99     this.s = 0;
    100     var i = s.length, mi = false, sh = 0;
    101     while (--i >= 0) {
    102         var x = (k == 8) ? s[i] & 0xff : intAt(s, i);
    103         if (x < 0) {
    104             if (s.charAt(i) == "-") mi = true;
    105             continue;
    106         }
    107         mi = false;
    108         if (sh == 0)
    109             this[this.t++] = x;
    110         else if (sh + k > this.DB) {
    111             this[this.t - 1] |= (x & ((1 << (this.DB - sh)) - 1)) << sh;
    112             this[this.t++] = (x >> (this.DB - sh));
    113         }
    114         else
    115             this[this.t - 1] |= x << sh;
    116         sh += k;
    117         if (sh >= this.DB) sh -= this.DB;
    118     }
    119     if (k == 8 && (s[0] & 0x80) != 0) {
    120         this.s = -1;
    121         if (sh > 0) this[this.t - 1] |= ((1 << (this.DB - sh)) - 1) << sh;
    122     }
    123     this.clamp();
    124     if (mi) BigInteger.ZERO.subTo(this, this);
    125 }
    126 function bnpClamp() {
    127     var c = this.s & this.DM;
    128     while (this.t > 0 && this[this.t - 1] == c) --this.t;
    129 }
    130 function bnToString(b) {
    131     if (this.s < 0) return "-" + this.negate().toString(b);
    132     var k;
    133     if (b == 16) k = 4;
    134     else if (b == 8) k = 3;
    135     else if (b == 2) k = 1;
    136     else if (b == 32) k = 5;
    137     else if (b == 4) k = 2;
    138     else return this.toRadix(b);
    139     var km = (1 << k) - 1, d, m = false, r = "", i = this.t;
    140     var p = this.DB - (i * this.DB) % k;
    141     if (i-- > 0) {
    142         if (p < this.DB && (d = this[i] >> p) > 0) { m = true; r = int2char(d); }
    143         while (i >= 0) {
    144             if (p < k) {
    145                 d = (this[i] & ((1 << p) - 1)) << (k - p);
    146                 d |= this[--i] >> (p += this.DB - k);
    147             }
    148             else {
    149                 d = (this[i] >> (p -= k)) & km;
    150                 if (p <= 0) { p += this.DB; --i; }
    151             }
    152             if (d > 0) m = true;
    153             if (m) r += int2char(d);
    154         }
    155     }
    156     return m ? r : "0";
    157 }
    158 function bnNegate() { var r = nbi(); BigInteger.ZERO.subTo(this, r); return r; }
    159 function bnAbs() { return (this.s < 0) ? this.negate() : this; }
    160 function bnCompareTo(a) {
    161     var r = this.s - a.s;
    162     if (r != 0) return r;
    163     var i = this.t;
    164     r = i - a.t;
    165     if (r != 0) return r;
    166     while (--i >= 0) if ((r = this[i] - a[i]) != 0) return r;
    167     return 0;
    168 }
    169 function nbits(x) {
    170     var r = 1, t;
    171     if ((t = x >>> 16) != 0) { x = t; r += 16; }
    172     if ((t = x >> 8) != 0) { x = t; r += 8; }
    173     if ((t = x >> 4) != 0) { x = t; r += 4; }
    174     if ((t = x >> 2) != 0) { x = t; r += 2; }
    175     if ((t = x >> 1) != 0) { x = t; r += 1; }
    176     return r;
    177 }
    178 function bnBitLength() {
    179     if (this.t <= 0) return 0;
    180     return this.DB * (this.t - 1) + nbits(this[this.t - 1] ^ (this.s & this.DM));
    181 }
    182 function bnpDLShiftTo(n, r) {
    183     var i;
    184     for (i = this.t - 1; i >= 0; --i) r[i + n] = this[i];
    185     for (i = n - 1; i >= 0; --i) r[i] = 0;
    186     r.t = this.t + n;
    187     r.s = this.s;
    188 }
    189 function bnpDRShiftTo(n, r) {
    190     for (var i = n; i < this.t; ++i) r[i - n] = this[i];
    191     r.t = Math.max(this.t - n, 0);
    192     r.s = this.s;
    193 }
    194 function bnpLShiftTo(n, r) {
    195     var bs = n % this.DB;
    196     var cbs = this.DB - bs;
    197     var bm = (1 << cbs) - 1;
    198     var ds = Math.floor(n / this.DB), c = (this.s << bs) & this.DM, i;
    199     for (i = this.t - 1; i >= 0; --i) {
    200         r[i + ds + 1] = (this[i] >> cbs) | c;
    201         c = (this[i] & bm) << bs;
    202     }
    203     for (i = ds - 1; i >= 0; --i) r[i] = 0;
    204     r[ds] = c;
    205     r.t = this.t + ds + 1;
    206     r.s = this.s;
    207     r.clamp();
    208 }
    209 function bnpRShiftTo(n, r) {
    210     r.s = this.s;
    211     var ds = Math.floor(n / this.DB);
    212     if (ds >= this.t) { r.t = 0; return; }
    213     var bs = n % this.DB;
    214     var cbs = this.DB - bs;
    215     var bm = (1 << bs) - 1;
    216     r[0] = this[ds] >> bs;
    217     for (var i = ds + 1; i < this.t; ++i) {
    218         r[i - ds - 1] |= (this[i] & bm) << cbs;
    219         r[i - ds] = this[i] >> bs;
    220     }
    221     if (bs > 0) r[this.t - ds - 1] |= (this.s & bm) << cbs;
    222     r.t = this.t - ds;
    223     r.clamp();
    224 }
    225 function bnpSubTo(a, r) {
    226     var i = 0, c = 0, m = Math.min(a.t, this.t);
    227     while (i < m) {
    228         c += this[i] - a[i];
    229         r[i++] = c & this.DM;
    230         c >>= this.DB;
    231     }
    232     if (a.t < this.t) {
    233         c -= a.s;
    234         while (i < this.t) {
    235             c += this[i];
    236             r[i++] = c & this.DM;
    237             c >>= this.DB;
    238         }
    239         c += this.s;
    240     }
    241     else {
    242         c += this.s;
    243         while (i < a.t) {
    244             c -= a[i];
    245             r[i++] = c & this.DM;
    246             c >>= this.DB;
    247         }
    248         c -= a.s;
    249     }
    250     r.s = (c < 0) ? -1 : 0;
    251     if (c < -1) r[i++] = this.DV + c;
    252     else if (c > 0) r[i++] = c;
    253     r.t = i;
    254     r.clamp();
    255 }
    256 function bnpMultiplyTo(a, r) {
    257     var x = this.abs(), y = a.abs();
    258     var i = x.t;
    259     r.t = i + y.t;
    260     while (--i >= 0) r[i] = 0;
    261     for (i = 0; i < y.t; ++i) r[i + x.t] = x.am(0, y[i], r, i, 0, x.t);
    262     r.s = 0;
    263     r.clamp();
    264     if (this.s != a.s) BigInteger.ZERO.subTo(r, r);
    265 }
    266 function bnpSquareTo(r) {
    267     var x = this.abs();
    268     var i = r.t = 2 * x.t;
    269     while (--i >= 0) r[i] = 0;
    270     for (i = 0; i < x.t - 1; ++i) {
    271         var c = x.am(i, x[i], r, 2 * i, 0, 1);
    272         if ((r[i + x.t] += x.am(i + 1, 2 * x[i], r, 2 * i + 1, c, x.t - i - 1)) >= x.DV) {
    273             r[i + x.t] -= x.DV;
    274             r[i + x.t + 1] = 1;
    275         }
    276     }
    277     if (r.t > 0) r[r.t - 1] += x.am(i, x[i], r, 2 * i, 0, 1);
    278     r.s = 0;
    279     r.clamp();
    280 }
    281 function bnpDivRemTo(m, q, r) {
    282     var pm = m.abs();
    283     if (pm.t <= 0) return;
    284     var pt = this.abs();
    285     if (pt.t < pm.t) {
    286         if (q != null) q.fromInt(0);
    287         if (r != null) this.copyTo(r);
    288         return;
    289     }
    290     if (r == null) r = nbi();
    291     var y = nbi(), ts = this.s, ms = m.s;
    292     var nsh = this.DB - nbits(pm[pm.t - 1]);
    293     if (nsh > 0) { pm.lShiftTo(nsh, y); pt.lShiftTo(nsh, r); }
    294     else { pm.copyTo(y); pt.copyTo(r); }
    295     var ys = y.t;
    296     var y0 = y[ys - 1];
    297     if (y0 == 0) return;
    298     var yt = y0 * (1 << this.F1) + ((ys > 1) ? y[ys - 2] >> this.F2 : 0);
    299     var d1 = this.FV / yt, d2 = (1 << this.F1) / yt, e = 1 << this.F2;
    300     var i = r.t, j = i - ys, t = (q == null) ? nbi() : q;
    301     y.dlShiftTo(j, t);
    302     if (r.compareTo(t) >= 0) {
    303         r[r.t++] = 1;
    304         r.subTo(t, r);
    305     }
    306     BigInteger.ONE.dlShiftTo(ys, t);
    307     t.subTo(y, y);
    308     while (y.t < ys) y[y.t++] = 0;
    309     while (--j >= 0) {
    310 
    311         var qd = (r[--i] == y0) ? this.DM : Math.floor(r[i] * d1 + (r[i - 1] + e) * d2);
    312         if ((r[i] += y.am(0, qd, r, j, 0, ys)) < qd) {
    313             y.dlShiftTo(j, t);
    314             r.subTo(t, r);
    315             while (r[i] < --qd) r.subTo(t, r);
    316         }
    317     }
    318     if (q != null) {
    319         r.drShiftTo(ys, q);
    320         if (ts != ms) BigInteger.ZERO.subTo(q, q);
    321     }
    322     r.t = ys;
    323     r.clamp();
    324     if (nsh > 0) r.rShiftTo(nsh, r);
    325     if (ts < 0) BigInteger.ZERO.subTo(r, r);
    326 }
    327 function bnMod(a) {
    328     var r = nbi();
    329     this.abs().divRemTo(a, null, r);
    330     if (this.s < 0 && r.compareTo(BigInteger.ZERO) > 0) a.subTo(r, r);
    331     return r;
    332 }
    333 function Classic(m) { this.m = m; }
    334 function cConvert(x) {
    335     if (x.s < 0 || x.compareTo(this.m) >= 0) return x.mod(this.m);
    336     else return x;
    337 }
    338 function cRevert(x) { return x; }
    339 function cReduce(x) { x.divRemTo(this.m, null, x); }
    340 function cMulTo(x, y, r) { x.multiplyTo(y, r); this.reduce(r); }
    341 function cSqrTo(x, r) { x.squareTo(r); this.reduce(r); }
    342 Classic.prototype.convert = cConvert;
    343 Classic.prototype.revert = cRevert;
    344 Classic.prototype.reduce = cReduce;
    345 Classic.prototype.mulTo = cMulTo;
    346 Classic.prototype.sqrTo = cSqrTo;
    347 function bnpInvDigit() {
    348     if (this.t < 1) return 0;
    349     var x = this[0];
    350     if ((x & 1) == 0) return 0;
    351     var y = x & 3;
    352     y = (y * (2 - (x & 0xf) * y)) & 0xf;
    353     y = (y * (2 - (x & 0xff) * y)) & 0xff;
    354     y = (y * (2 - (((x & 0xffff) * y) & 0xffff))) & 0xffff;
    355 
    356 
    357     y = (y * (2 - x * y % this.DV)) % this.DV;
    358 
    359     return (y > 0) ? this.DV - y : -y;
    360 }
    361 function Montgomery(m) {
    362     this.m = m;
    363     this.mp = m.invDigit();
    364     this.mpl = this.mp & 0x7fff;
    365     this.mph = this.mp >> 15;
    366     this.um = (1 << (m.DB - 15)) - 1;
    367     this.mt2 = 2 * m.t;
    368 }
    369 function montConvert(x) {
    370     var r = nbi();
    371     x.abs().dlShiftTo(this.m.t, r);
    372     r.divRemTo(this.m, null, r);
    373     if (x.s < 0 && r.compareTo(BigInteger.ZERO) > 0) this.m.subTo(r, r);
    374     return r;
    375 }
    376 function montRevert(x) {
    377     var r = nbi();
    378     x.copyTo(r);
    379     this.reduce(r);
    380     return r;
    381 }
    382 function montReduce(x) {
    383     while (x.t <= this.mt2)
    384         x[x.t++] = 0;
    385     for (var i = 0; i < this.m.t; ++i) {
    386 
    387         var j = x[i] & 0x7fff;
    388         var u0 = (j * this.mpl + (((j * this.mph + (x[i] >> 15) * this.mpl) & this.um) << 15)) & x.DM;
    389 
    390         j = i + this.m.t;
    391         x[j] += this.m.am(0, u0, x, i, 0, this.m.t);
    392 
    393         while (x[j] >= x.DV) { x[j] -= x.DV; x[++j]++; }
    394     }
    395     x.clamp();
    396     x.drShiftTo(this.m.t, x);
    397     if (x.compareTo(this.m) >= 0) x.subTo(this.m, x);
    398 }
    399 function montSqrTo(x, r) { x.squareTo(r); this.reduce(r); }
    400 function montMulTo(x, y, r) { x.multiplyTo(y, r); this.reduce(r); }
    401 Montgomery.prototype.convert = montConvert;
    402 Montgomery.prototype.revert = montRevert;
    403 Montgomery.prototype.reduce = montReduce;
    404 Montgomery.prototype.mulTo = montMulTo;
    405 Montgomery.prototype.sqrTo = montSqrTo;
    406 function bnpIsEven() { return ((this.t > 0) ? (this[0] & 1) : this.s) == 0; }
    407 function bnpExp(e, z) {
    408     if (e > 0xffffffff || e < 1) return BigInteger.ONE;
    409     var r = nbi(), r2 = nbi(), g = z.convert(this), i = nbits(e) - 1;
    410     g.copyTo(r);
    411     while (--i >= 0) {
    412         z.sqrTo(r, r2);
    413         if ((e & (1 << i)) > 0) z.mulTo(r2, g, r);
    414         else { var t = r; r = r2; r2 = t; }
    415     }
    416     return z.revert(r);
    417 }
    418 function bnModPowInt(e, m) {
    419     var z;
    420     if (e < 256 || m.isEven()) z = new Classic(m); else z = new Montgomery(m);
    421     return this.exp(e, z);
    422 }
    423 BigInteger.prototype.copyTo = bnpCopyTo;
    424 BigInteger.prototype.fromInt = bnpFromInt;
    425 BigInteger.prototype.fromString = bnpFromString;
    426 BigInteger.prototype.clamp = bnpClamp;
    427 BigInteger.prototype.dlShiftTo = bnpDLShiftTo;
    428 BigInteger.prototype.drShiftTo = bnpDRShiftTo;
    429 BigInteger.prototype.lShiftTo = bnpLShiftTo;
    430 BigInteger.prototype.rShiftTo = bnpRShiftTo;
    431 BigInteger.prototype.subTo = bnpSubTo;
    432 BigInteger.prototype.multiplyTo = bnpMultiplyTo;
    433 BigInteger.prototype.squareTo = bnpSquareTo;
    434 BigInteger.prototype.divRemTo = bnpDivRemTo;
    435 BigInteger.prototype.invDigit = bnpInvDigit;
    436 BigInteger.prototype.isEven = bnpIsEven;
    437 BigInteger.prototype.exp = bnpExp;
    438 BigInteger.prototype.toString = bnToString;
    439 BigInteger.prototype.negate = bnNegate;
    440 BigInteger.prototype.abs = bnAbs;
    441 BigInteger.prototype.compareTo = bnCompareTo;
    442 BigInteger.prototype.bitLength = bnBitLength;
    443 BigInteger.prototype.mod = bnMod;
    444 BigInteger.prototype.modPowInt = bnModPowInt;
    445 BigInteger.ZERO = nbv(0);
    446 BigInteger.ONE = nbv(1);
    447 function Arcfour() {
    448     this.i = 0;
    449     this.j = 0;
    450     this.S = new Array();
    451 }
    452 function ARC4init(key) {
    453     var i, j, t;
    454     for (i = 0; i < 256; ++i)
    455         this.S[i] = i;
    456     j = 0;
    457     for (i = 0; i < 256; ++i) {
    458         j = (j + this.S[i] + key[i % key.length]) & 255;
    459         t = this.S[i];
    460         this.S[i] = this.S[j];
    461         this.S[j] = t;
    462     }
    463     this.i = 0;
    464     this.j = 0;
    465 }
    466 function ARC4next() {
    467     var t;
    468     this.i = (this.i + 1) & 255;
    469     this.j = (this.j + this.S[this.i]) & 255;
    470     t = this.S[this.i];
    471     this.S[this.i] = this.S[this.j];
    472     this.S[this.j] = t;
    473     return this.S[(t + this.S[this.i]) & 255];
    474 }
    475 Arcfour.prototype.init = ARC4init;
    476 Arcfour.prototype.next = ARC4next;
    477 function prng_newstate() {
    478     return new Arcfour();
    479 }
    480 var rng_psize = 256;
    481 var rng_state;
    482 var rng_pool;
    483 var rng_pptr;
    484 function rng_seed_int(x) {
    485     rng_pool[rng_pptr++] ^= x & 255;
    486     rng_pool[rng_pptr++] ^= (x >> 8) & 255;
    487     rng_pool[rng_pptr++] ^= (x >> 16) & 255;
    488     rng_pool[rng_pptr++] ^= (x >> 24) & 255;
    489     if (rng_pptr >= rng_psize) rng_pptr -= rng_psize;
    490 }
    491 function rng_seed_time() {
    492     rng_seed_int(new Date().getTime());
    493 }
    494 if (rng_pool == null) {
    495     rng_pool = new Array();
    496     rng_pptr = 0;
    497     var t;
    498     if (navigator.appName == "Netscape" && navigator.appVersion < "5" && window.crypto) {
    499 
    500         var z = window.crypto.random(32);
    501         for (t = 0; t < z.length; ++t)
    502             rng_pool[rng_pptr++] = z.charCodeAt(t) & 255;
    503     }
    504     while (rng_pptr < rng_psize) {
    505         t = Math.floor(65536 * Math.random());
    506         rng_pool[rng_pptr++] = t >>> 8;
    507         rng_pool[rng_pptr++] = t & 255;
    508     }
    509     rng_pptr = 0;
    510     rng_seed_time();
    511 
    512 
    513 }
    514 function rng_get_byte() {
    515     if (rng_state == null) {
    516         rng_seed_time();
    517         rng_state = prng_newstate();
    518         rng_state.init(rng_pool);
    519         for (rng_pptr = 0; rng_pptr < rng_pool.length; ++rng_pptr)
    520             rng_pool[rng_pptr] = 0;
    521         rng_pptr = 0;
    522 
    523     }
    524 
    525     return rng_state.next();
    526 }
    527 function rng_get_bytes(ba) {
    528     var i;
    529     for (i = 0; i < ba.length; ++i) ba[i] = rng_get_byte();
    530 }
    531 function SecureRandom() { }
    532 SecureRandom.prototype.nextBytes = rng_get_bytes;
    533 function parseBigInt(str, r) {
    534     return new BigInteger(str, r);
    535 }
    536 function linebrk(s, n) {
    537     var ret = "";
    538     var i = 0;
    539     while (i + n < s.length) {
    540         ret += s.substring(i, i + n) + "
    ";
    541         i += n;
    542     }
    543     return ret + s.substring(i, s.length);
    544 }
    545 function byte2Hex(b) {
    546     if (b < 0x10)
    547         return "0" + b.toString(16);
    548     else
    549         return b.toString(16);
    550 }
    551 function pkcs1pad2(s, n) {
    552     if (n < s.length + 11) {
    553         alert("Message too long for RSA");
    554         return null;
    555     }
    556     var ba = new Array();
    557     var i = s.length - 1;
    558     while (i >= 0 && n > 0) ba[--n] = s.charCodeAt(i--);
    559     ba[--n] = 0;
    560     var rng = new SecureRandom();
    561     var x = new Array();
    562     while (n > 2) {
    563         x[0] = 0;
    564         while (x[0] == 0) rng.nextBytes(x);
    565         ba[--n] = x[0];
    566     }
    567     ba[--n] = 2;
    568     ba[--n] = 0;
    569     return new BigInteger(ba);
    570 }
    571 function RSAKey() {
    572     this.n = null;
    573     this.e = 0;
    574     this.d = null;
    575     this.p = null;
    576     this.q = null;
    577     this.dmp1 = null;
    578     this.dmq1 = null;
    579     this.coeff = null;
    580 }
    581 function RSASetPublic(N, E) {
    582     if (N != null && E != null && N.length > 0 && E.length > 0) {
    583         this.n = parseBigInt(N, 16);
    584         this.e = parseInt(E, 16);
    585     }
    586     else
    587         alert("Invalid RSA public key");
    588 }
    589 function RSADoPublic(x) {
    590     return x.modPowInt(this.e, this.n);
    591 }
    592 function RSAEncrypt(text) {
    593     var m = pkcs1pad2(text, (this.n.bitLength() + 7) >> 3);
    594     if (m == null) return null;
    595     var c = this.doPublic(m);
    596     if (c == null) return null;
    597     var h = c.toString(16);
    598     if ((h.length & 1) == 0) return h; else return "0" + h;
    599 }
    600 RSAKey.prototype.doPublic = RSADoPublic;
    601 RSAKey.prototype.setPublic = RSASetPublic;
    602 RSAKey.prototype.encrypt = RSAEncrypt;
    603 var b64map = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
    604 var b64pad = "=";
    605 function hex2b64(h) {
    606     var i;
    607     var c;
    608     var ret = "";
    609     for (i = 0; i + 3 <= h.length; i += 3) {
    610         c = parseInt(h.substring(i, i + 3), 16);
    611         ret += b64map.charAt(c >> 6) + b64map.charAt(c & 63);
    612     }
    613     if (i + 1 == h.length) {
    614         c = parseInt(h.substring(i, i + 1), 16);
    615         ret += b64map.charAt(c << 2);
    616     }
    617     else if (i + 2 == h.length) {
    618         c = parseInt(h.substring(i, i + 2), 16);
    619         ret += b64map.charAt(c >> 2) + b64map.charAt((c & 3) << 4);
    620     }
    621     while ((ret.length & 3) > 0) ret += b64pad;
    622     return ret;
    623 }
    624 function b64tohex(s) {
    625     var ret = ""
    626     var i;
    627     var k = 0;
    628     var slop;
    629     for (i = 0; i < s.length; ++i) {
    630         if (s.charAt(i) == b64pad) break;
    631         v = b64map.indexOf(s.charAt(i));
    632         if (v < 0) continue;
    633         if (k == 0) {
    634             ret += int2char(v >> 2);
    635             slop = v & 3;
    636             k = 1;
    637         }
    638         else if (k == 1) {
    639             ret += int2char((slop << 2) | (v >> 4));
    640             slop = v & 0xf;
    641             k = 2;
    642         }
    643         else if (k == 2) {
    644             ret += int2char(slop);
    645             ret += int2char(v >> 2);
    646             slop = v & 3;
    647             k = 3;
    648         }
    649         else {
    650             ret += int2char((slop << 2) | (v >> 4));
    651             ret += int2char(v & 0xf);
    652             k = 0;
    653         }
    654     }
    655     if (k == 1)
    656         ret += int2char(slop << 2);
    657     return ret;
    658 }
    659 function b64toBA(s) {
    660 
    661     var h = b64tohex(s);
    662     var i;
    663     var a = new Array();
    664     for (i = 0; 2 * i < h.length; ++i) {
    665         a[i] = parseInt(h.substring(2 * i, 2 * i + 2), 16);
    666     }
    667     return a;
    668 }

    Case Three

     1         function OnBeforeLoginClick() {
     2 
     3             var pwd = $("PlainPass").value; 
     4 
     5             var maskValue = "xxxxxxxxxxxxxxxxxxxxx".substring(0, pwd.length);
     6             $("PlainPass").value = maskValue;
     7 
     8             var modulus = '公钥modulus';
     9             var exponent = '公钥exponent';
    10 
    11             var rsa = new RSAKey();
    12             rsa.setPublic(modulus, exponent);
    13 
    14             var res = rsa.encrypt(pwd);
    15             if (res) {
    16                 $("hfPassword").value = res;
    17             }
    18 
    19             return true;
    20         }
  • 相关阅读:
    RMAN参考使用手册2[转载]
    硬盘故障的解决
    控制文件和重做日志文件
    设置DBID
    让虚拟机从U盘启动[转载]
    windows7桌面预览不出现的解决方法
    让.Net程序脱离.Net Framework框架运行
    抗辐射多吃六种食物《转》
    ASPX.NET学习记录(一)
    饮食影响人的七情六欲《转》
  • 原文地址:https://www.cnblogs.com/Jeremy2001/p/6691637.html
Copyright © 2011-2022 走看看