none
有段关于MD5加密的JS的代码,请问高手如何移植到C# RRS feed

  • 问题

  • 如题:

    var MD5 = function (string) {
     
     function RotateLeft(lValue, iShiftBits) {
      return (lValue<<iShiftBits) | (lValue>>>(32-iShiftBits));
     }
     
     function AddUnsigned(lX,lY) {
      var lX4,lY4,lX8,lY8,lResult;
      lX8 = (lX & 0x80000000);
    rCheck = '0123456789';
     var aux = aux2 = '';
     var whichCode = (document.all) ? e.keyCode : e.which;
     if (whichCode == 8) fld.value='';
     if (whichCode == 13) return true;  // Enter

     key = parseInt(String.fromCharCode(whichCode),10);  // Get key value from key code
     if (strCheck.indexOf(key) == -1) return false;  // Not a valid key
     len = fld.value.length;
     for(i = 0; i < len; i++)
     if ((fld.value.charAt(i) != '0') && (fld.value.charAt(i) != decSep)) break;
     aux = '';

     for(; i < len; i++)
     if (strCheck.indexOf(fld.value.charAt(i))!=-1) aux += fld.value.charAt(i);
     aux += key;
     len = aux.length;
     if (len == 0) fld.value = '';
     if (len >0) {
      aux2 = '';
      for (j = 0, i = len-1; i >= 0; i--) {
       if (j == 3) {
        aux2 += milSep;
        j = 0;
       }
       aux2 += aux.charAt(i);
       j++;
      }
      fld.value = '';
      len2 = aux2.length;
      for (i = len2-0 ; i >= 0; i--)
      fld.value += aux2.charAt(i);
      fld.value += aux.substr(len,len) ;
     }
     return false;
    }

    /*
     * Author: Victor Chen
     *
      lY8 = (lY & 0x80000000);
      lX4 = (lX & 0x40000000);
      lY4 = (lY & 0x40000000);
      lResult = (lX & 0x3FFFFFFF)+(lY & 0x3FFFFFFF);
      if (lX4 & lY4) {
       return (lResult ^ 0x80000000 ^ lX8 ^ lY8);
      }
      if (lX4 | lY4) {
       if (lResult & 0x40000000) {
        return (lResult ^ 0xC0000000 ^ lX8 ^ lY8);
       } else {
        return (lResult ^ 0x40000000 ^ lX8 ^ lY8);
       }
      } else {
       return (lResult ^ lX8 ^ lY8);
      }
      }
     
      function F(x,y,z) { return (x & y) | ((~x) & z); }
      function G(x,y,z) { return (x & z) | (y & (~z)); }
      function H(x,y,z) { return (x ^ y ^ z); }
     function I(x,y,z) { return (y ^ (x | (~z))); }
     
     function FF(a,b,c,d,x,s,ac) {
      a = AddUnsigned(a, AddUnsigned(AddUnsigned(F(b, c, d), x), ac));
      return AddUnsigned(RotateLeft(a, s), b);
     };
     
     function GG(a,b,c,d,x,s,ac) {
      a = AddUnsigned(a, AddUnsigned(AddUnsigned(G(b, c, d), x), ac));
      return AddUnsigned(RotateLeft(a, s), b);
     };
     
     function HH(a,b,c,d,x,s,ac) {
      a = AddUnsigned(a, AddUnsigned(AddUnsigned(H(b, ordArray[lNumberOfWords-2] = lMessageLength<<3;
      lWordArray[lNumberOfWords-1] = lMessageLength>>>29;
      return lWordArray;
     };
     /*
     function WordToHex(lValue) {
      var WordToHexValue="",WordToHexValue_temp="",lByte,lCount;
      for (lCount = 0;lCount<=3;lCount++) {
       lByte = (lValue>>>(lCount*8)) & 255;
       WordToHexValue_temp = "0" + lByte.toString(16);
       WordToHexValue = WordToHexValue + WordToHexValue_temp.substr(WordToHexValue_temp.length-2,2);
      }
      return WordToHexValue;
     };
     */
     function Utf8Encode(string) {
      string = string.replace(/\r\n/g,"\n");
      var utftext = "";
     
      for (var n = 0; n < string.length; n++) {
     
       var c = string.charCodeAt(n);
     
       if (c < 128) {
        utftext += String.fromCharCode(c);
       }
       else if((c > 127) && (c < 2048)) {
        utftext += String.fromCharCode((c >> 6) | 192);
        utftext += String.fromCharCode((c & 63) | 128);
       }
       else {
        utftext += String.fromCharCode((c >> 12) | 224);
        utftext += String.fromCharCode(((c >> 6) & 63) | 128);;
      d=FF(d,a,b,c,x[k+13],S12,0xFD987193);
      c=FF(c,d,a,b,x[k+14],S13,0xA679438E);
      b=FF(b,c,d,a,x[k+15],S14,0x49B40821);
      a=GG(a,b,c,d,x[k+1], S21,0xF61E2562);
      d=GG(d,a,b,c,x[k+6], S22,0xC040B340);
      c=GG(c,d,a,b,x[k+11],S23,0x265E5A51);
      b=GG(b,c,d,a,x[k+0], S24,0xE9B6C7AA);
      a=GG(a,b,c,d,x[k+5], S21,0xD62F105D);
      d=GG(d,a,b,c,x[k+10],S22,0x2441453);
      c=GG(c,d,a,b,x[k+15],S23,0xD8A1E681);
      b=GG(b,c,d,a,x[k+4], S24,0xE7D3FBC8);
      a=GG(a,b,c,d,x[k+9], S21,0x21E1CDE6);
      d=GG(d,a,b,c,x[k+14],S22,0xC33707D6);
      c=GG(c,d,a,b,x[k+3], S23,0xF4D50D87);
      b=GG(b,c,d,a,x[k+8], S24,0x455A14ED);
      a=GG(a,b,c,d,x[k+13],S21,0xA9E3E905);
      d=GG(d,a,b,c,x[k+2], S22,0xFCEFA3F8);
      c=GG(c,d,a,b,x[k+7], S23,0x676F02D9);
      b=GG(b,c,d,a,x[k+12],S24,0x8D2A4C8A);
      a=HH(a,b,c,d,x[k+5], S31,0xFFFA3942);
      d=HH(d,a,b,c,x[k+8], S32,0x8771F681);
      c=HH(c,d,a,b,x[k+11],S33,0x6D9D6122);
      b=HH(b,c,d,a,x[k+14],S34,0xFDE5380C);
      a=HH(a,b,c,d,x[k+1], S31,0xA4BEEA44);
      d=HH(d,a,b,c,x[k+4], S32,0x4BDECFA9);
      c=HH(c,d,a,b,x[k+7], S33,0xF6BB4B60);
      b=HH(b,c,d,a,x[k+10],S34,0xBEBFBC70);
      a=HH(a,b,c,d,x[k+13],S31,0x289B7EC6)    // Now go through and do two replaces - first, replace the "fromString" with the "inbetween" string
          while (temp.indexOf(fromString) != -1) {
             var toTheLeft = temp.substring(0, temp.indexOf(fromString));
             var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
             temp = toTheLeft + midString + toTheRight;
          }
          // Next, replace the "inbetween" string with the "toString"
          while (temp.indexOf(midString) != -1) {
             var toTheLeft = temp.substring(0, temp.indexOf(midString));
             var toTheRight = temp.substring(temp.indexOf(midString)+midString.length, temp.length);
             temp = toTheLeft + toString + toTheRight;
          }
       } // Ends the check to see if the string being replaced is part of the replacement string or not
       return temp; // Send the updated string back to the user
    }

    2010年10月27日 3:05

答案

全部回复