神州教育培训网 打造高端培训课程,传播精品教育理念! 神州教育 神州培训 教育 企业培训 企业内训 企业管理 研修课程 简章 资本运作 房地产 国学 名师推荐

js MD5包含中文串时加密结果与JAVA结果不一致的解决方案

造成这个现象的原因是:js加密前未对中文字符进行UTF-8转码;

解决方案1:

直接使用CryptoJS的MD5包加密即可:

下载地址:https://code.google.com/archive/p/crypto-js/downloads

console.log('hex_md5', CryptoJS.MD5('加密字符').toString());

 

解决方案2:

直接使用下面的JS代码进行加密即可:

  1 function md5(string) {
  2     function md5_RotateLeft(lValue, iShiftBits) {
  3         return (lValue << iShiftBits) | (lValue >>> (32 - iShiftBits));
  4     }
  5 
  6     function md5_AddUnsigned(lX, lY) {
  7         var lX4, lY4, lX8, lY8, lResult;
  8         lX8 = (lX & 0x80000000);
  9         lY8 = (lY & 0x80000000);
 10         lX4 = (lX & 0x40000000);
 11         lY4 = (lY & 0x40000000);
 12         lResult = (lX & 0x3FFFFFFF) + (lY & 0x3FFFFFFF);
 13         if (lX4 & lY4) {
 14             return (lResult ^ 0x80000000 ^ lX8 ^ lY8);
 15         }
 16         if (lX4 | lY4) {
 17             if (lResult & 0x40000000) {
 18                 return (lResult ^ 0xC0000000 ^ lX8 ^ lY8);
 19             } else {
 20                 return (lResult ^ 0x40000000 ^ lX8 ^ lY8);
 21             }
 22         } else {
 23             return (lResult ^ lX8 ^ lY8);
 24         }
 25     }
 26 
 27     function md5_F(x, y, z) {
 28         return (x & y) | ((~x) & z);
 29     }
 30 
 31     function md5_G(x, y, z) {
 32         return (x & z) | (y & (~z));
 33     }
 34 
 35     function md5_H(x, y, z) {
 36         return (x ^ y ^ z);
 37     }
 38 
 39     function md5_I(x, y, z) {
 40         return (y ^ (x | (~z)));
 41     }
 42 
 43     function md5_FF(a, b, c, d, x, s, ac) {
 44         a = md5_AddUnsigned(a, md5_AddUnsigned(md5_AddUnsigned(md5_F(b, c, d), x), ac));
 45         return md5_AddUnsigned(md5_RotateLeft(a, s), b);
 46     };
 47 
 48     function md5_GG(a, b, c, d, x, s, ac) {
 49         a = md5_AddUnsigned(a, md5_AddUnsigned(md5_AddUnsigned(md5_G(b, c, d), x), ac));
 50         return md5_AddUnsigned(md5_RotateLeft(a, s), b);
 51     };
 52 
 53     function md5_HH(a, b, c, d, x, s, ac) {
 54         a = md5_AddUnsigned(a, md5_AddUnsigned(md5_AddUnsigned(md5_H(b, c, d), x), ac));
 55         return md5_AddUnsigned(md5_RotateLeft(a, s), b);
 56     };
 57 
 58     function md5_II(a, b, c, d, x, s, ac) {
 59         a = md5_AddUnsigned(a, md5_AddUnsigned(md5_AddUnsigned(md5_I(b, c, d), x), ac));
 60         return md5_AddUnsigned(md5_RotateLeft(a, s), b);
 61     };
 62 
 63     function md5_ConvertToWordArray(string) {
 64         var lWordCount;
 65         var lMessageLength = string.length;
 66         var lNumberOfWords_temp1 = lMessageLength + 8;
 67         var lNumberOfWords_temp2 = (lNumberOfWords_temp1 - (lNumberOfWords_temp1 % 64)) / 64;
 68         var lNumberOfWords = (lNumberOfWords_temp2 + 1) * 16;
 69         var lWordArray = Array(lNumberOfWords - 1);
 70         var lBytePosition = 0;
 71         var lByteCount = 0;
 72         while (lByteCount < lMessageLength) {
 73             lWordCount = (lByteCount - (lByteCount % 4)) / 4;
 74             lBytePosition = (lByteCount % 4) * 8;
 75             lWordArray[lWordCount] = (lWordArray[lWordCount] | (string.charCodeAt(lByteCount) << lBytePosition));
 76             lByteCount++;
 77         }
 78         lWordCount = (lByteCount - (lByteCount % 4)) / 4;
 79         lBytePosition = (lByteCount % 4) * 8;
 80         lWordArray[lWordCount] = lWordArray[lWordCount] | (0x80 << lBytePosition);
 81         lWordArray[lNumberOfWords - 2] = lMessageLength << 3;
 82         lWordArray[lNumberOfWords - 1] = lMessageLength >>> 29;
 83         return lWordArray;
 84     };
 85 
 86     function md5_WordToHex(lValue) {
 87         var WordToHexValue = "",
 88             WordToHexValue_temp = "",
 89             lByte, lCount;
 90         for (lCount = 0; lCount <= 3; lCount++) {
 91             lByte = (lValue >>> (lCount * 8)) & 255;
 92             WordToHexValue_temp = "0" + lByte.toString(16);
 93             WordToHexValue = WordToHexValue + WordToHexValue_temp.substr(WordToHexValue_temp.length - 2, 2);
 94         }
 95         return WordToHexValue;
 96     };
 97 
 98     function md5_Utf8Encode(string) {
 99         string = string.replace(/\r\n/g, "\n");
100         var utftext = "";
101         for (var n = 0; n < string.length; n++) {
102             var c = string.charCodeAt(n);
103             if (c < 128) {
104                 utftext += String.fromCharCode(c);
105             } else if ((c > 127) && (c < 2048)) {
106                 utftext += String.fromCharCode((c >> 6) | 192);
107                 utftext += String.fromCharCode((c & 63) | 128);
108             } else {
109                 utftext += String.fromCharCode((c >> 12) | 224);
110                 utftext += String.fromCharCode(((c >> 6) & 63) | 128);
111                 utftext += String.fromCharCode((c & 63) | 128);
112             }
113         }
114         return utftext;
115     };
116     var x = Array();
117     var k, AA, BB, CC, DD, a, b, c, d;
118     var S11 = 7,
119         S12 = 12,
120         S13 = 17,
121         S14 = 22;
122     var S21 = 5,
123         S22 = 9,
124         S23 = 14,
125         S24 = 20;
126     var S31 = 4,
127         S32 = 11,
128         S33 = 16,
129         S34 = 23;
130     var S41 = 6,
131         S42 = 10,
132         S43 = 15,
133         S44 = 21;
134     string = md5_Utf8Encode(string);
135     x = md5_ConvertToWordArray(string);
136     a = 0x67452301;
137     b = 0xEFCDAB89;
138     c = 0x98BADCFE;
139     d = 0x10325476;
140     for (k = 0; k < x.length; k += 16) {
141         AA = a;
142         BB = b;
143         CC = c;
144         DD = d;
145         a = md5_FF(a, b, c, d, x[k + 0], S11, 0xD76AA478);
146         d = md5_FF(d, a, b, c, x[k + 1], S12, 0xE8C7B756);
147         c = md5_FF(c, d, a, b, x[k + 2], S13, 0x242070DB);
148         b = md5_FF(b, c, d, a, x[k + 3], S14, 0xC1BDCEEE);
149         a = md5_FF(a, b, c, d, x[k + 4], S11, 0xF57C0FAF);
150         d = md5_FF(d, a, b, c, x[k + 5], S12, 0x4787C62A);
151         c = md5_FF(c, d, a, b, x[k + 6], S13, 0xA8304613);
152         b = md5_FF(b, c, d, a, x[k + 7], S14, 0xFD469501);
153         a = md5_FF(a, b, c, d, x[k + 8], S11, 0x698098D8);
154         d = md5_FF(d, a, b, c, x[k + 9], S12, 0x8B44F7AF);
155         c = md5_FF(c, d, a, b, x[k + 10], S13, 0xFFFF5BB1);
156         b = md5_FF(b, c, d, a, x[k + 11], S14, 0x895CD7BE);
157         a = md5_FF(a, b, c, d, x[k + 12], S11, 0x6B901122);
158         d = md5_FF(d, a, b, c, x[k + 13], S12, 0xFD987193);
159         c = md5_FF(c, d, a, b, x[k + 14], S13, 0xA679438E);
160         b = md5_FF(b, c, d, a, x[k + 15], S14, 0x49B40821);
161         a = md5_GG(a, b, c, d, x[k + 1], S21, 0xF61E2562);
162         d = md5_GG(d, a, b, c, x[k + 6], S22, 0xC040B340);
163         c = md5_GG(c, d, a, b, x[k + 11], S23, 0x265E5A51);
164         b = md5_GG(b, c, d, a, x[k + 0], S24, 0xE9B6C7AA);
165         a = md5_GG(a, b, c, d, x[k + 5], S21, 0xD62F105D);
166         d = md5_GG(d, a, b, c, x[k + 10], S22, 0x2441453);
167         c = md5_GG(c, d, a, b, x[k + 15], S23, 0xD8A1E681);
168         b = md5_GG(b, c, d, a, x[k + 4], S24, 0xE7D3FBC8);
169         a = md5_GG(a, b, c, d, x[k + 9], S21, 0x21E1CDE6);
170         d = md5_GG(d, a, b, c, x[k + 14], S22, 0xC33707D6);
171         c = md5_GG(c, d, a, b, x[k + 3], S23, 0xF4D50D87);
172         b = md5_GG(b, c, d, a, x[k + 8], S24, 0x455A14ED);
173         a = md5_GG(a, b, c, d, x[k + 13], S21, 0xA9E3E905);
174         d = md5_GG(d, a, b, c, x[k + 2], S22, 0xFCEFA3F8);
175         c = md5_GG(c, d, a, b, x[k + 7], S23, 0x676F02D9);
176         b = md5_GG(b, c, d, a, x[k + 12], S24, 0x8D2A4C8A);
177         a = md5_HH(a, b, c, d, x[k + 5], S31, 0xFFFA3942);
178         d = md5_HH(d, a, b, c, x[k + 8], S32, 0x8771F681);
179         c = md5_HH(c, d, a, b, x[k + 11], S33, 0x6D9D6122);
180         b = md5_HH(b, c, d, a, x[k + 14], S34, 0xFDE5380C);
181         a = md5_HH(a, b, c, d, x[k + 1], S31, 0xA4BEEA44);
182         d = md5_HH(d, a, b, c, x[k + 4], S32, 0x4BDECFA9);
183         c = md5_HH(c, d, a, b, x[k + 7], S33, 0xF6BB4B60);
184         b = md5_HH(b, c, d, a, x[k + 10], S34, 0xBEBFBC70);
185         a = md5_HH(a, b, c, d, x[k + 13], S31, 0x289B7EC6);
186         d = md5_HH(d, a, b, c, x[k + 0], S32, 0xEAA127FA);
187         c = md5_HH(c, d, a, b, x[k + 3], S33, 0xD4EF3085);
188         b = md5_HH(b, c, d, a, x[k + 6], S34, 0x4881D05);
189         a = md5_HH(a, b, c, d, x[k + 9], S31, 0xD9D4D039);
190         d = md5_HH(d, a, b, c, x[k + 12], S32, 0xE6DB99E5);
191         c = md5_HH(c, d, a, b, x[k + 15], S33, 0x1FA27CF8);
192         b = md5_HH(b, c, d, a, x[k + 2], S34, 0xC4AC5665);
193         a = md5_II(a, b, c, d, x[k + 0], S41, 0xF4292244);
194         d = md5_II(d, a, b, c, x[k + 7], S42, 0x432AFF97);
195         c = md5_II(c, d, a, b, x[k + 14], S43, 0xAB9423A7);
196         b = md5_II(b, c, d, a, x[k + 5], S44, 0xFC93A039);
197         a = md5_II(a, b, c, d, x[k + 12], S41, 0x655B59C3);
198         d = md5_II(d, a, b, c, x[k + 3], S42, 0x8F0CCC92);
199         c = md5_II(c, d, a, b, x[k + 10], S43, 0xFFEFF47D);
200         b = md5_II(b, c, d, a, x[k + 1], S44, 0x85845DD1);
201         a = md5_II(a, b, c, d, x[k + 8], S41, 0x6FA87E4F);
202         d = md5_II(d, a, b, c, x[k + 15], S42, 0xFE2CE6E0);
203         c = md5_II(c, d, a, b, x[k + 6], S43, 0xA3014314);
204         b = md5_II(b, c, d, a, x[k + 13], S44, 0x4E0811A1);
205         a = md5_II(a, b, c, d, x[k + 4], S41, 0xF7537E82);
206         d = md5_II(d, a, b, c, x[k + 11], S42, 0xBD3AF235);
207         c = md5_II(c, d, a, b, x[k + 2], S43, 0x2AD7D2BB);
208         b = md5_II(b, c, d, a, x[k + 9], S44, 0xEB86D391);
209         a = md5_AddUnsigned(a, AA);
210         b = md5_AddUnsigned(b, BB);
211         c = md5_AddUnsigned(c, CC);
212         d = md5_AddUnsigned(d, DD);
213     }
214     return (md5_WordToHex(a) + md5_WordToHex(b) + md5_WordToHex(c) + md5_WordToHex(d)).toLowerCase();
215 }
View Code

 

解决方案3:

仍使用常规的加密代码,加密前先对待加密的字符进行UTF-8转码,相关代码如下:

  1 //UTF-8转码方法1
  2 URLEncoder.encode(str,'utf-8');
  3 
  4 //UTF-8转码方法2
  5 function Utf8Encode(string) {
  6     var utftext = "";
  7     for (var n = 0; n < string.length; n++) {
  8         var c = string.charCodeAt(n);
  9         if (c < 128) {
 10             utftext += String.fromCharCode(c);
 11         } else if ((c > 127) && (c < 2048)) {
 12             utftext += String.fromCharCode((c >> 6) | 192);
 13             utftext += String.fromCharCode((c & 63) | 128);
 14         } else {
 15             utftext += String.fromCharCode((c >> 12) | 224);
 16             utftext += String.fromCharCode(((c >> 6) & 63) | 128);
 17             utftext += String.fromCharCode((c & 63) | 128);
 18         }
 19     }
 20     return utftext;
 21 }
 22 
 23 //常规MD5加密代码(供参考)
 24 /*
 25  * A JavaScript implementation of the RSA Data Security, Inc. MD5 Message
 26  * Digest Algorithm, as defined in RFC 1321.
 27  * Version 2.1 Copyright (C) Paul Johnston 1999 - 2002.
 28  * Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet
 29  * Distributed under the BSD License
 30  * See http://pajhome.org.uk/crypt/md5 for more info.
 31  */
 32 
 33 /*
 34  * Configurable variables. You may need to tweak these to be compatible with
 35  * the server-side, but the defaults work in most cases.
 36  */
 37 var hexcase = 0;  /* hex output format. 0 - lowercase; 1 - uppercase        */
 38 var b64pad  = ""; /* base-64 pad character. "=" for strict RFC compliance   */
 39 var chrsz   = 8;  /* bits per input character. 8 - ASCII; 16 - Unicode      */
 40 
 41 /*
 42  * These are the functions you'll usually want to call
 43  * They take string arguments and return either hex or base-64 encoded strings
 44  */
 45 function hex_md5(s){ return binl2hex(core_md5(str2binl(s), s.length * chrsz));}
 46 function b64_md5(s){ return binl2b64(core_md5(str2binl(s), s.length * chrsz));}
 47 function str_md5(s){ return binl2str(core_md5(str2binl(s), s.length * chrsz));}
 48 function hex_hmac_md5(key, data) { return binl2hex(core_hmac_md5(key, data)); }
 49 function b64_hmac_md5(key, data) { return binl2b64(core_hmac_md5(key, data)); }
 50 function str_hmac_md5(key, data) { return binl2str(core_hmac_md5(key, data)); }
 51 
 52 /*
 53  * Perform a simple self-test to see if the VM is working
 54  */
 55 function md5_vm_test()
 56 {
 57   return hex_md5("abc") == "900150983cd24fb0d6963f7d28e17f72";
 58 }
 59 
 60 /*
 61  * Calculate the MD5 of an array of little-endian words, and a bit length
 62  */
 63 function core_md5(x, len)
 64 {
 65   /* append padding */
 66   x[len >> 5] |= 0x80 << ((len) % 32);
 67   x[(((len + 64) >>> 9) << 4) + 14] = len;
 68 
 69   var a =  1732584193;
 70   var b = -271733879;
 71   var c = -1732584194;
 72   var d =  271733878;
 73 
 74   for(var i = 0; i < x.length; i += 16)
 75   {
 76     var olda = a;
 77     var oldb = b;
 78     var oldc = c;
 79     var oldd = d;
 80 
 81     a = md5_ff(a, b, c, d, x[i+ 0], 7 , -680876936);
 82     d = md5_ff(d, a, b, c, x[i+ 1], 12, -389564586);
 83     c = md5_ff(c, d, a, b, x[i+ 2], 17,  606105819);
 84     b = md5_ff(b, c, d, a, x[i+ 3], 22, -1044525330);
 85     a = md5_ff(a, b, c, d, x[i+ 4], 7 , -176418897);
 86     d = md5_ff(d, a, b, c, x[i+ 5], 12,  1200080426);
 87     c = md5_ff(c, d, a, b, x[i+ 6], 17, -1473231341);
 88     b = md5_ff(b, c, d, a, x[i+ 7], 22, -45705983);
 89     a = md5_ff(a, b, c, d, x[i+ 8], 7 ,  1770035416);
 90     d = md5_ff(d, a, b, c, x[i+ 9], 12, -1958414417);
 91     c = md5_ff(c, d, a, b, x[i+10], 17, -42063);
 92     b = md5_ff(b, c, d, a, x[i+11], 22, -1990404162);
 93     a = md5_ff(a, b, c, d, x[i+12], 7 ,  1804603682);
 94     d = md5_ff(d, a, b, c, x[i+13], 12, -40341101);
 95     c = md5_ff(c, d, a, b, x[i+14], 17, -1502002290);
 96     b = md5_ff(b, c, d, a, x[i+15], 22,  1236535329);
 97 
 98     a = md5_gg(a, b, c, d, x[i+ 1], 5 , -165796510);
 99     d = md5_gg(d, a, b, c, x[i+ 6], 9 , -1069501632);
100     c = md5_gg(c, d, a, b, x[i+11], 14,  643717713);
101     b = md5_gg(b, c, d, a, x[i+ 0], 20, -373897302);
102     a = md5_gg(a, b, c, d, x[i+ 5], 5 , -701558691);
103     d = md5_gg(d, a, b, c, x[i+10], 9 ,  38016083);
104     c = md5_gg(c, d, a, b, x[i+15], 14, -660478335);
105     b = md5_gg(b, c, d, a, x[i+ 4], 20, -405537848);
106     a = md5_gg(a, b, c, d, x[i+ 9], 5 ,  568446438);
107     d = md5_gg(d, a, b, c, x[i+14], 9 , -1019803690);
108     c = md5_gg(c, d, a, b, x[i+ 3], 14, -187363961);
109     b = md5_gg(b, c, d, a, x[i+ 8], 20,  1163531501);
110     a = md5_gg(a, b, c, d, x[i+13], 5 , -1444681467);
111     d = md5_gg(d, a, b, c, x[i+ 2], 9 , -51403784);
112     c = md5_gg(c, d, a, b, x[i+ 7], 14,  1735328473);
113     b = md5_gg(b, c, d, a, x[i+12], 20, -1926607734);
114 
115     a = md5_hh(a, b, c, d, x[i+ 5], 4 , -378558);
116     d = md5_hh(d, a, b, c, x[i+ 8], 11, -2022574463);
117     c = md5_hh(c, d, a, b, x[i+11], 16,  1839030562);
118     b = md5_hh(b, c, d, a, x[i+14], 23, -35309556);
119     a = md5_hh(a, b, c, d, x[i+ 1], 4 , -1530992060);
120     d = md5_hh(d, a, b, c, x[i+ 4], 11,  1272893353);
121     c = md5_hh(c, d, a, b, x[i+ 7], 16, -155497632);
122     b = md5_hh(b, c, d, a, x[i+10], 23, -1094730640);
123     a = md5_hh(a, b, c, d, x[i+13], 4 ,  681279174);
124     d = md5_hh(d, a, b, c, x[i+ 0], 11, -358537222);
125     c = md5_hh(c, d, a, b, x[i+ 3], 16, -722521979);
126     b = md5_hh(b, c, d, a, x[i+ 6], 23,  76029189);
127     a = md5_hh(a, b, c, d, x[i+ 9], 4 , -640364487);
128     d = md5_hh(d, a, b, c, x[i+12], 11, -421815835);
129     c = md5_hh(c, d, a, b, x[i+15], 16,  530742520);
130     b = md5_hh(b, c, d, a, x[i+ 2], 23, -995338651);
131 
132     a = md5_ii(a, b, c, d, x[i+ 0], 6 , -198630844);
133     d = md5_ii(d, a, b, c, x[i+ 7], 10,  1126891415);
134     c = md5_ii(c, d, a, b, x[i+14], 15, -1416354905);
135     b = md5_ii(b, c, d, a, x[i+ 5], 21, -57434055);
136     a = md5_ii(a, b, c, d, x[i+12], 6 ,  1700485571);
137     d = md5_ii(d, a, b, c, x[i+ 3], 10, -1894986606);
138     c = md5_ii(c, d, a, b, x[i+10], 15, -1051523);
139     b = md5_ii(b, c, d, a, x[i+ 1], 21, -2054922799);
140     a = md5_ii(a, b, c, d, x[i+ 8], 6 ,  1873313359);
141     d = md5_ii(d, a, b, c, x[i+15], 10, -30611744);
142     c = md5_ii(c, d, a, b, x[i+ 6], 15, -1560198380);
143     b = md5_ii(b, c, d, a, x[i+13], 21,  1309151649);
144     a = md5_ii(a, b, c, d, x[i+ 4], 6 , -145523070);
145     d = md5_ii(d, a, b, c, x[i+11], 10, -1120210379);
146     c = md5_ii(c, d, a, b, x[i+ 2], 15,  718787259);
147     b = md5_ii(b, c, d, a, x[i+ 9], 21, -343485551);
148 
149     a = safe_add(a, olda);
150     b = safe_add(b, oldb);
151     c = safe_add(c, oldc);
152     d = safe_add(d, oldd);
153   }
154   return Array(a, b, c, d);
155 
156 }
157 
158 /*
159  * These functions implement the four basic operations the algorithm uses.
160  */
161 function md5_cmn(q, a, b, x, s, t)
162 {
163   return safe_add(bit_rol(safe_add(safe_add(a, q), safe_add(x, t)), s),b);
164 }
165 function md5_ff(a, b, c, d, x, s, t)
166 {
167   return md5_cmn((b & c) | ((~b) & d), a, b, x, s, t);
168 }
169 function md5_gg(a, b, c, d, x, s, t)
170 {
171   return md5_cmn((b & d) | (c & (~d)), a, b, x, s, t);
172 }
173 function md5_hh(a, b, c, d, x, s, t)
174 {
175   return md5_cmn(b ^ c ^ d, a, b, x, s, t);
176 }
177 function md5_ii(a, b, c, d, x, s, t)
178 {
179   return md5_cmn(c ^ (b | (~d)), a, b, x, s, t);
180 }
181 
182 /*
183  * Calculate the HMAC-MD5, of a key and some data
184  */
185 function core_hmac_md5(key, data)
186 {
187   var bkey = str2binl(key);
188   if(bkey.length > 16) bkey = core_md5(bkey, key.length * chrsz);
189 
190   var ipad = Array(16), opad = Array(16);
191   for(var i = 0; i < 16; i++)
192   {
193     ipad[i] = bkey[i] ^ 0x36363636;
194     opad[i] = bkey[i] ^ 0x5C5C5C5C;
195   }
196 
197   var hash = core_md5(ipad.concat(str2binl(data)), 512 + data.length * chrsz);
198   return core_md5(opad.concat(hash), 512 + 128);
199 }
200 
201 /*
202  * Add integers, wrapping at 2^32. This uses 16-bit operations internally
203  * to work around bugs in some JS interpreters.
204  */
205 function safe_add(x, y)
206 {
207   var lsw = (x & 0xFFFF) + (y & 0xFFFF);
208   var msw = (x >> 16) + (y >> 16) + (lsw >> 16);
209   return (msw << 16) | (lsw & 0xFFFF);
210 }
211 
212 /*
213  * Bitwise rotate a 32-bit number to the left.
214  */
215 function bit_rol(num, cnt)
216 {
217   return (num << cnt) | (num >>> (32 - cnt));
218 }
219 
220 /*
221  * Convert a string to an array of little-endian words
222  * If chrsz is ASCII, characters >255 have their hi-byte silently ignored.
223  */
224 function str2binl(str)
225 {
226   var bin = Array();
227   var mask = (1 << chrsz) - 1;
228   for(var i = 0; i < str.length * chrsz; i += chrsz)
229     bin[i>>5] |= (str.charCodeAt(i / chrsz) & mask) << (i%32);
230   return bin;
231 }
232 
233 /*
234  * Convert an array of little-endian words to a string
235  */
236 function binl2str(bin)
237 {
238   var str = "";
239   var mask = (1 << chrsz) - 1;
240   for(var i = 0; i < bin.length * 32; i += chrsz)
241     str += String.fromCharCode((bin[i>>5] >>> (i % 32)) & mask);
242   return str;
243 }
244 
245 /*
246  * Convert an array of little-endian words to a hex string.
247  */
248 function binl2hex(binarray)
249 {
250   var hex_tab = hexcase ? "0123456789ABCDEF" : "0123456789abcdef";
251   var str = "";
252   for(var i = 0; i < binarray.length * 4; i++)
253   {
254     str += hex_tab.charAt((binarray[i>>2] >> ((i%4)*8+4)) & 0xF) +
255            hex_tab.charAt((binarray[i>>2] >> ((i%4)*8  )) & 0xF);
256   }
257   return str;
258 }
259 
260 /*
261  * Convert an array of little-endian words to a base-64 string
262  */
263 function binl2b64(binarray)
264 {
265   var tab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
266   var str = "";
267   for(var i = 0; i < binarray.length * 4; i += 3)
268   {
269     var triplet = (((binarray[i   >> 2] >> 8 * ( i   %4)) & 0xFF) << 16)
270                 | (((binarray[i+1 >> 2] >> 8 * ((i+1)%4)) & 0xFF) << 8 )
271                 |  ((binarray[i+2 >> 2] >> 8 * ((i+2)%4)) & 0xFF);
272     for(var j = 0; j < 4; j++)
273     {
274       if(i * 8 + j * 6 > binarray.length * 32) str += b64pad;
275       else str += tab.charAt((triplet >> 6*(3-j)) & 0x3F);
276     }
277   }
278   return str;
279 }
View Code

 

posted on 2020-03-25 10:38  努力&快乐  阅读(1333)  评论(0编辑  收藏  举报

导航

神州教育培训网 打造高端培训课程,传播精品教育理念! 神州教育 神州培训 教育 企业培训 企业内训 企业管理 研修课程 简章 资本运作 房地产 国学 名师推荐