MD5 加密之 java 与 js 实现

| |
[不指定 2024/01/09 16:45 | by 刘新修 ]

MD5 加密算法实际是一种信息摘要算法,其加密不可逆向解密;

其一般用作数据签名,来确保信息传输的完整性与安全性;

  • 完整性:传输数据完整未丢失
  • 安全性:数据未被恶意篡改

一、java 实现 MD5 加密

Java代码
  1. public class MD5Util {  
  2.   
  3.     /**
  4.      * 对字符串md5加密
  5.      *
  6.      * @param str 传入要加密的字符串
  7.      * @return MD5加密后的字符串(小写+字母)
  8.      */  
  9.     public static String getMD5LowerCase(String str) {  
  10.         try {  
  11.             // 生成一个MD5加密计算摘要  
  12.             MessageDigest md = MessageDigest.getInstance("MD5");  
  13.             // 计算md5函数  
  14.             md.update(str.getBytes());  
  15.             // digest()最后确定返回md5 hash值,返回值为8为字符串。因为md5 hash值是16位的hex值,实际上就是8位的字符  
  16.             // BigInteger函数则将8位的字符串转换成16位hex值,用字符串来表示;得到字符串形式的hash值  
  17.             return new BigInteger(1, md.digest()).toString(16);  
  18.         } catch (Exception e) {  
  19.             e.printStackTrace();  
  20.             return null;  
  21.         }  
  22.     }  
  23.   
  24.     /**
  25.      * 对字符串md5加密
  26.      *
  27.      * @param str 传入要加密的字符串
  28.      * @return MD5加密后的字符串(大写+数字)
  29.      */  
  30.   
  31.     public static String getMD5UpperCase(String s) {  
  32.         char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',  
  33.                 'A', 'B', 'C', 'D', 'E', 'F' };  
  34.         try {  
  35.             byte[] btInput = s.getBytes();  
  36.             // 获得MD5摘要算法的 MessageDigest 对象  
  37.             MessageDigest mdInst = MessageDigest.getInstance("MD5");  
  38.             // 使用指定的字节更新摘要  
  39.             mdInst.update(btInput);  
  40.             // 获得密文  
  41.             byte[] md = mdInst.digest();  
  42.             // 把密文转换成十六进制的字符串形式  
  43.             int j = md.length;  
  44.             char str[] = new char[j * 2];  
  45.             int k = 0;  
  46.             for (int i = 0; i < j; i++) {  
  47.                 byte byte0 = md[i];  
  48.                 str[k++] = hexDigits[byte0 >>> 4 & 0xf];  
  49.                 str[k++] = hexDigits[byte0 & 0xf];  
  50.             }  
  51.             return new String(str);  
  52.         } catch (Exception e) {  
  53.             e.printStackTrace();  
  54.             return null;  
  55.         }  
  56.     }  
  57.   
  58.     public static void main(String[] args) {  
  59.         String str = "\"我+Admin1234~!@#¥%……&*()\"";  
  60.         System.out.println(str);  
  61.         //"我+Admin1234~!@#¥%……&*()"  
  62.           
  63.         String s1 = getMD5LowerCase(str);  
  64.         System.out.println(s1);  
  65.         //6bb6f83c026357a15cdf12e5d6b2b310  
  66.           
  67.         String s2 = getMD5UpperCase(str);  
  68.         System.out.println(s2);  
  69.         //6BB6F83C026357A15CDF12E5D6B2B310  
  70.     }  
  71. }  

测试

db06c78d1e24cf708a14ce81c9b617ec

DB06C78D1E24CF708A14CE81C9B617EC

二、js 实现 MD5 加密

使用第三方库 crypto-js,示例代码如下:

XML/HTML代码
  1. <!DOCTYPE html>  
  2. <html lang="zh-CN">  
  3. <head>  
  4.     <meta charset="utf-8">  
  5.     <title>111</title>  
  6.     <style type="text/css">  
  7.         body{background:#2C91AE;}  
  8.     </style>  
  9. </head>  
  10. <body>  
  11.   
  12. </body>  
  13. <script src="http://172.16.102.156/public/crypto/crypto-js.min.js"></script>  
  14.   
  15. <!-- 中国文书网MD5加密封装方法调用 -->  
  16. <script type="text/javascript" src="md5.js"></script>  
  17. <script type="text/javascript">  
  18. /*** 刘新修本地md5加密方法封装(大写) ***/  
  19. // function md5(string) {  
  20. //     function rotateLeft(lValue, iShiftBits) {  
  21. //         return (lValue << iShiftBits) | (lValue >>> (32 - iShiftBits));  
  22. //     }  
  23. //     function addUnsigned(lX, lY) {  
  24. //         var lX4, lY4, lX8, lY8, lResult;  
  25. //         lX8 = (lX & 0x80000000);  
  26. //         lY8 = (lY & 0x80000000);  
  27. //         lX4 = (lX & 0x40000000);  
  28. //         lY4 = (lY & 0x40000000);  
  29. //         lResult = (lX & 0x3FFFFFFF) + (lY & 0x3FFFFFFF);  
  30. //         if (lX4 & lY4) {  
  31. //             return (lResult ^ 0x80000000 ^ lX8 ^ lY8);  
  32. //         }  
  33. //         if (lX4 | lY4) {  
  34. //             if (lResult & 0x40000000) {  
  35. //                 return (lResult ^ 0xC0000000 ^ lX8 ^ lY8);  
  36. //             } else {  
  37. //                 return (lResult ^ 0x40000000 ^ lX8 ^ lY8);  
  38. //             }  
  39. //         } else {  
  40. //             return (lResult ^ lX8 ^ lY8);  
  41. //         }  
  42. //     }  
  43.   
  44. //     function f(x, y, z) {  
  45. //         return (x & y) | ((~x) & z);  
  46. //     }  
  47.   
  48. //     function g(x, y, z) {  
  49. //         return (x & z) | (y & (~z));  
  50. //     }  
  51.   
  52. //     function h(x, y, z) {  
  53. //         return (x ^ y ^ z);  
  54. //     }  
  55.   
  56. //     function i(x, y, z) {  
  57. //         return (y ^ (x | (~z)));  
  58. //     }  
  59.   
  60. //     function FF(a, b, c, d, x, s, ac) {  
  61. //         a = addUnsigned(a, addUnsigned(addUnsigned(f(b, c, d), x), ac));  
  62. //         return addUnsigned(rotateLeft(a, s), b);  
  63. //     }  
  64.   
  65. //     function GG(a, b, c, d, x, s, ac) {  
  66. //         a = addUnsigned(a, addUnsigned(addUnsigned(g(b, c, d), x), ac));  
  67. //         return addUnsigned(rotateLeft(a, s), b);  
  68. //     }  
  69.   
  70. //     function HH(a, b, c, d, x, s, ac) {  
  71. //         a = addUnsigned(a, addUnsigned(addUnsigned(h(b, c, d), x), ac));  
  72. //         return addUnsigned(rotateLeft(a, s), b);  
  73. //     }  
  74.   
  75. //     function II(a, b, c, d, x, s, ac) {  
  76. //         a = addUnsigned(a, addUnsigned(addUnsigned(i(b, c, d), x), ac));  
  77. //         return addUnsigned(rotateLeft(a, s), b);  
  78. //     }  
  79.   
  80. //     function convertToWordArray(string) {  
  81. //         var lWordCount;  
  82. //         var lMessageLength = string.length;  
  83. //         var lNumberOfWords_temp1 = lMessageLength + 8;  
  84. //         var lNumberOfWords_temp2 = (lNumberOfWords_temp1 - (lNumberOfWords_temp1 % 64)) / 64;  
  85. //         var lNumberOfWords = (lNumberOfWords_temp2 + 1) * 16;  
  86. //         var lWordArray = new Array(lNumberOfWords - 1);  
  87. //         var lBytePosition = 0;  
  88. //         var lByteCount = 0;  
  89. //         while (lByteCount < lMessageLength) {  
  90. //             lWordCount = (lByteCount - (lByteCount % 4)) / 4;  
  91. //             lBytePosition = (lByteCount % 4) * 8;  
  92. //             lWordArray[lWordCount] = (lWordArray[lWordCount] | (string.charCodeAt(lByteCount) << lBytePosition));  
  93. //             lByteCount++;  
  94. //         }  
  95.   
  96. //         lWordCount = (lByteCount - (lByteCount % 4)) / 4;  
  97. //         lBytePosition = (lByteCount % 4) * 8;  
  98. //         lWordArray[lWordCount] = lWordArray[lWordCount] | (0x80 << lBytePosition);  
  99. //         lWordArray[lNumberOfWords - 2] = lMessageLength << 3;  
  100. //         lWordArray[lNumberOfWords - 1] = lMessageLength >>> 29;  
  101. //         return lWordArray;  
  102. //     }  
  103.   
  104. //     function wordToHex(lValue) {  
  105. //         var WordToHexValue = "", WordToHexValue_temp = "", lByte, lCount;  
  106. //         for (lCount = 0; lCount <= 3; lCount++) {  
  107. //             lByte = (lValue >>> (lCount * 8)) & 255;  
  108. //             WordToHexValue_temp = "0" + lByte.toString(16);  
  109. //             WordToHexValueWordToHexValue = WordToHexValue + WordToHexValue_temp.substr(WordToHexValue_temp.length - 2, 2);  
  110. //         }  
  111. //         return WordToHexValue;  
  112. //     }  
  113.   
  114. //     function utf8Encode(string) {  
  115. //         stringstring = string.replace(/\r\n/g, "\n");  
  116. //         var utftext = "";  
  117.   
  118. //         for (var n = 0; n < string.length; n++) {  
  119.   
  120. //             var c = string.charCodeAt(n);  
  121.   
  122. //             if (c < 128) {  
  123. //                 utftext += String.fromCharCode(c);  
  124. //             }  
  125. //             else if ((c > 127) && (c < 2048)) {  
  126. //                 utftext += String.fromCharCode((c >> 6) | 192);  
  127. //                 utftext += String.fromCharCode((c & 63) | 128);  
  128. //             }  
  129. //             else {  
  130. //                 utftext += String.fromCharCode((c >> 12) | 224);  
  131. //                 utftext += String.fromCharCode(((c >> 6) & 63) | 128);  
  132. //                 utftext += String.fromCharCode((c & 63) | 128);  
  133. //             }  
  134.   
  135. //         }  
  136.   
  137. //         return utftext;  
  138. //     }  
  139.   
  140. //     var x = [],  
  141. //         k, AA, BB, CC, DD, a, b, c, d,  
  142. //         S11 = 7, S12 = 12, S13 = 17, S14 = 22,  
  143. //         S21 = 5, S22 = 9 , S23 = 14, S24 = 20,  
  144. //         S31 = 4, S32 = 11, S33 = 16, S34 = 23,  
  145. //         S41 = 6, S42 = 10, S43 = 15, S44 = 21;  
  146.   
  147. //     string = utf8Encode(string);  
  148.   
  149. //     x = convertToWordArray(string);  
  150. //     // console.log(JSON.stringify(x))  
  151. //     // console.log(x.length)  
  152.   
  153. //     a = 0x67452301;  
  154. //     b = 0xEFCDAB89;  
  155. //     c = 0x98BADCFE;  
  156. //     d = 0x10325476;  
  157.   
  158. //     for (k = 0; k < x.length; k += 16) {  
  159. //         AA = a;  
  160. //         BB = b;  
  161. //         CC = c;  
  162. //         DD = d;  
  163. //         a = FF(a, b, c, d, x[k + 0], S11, 0xD76AA478);  
  164. //         d = FF(d, a, b, c, x[k + 1], S12, 0xE8C7B756);  
  165. //         c = FF(c, d, a, b, x[k + 2], S13, 0x242070DB);  
  166. //         b = FF(b, c, d, a, x[k + 3], S14, 0xC1BDCEEE);  
  167. //         a = FF(a, b, c, d, x[k + 4], S11, 0xF57C0FAF);  
  168. //         d = FF(d, a, b, c, x[k + 5], S12, 0x4787C62A);  
  169. //         c = FF(c, d, a, b, x[k + 6], S13, 0xA8304613);  
  170. //         b = FF(b, c, d, a, x[k + 7], S14, 0xFD469501);  
  171. //         a = FF(a, b, c, d, x[k + 8], S11, 0x698098D8);  
  172. //         d = FF(d, a, b, c, x[k + 9], S12, 0x8B44F7AF);  
  173. //         c = FF(c, d, a, b, x[k + 10], S13, 0xFFFF5BB1);  
  174. //         b = FF(b, c, d, a, x[k + 11], S14, 0x895CD7BE);  
  175. //         a = FF(a, b, c, d, x[k + 12], S11, 0x6B901122);  
  176. //         d = FF(d, a, b, c, x[k + 13], S12, 0xFD987193);  
  177. //         c = FF(c, d, a, b, x[k + 14], S13, 0xA679438E);  
  178. //         b = FF(b, c, d, a, x[k + 15], S14, 0x49B40821);  
  179. //         a = GG(a, b, c, d, x[k + 1], S21, 0xF61E2562);  
  180. //         d = GG(d, a, b, c, x[k + 6], S22, 0xC040B340);  
  181. //         c = GG(c, d, a, b, x[k + 11], S23, 0x265E5A51);  
  182. //         b = GG(b, c, d, a, x[k + 0], S24, 0xE9B6C7AA);  
  183. //         a = GG(a, b, c, d, x[k + 5], S21, 0xD62F105D);  
  184. //         d = GG(d, a, b, c, x[k + 10], S22, 0x2441453);  
  185. //         c = GG(c, d, a, b, x[k + 15], S23, 0xD8A1E681);  
  186. //         b = GG(b, c, d, a, x[k + 4], S24, 0xE7D3FBC8);  
  187. //         a = GG(a, b, c, d, x[k + 9], S21, 0x21E1CDE6);  
  188. //         d = GG(d, a, b, c, x[k + 14], S22, 0xC33707D6);  
  189. //         c = GG(c, d, a, b, x[k + 3], S23, 0xF4D50D87);  
  190. //         b = GG(b, c, d, a, x[k + 8], S24, 0x455A14ED);  
  191. //         a = GG(a, b, c, d, x[k + 13], S21, 0xA9E3E905);  
  192. //         d = GG(d, a, b, c, x[k + 2], S22, 0xFCEFA3F8);  
  193. //         c = GG(c, d, a, b, x[k + 7], S23, 0x676F02D9);  
  194. //         b = GG(b, c, d, a, x[k + 12], S24, 0x8D2A4C8A);  
  195. //         a = HH(a, b, c, d, x[k + 5], S31, 0xFFFA3942);  
  196. //         d = HH(d, a, b, c, x[k + 8], S32, 0x8771F681);  
  197. //         c = HH(c, d, a, b, x[k + 11], S33, 0x6D9D6122);  
  198. //         b = HH(b, c, d, a, x[k + 14], S34, 0xFDE5380C);  
  199. //         a = HH(a, b, c, d, x[k + 1], S31, 0xA4BEEA44);  
  200. //         d = HH(d, a, b, c, x[k + 4], S32, 0x4BDECFA9);  
  201. //         c = HH(c, d, a, b, x[k + 7], S33, 0xF6BB4B60);  
  202. //         b = HH(b, c, d, a, x[k + 10], S34, 0xBEBFBC70);  
  203. //         a = HH(a, b, c, d, x[k + 13], S31, 0x289B7EC6);  
  204. //         d = HH(d, a, b, c, x[k + 0], S32, 0xEAA127FA);  
  205. //         c = HH(c, d, a, b, x[k + 3], S33, 0xD4EF3085);  
  206. //         b = HH(b, c, d, a, x[k + 6], S34, 0x4881D05);  
  207. //         a = HH(a, b, c, d, x[k + 9], S31, 0xD9D4D039);  
  208. //         d = HH(d, a, b, c, x[k + 12], S32, 0xE6DB99E5);  
  209. //         c = HH(c, d, a, b, x[k + 15], S33, 0x1FA27CF8);  
  210. //         b = HH(b, c, d, a, x[k + 2], S34, 0xC4AC5665);  
  211. //         a = II(a, b, c, d, x[k + 0], S41, 0xF4292244);  
  212. //         d = II(d, a, b, c, x[k + 7], S42, 0x432AFF97);  
  213. //         c = II(c, d, a, b, x[k + 14], S43, 0xAB9423A7);  
  214. //         b = II(b, c, d, a, x[k + 5], S44, 0xFC93A039);  
  215. //         a = II(a, b, c, d, x[k + 12], S41, 0x655B59C3);  
  216. //         d = II(d, a, b, c, x[k + 3], S42, 0x8F0CCC92);  
  217. //         c = II(c, d, a, b, x[k + 10], S43, 0xFFEFF47D);  
  218. //         b = II(b, c, d, a, x[k + 1], S44, 0x85845DD1);  
  219. //         a = II(a, b, c, d, x[k + 8], S41, 0x6FA87E4F);  
  220. //         d = II(d, a, b, c, x[k + 15], S42, 0xFE2CE6E0);  
  221. //         c = II(c, d, a, b, x[k + 6], S43, 0xA3014314);  
  222. //         b = II(b, c, d, a, x[k + 13], S44, 0x4E0811A1);  
  223. //         a = II(a, b, c, d, x[k + 4], S41, 0xF7537E82);  
  224. //         d = II(d, a, b, c, x[k + 11], S42, 0xBD3AF235);  
  225. //         c = II(c, d, a, b, x[k + 2], S43, 0x2AD7D2BB);  
  226. //         b = II(b, c, d, a, x[k + 9], S44, 0xEB86D391);  
  227. //         a = addUnsigned(a, AA);  
  228. //         b = addUnsigned(b, BB);  
  229. //         c = addUnsigned(c, CC);  
  230. //         d = addUnsigned(d, DD);  
  231. //     }  
  232.   
  233. //     var temp = wordToHex(a) + wordToHex(b) + wordToHex(c) + wordToHex(d);  
  234. //     return temp.toUpperCase();  
  235. // };  
  236.   
  237. // console.log('刘新修本地封装/md5加密(--测试--)', md5('测试'))  
  238. //"DB06C78D1E24CF708A14CE81C9B617EC"  
  239.   
  240.   
  241. /*** 第三方类库CryptoJS.MD5()方法 || 和中国文书网测试结果一致(小写) ***/  
  242. console.log('刘新修/CryptoJS.MD5()-加密(--测试--)\n', CryptoJS.MD5('测试').toString())  
  243. </script>  
  244. </html>  

中网文书网/MD5加密(--测试--) db06c78d1e24cf708a14ce81c9b617ec

刘新修/CryptoJS.MD5()-加密(--测试--) db06c78d1e24cf708a14ce81c9b617ec

 

下载文件
这个文件只能在登入之后下载。请先 注册登入

PHP/Java/Go | 评论(0) | 引用(0) | 阅读(982)