问题说明

MD5的作用是让大容量信息在用数字签名软件签署私人密钥前被"压缩"成一种保密的格式(就是把一个任意长度的字节串变换成一定长的十六进制数字串)。经过md5后的字符数组中含有负数元素,因此如果将这个字符数组进行转换成字符串,查询GB2312或者UTF-8都是不合适的会出现乱码。可以通过“Base64”编码解决字节数组转字符串的乱码问题。

参考链接:

Code Sample

Java code
  1. package buct.edu.cn;
  2. import java.io.UnsupportedEncodingException;
  3. import java.security.MessageDigest;
  4. import java.security.NoSuchAlgorithmException;
  5. import java.util.Arrays;
  6. import Decoder.BASE64Encoder;
  7. /**
  8. * @author yuvmtest
  9. * 获取base64位编码的MD5值
  10. */
  11. public class Demo {
  12. public static void main(String[] args) throws NoSuchAlgorithmException, UnsupportedEncodingException{
  13. System.out.println("------------------------");
  14. String data = "md5Test";
  15. MessageDigest md = MessageDigest.getInstance("md5");
  16. byte[] md5 = md.digest(data.getBytes());
  17. System.out.println(Arrays.toString(md5));
  18. BASE64Encoder be = new BASE64Encoder();
  19. String base64 = be.encode(md5);
  20. System.out.println(base64);
  21. System.out.println(base64.toUpperCase());
  22. System.out.println("------------------------");
  23. }
  24. }
JavaScript code
  1. <html>
  2. <head>
  3. <title>md5_Test</title>
  4. <script>
  5. /*
  6. * A JavaScript implementation of the RSA Data Security, Inc. MD5 Message
  7. * Digest Algorithm, as defined in RFC 1321.
  8. * Version 2.2 Copyright (C) Paul Johnston 1999 - 2009
  9. * Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet
  10. * Distributed under the BSD License
  11. * See http://pajhome.org.uk/crypt/md5 for more info.
  12. */
  13. /*
  14. * Configurable variables. You may need to tweak these to be compatible with
  15. * the server-side, but the defaults work in most cases.
  16. */
  17. var hexcase = 0; /* hex output format. 0 - lowercase; 1 - uppercase */
  18. var b64pad = "="; /* base-64 pad character. "=" for strict RFC compliance 注意此处需要添加 ‘=’ 填补空缺,确保生成的MD5值位24位 */
  19. /*
  20. * These are the functions you'll usually want to call
  21. * They take string arguments and return either hex or base-64 encoded strings
  22. */
  23. function hex_md5(s) { return rstr2hex(rstr_md5(str2rstr_utf8(s))); }
  24. function b64_md5(s) { return rstr2b64(rstr_md5(str2rstr_utf8(s))); }
  25. function any_md5(s, e) { return rstr2any(rstr_md5(str2rstr_utf8(s)), e); }
  26. function hex_hmac_md5(k, d)
  27. { return rstr2hex(rstr_hmac_md5(str2rstr_utf8(k), str2rstr_utf8(d))); }
  28. function b64_hmac_md5(k, d)
  29. { return rstr2b64(rstr_hmac_md5(str2rstr_utf8(k), str2rstr_utf8(d))); }
  30. function any_hmac_md5(k, d, e)
  31. { return rstr2any(rstr_hmac_md5(str2rstr_utf8(k), str2rstr_utf8(d)), e); }
  32. /*
  33. * Perform a simple self-test to see if the VM is working
  34. */
  35. function md5_vm_test()
  36. {
  37. return hex_md5("abc").toLowerCase() == "900150983cd24fb0d6963f7d28e17f72";
  38. }
  39. /*
  40. * Calculate the MD5 of a raw string
  41. */
  42. function rstr_md5(s)
  43. {
  44. return binl2rstr(binl_md5(rstr2binl(s), s.length * 8));
  45. }
  46. /*
  47. * Calculate the HMAC-MD5, of a key and some data (raw strings)
  48. */
  49. function rstr_hmac_md5(key, data)
  50. {
  51. var bkey = rstr2binl(key);
  52. if(bkey.length > 16) bkey = binl_md5(bkey, key.length * 8);
  53. var ipad = Array(16), opad = Array(16);
  54. for(var i = 0; i < 16; i++)
  55. {
  56. ipad[i] = bkey[i] ^ 0x36363636;
  57. opad[i] = bkey[i] ^ 0x5C5C5C5C;
  58. }
  59. var hash = binl_md5(ipad.concat(rstr2binl(data)), 512 + data.length * 8);
  60. return binl2rstr(binl_md5(opad.concat(hash), 512 + 128));
  61. }
  62. /*
  63. * Convert a raw string to a hex string
  64. */
  65. function rstr2hex(input)
  66. {
  67. try { hexcase } catch(e) { hexcase=0; }
  68. var hex_tab = hexcase ? "0123456789ABCDEF" : "0123456789abcdef";
  69. var output = "";
  70. var x;
  71. for(var i = 0; i < input.length; i++)
  72. {
  73. x = input.charCodeAt(i);
  74. output += hex_tab.charAt((x >>> 4) & 0x0F)
  75. + hex_tab.charAt( x & 0x0F);
  76. }
  77. return output;
  78. }
  79. /*
  80. * Convert a raw string to a base-64 string
  81. */
  82. function rstr2b64(input)
  83. {
  84. try { b64pad } catch(e) { b64pad=''; }
  85. var tab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  86. var output = "";
  87. var len = input.length;
  88. for(var i = 0; i < len; i += 3)
  89. {
  90. var triplet = (input.charCodeAt(i) << 16)
  91. | (i + 1 < len ? input.charCodeAt(i+1) << 8 : 0)
  92. | (i + 2 < len ? input.charCodeAt(i+2) : 0);
  93. for(var j = 0; j < 4; j++)
  94. {
  95. if(i * 8 + j * 6 > input.length * 8) output += b64pad;
  96. else output += tab.charAt((triplet >>> 6*(3-j)) & 0x3F);
  97. }
  98. }
  99. return output;
  100. }
  101. /*
  102. * Convert a raw string to an arbitrary string encoding
  103. */
  104. function rstr2any(input, encoding)
  105. {
  106. var divisor = encoding.length;
  107. var i, j, q, x, quotient;
  108. /* Convert to an array of 16-bit big-endian values, forming the dividend */
  109. var dividend = Array(Math.ceil(input.length / 2));
  110. for(i = 0; i < dividend.length; i++)
  111. {
  112. dividend[i] = (input.charCodeAt(i * 2) << 8) | input.charCodeAt(i * 2 + 1);
  113. }
  114. /*
  115. * Repeatedly perform a long division. The binary array forms the dividend,
  116. * the length of the encoding is the divisor. Once computed, the quotient
  117. * forms the dividend for the next step. All remainders are stored for later
  118. * use.
  119. */
  120. var full_length = Math.ceil(input.length * 8 /
  121. (Math.log(encoding.length) / Math.log(2)));
  122. var remainders = Array(full_length);
  123. for(j = 0; j < full_length; j++)
  124. {
  125. quotient = Array();
  126. x = 0;
  127. for(i = 0; i < dividend.length; i++)
  128. {
  129. x = (x << 16) + dividend[i];
  130. q = Math.floor(x / divisor);
  131. x -= q * divisor;
  132. if(quotient.length > 0 || q > 0)
  133. quotient[quotient.length] = q;
  134. }
  135. remainders[j] = x;
  136. dividend = quotient;
  137. }
  138. /* Convert the remainders to the output string */
  139. var output = "";
  140. for(i = remainders.length - 1; i >= 0; i--)
  141. output += encoding.charAt(remainders[i]);
  142. return output;
  143. }
  144. /*
  145. * Encode a string as utf-8.
  146. * For efficiency, this assumes the input is valid utf-16.
  147. */
  148. function str2rstr_utf8(input)
  149. {
  150. var output = "";
  151. var i = -1;
  152. var x, y;
  153. while(++i < input.length)
  154. {
  155. /* Decode utf-16 surrogate pairs */
  156. x = input.charCodeAt(i);
  157. y = i + 1 < input.length ? input.charCodeAt(i + 1) : 0;
  158. if(0xD800 <= x && x <= 0xDBFF && 0xDC00 <= y && y <= 0xDFFF)
  159. {
  160. x = 0x10000 + ((x & 0x03FF) << 10) + (y & 0x03FF);
  161. i++;
  162. }
  163. /* Encode output as utf-8 */
  164. if(x <= 0x7F)
  165. output += String.fromCharCode(x);
  166. else if(x <= 0x7FF)
  167. output += String.fromCharCode(0xC0 | ((x >>> 6 ) & 0x1F),
  168. 0x80 | ( x & 0x3F));
  169. else if(x <= 0xFFFF)
  170. output += String.fromCharCode(0xE0 | ((x >>> 12) & 0x0F),
  171. 0x80 | ((x >>> 6 ) & 0x3F),
  172. 0x80 | ( x & 0x3F));
  173. else if(x <= 0x1FFFFF)
  174. output += String.fromCharCode(0xF0 | ((x >>> 18) & 0x07),
  175. 0x80 | ((x >>> 12) & 0x3F),
  176. 0x80 | ((x >>> 6 ) & 0x3F),
  177. 0x80 | ( x & 0x3F));
  178. }
  179. return output;
  180. }
  181. /*
  182. * Encode a string as utf-16
  183. */
  184. function str2rstr_utf16le(input)
  185. {
  186. var output = "";
  187. for(var i = 0; i < input.length; i++)
  188. output += String.fromCharCode( input.charCodeAt(i) & 0xFF,
  189. (input.charCodeAt(i) >>> 8) & 0xFF);
  190. return output;
  191. }
  192. function str2rstr_utf16be(input)
  193. {
  194. var output = "";
  195. for(var i = 0; i < input.length; i++)
  196. output += String.fromCharCode((input.charCodeAt(i) >>> 8) & 0xFF,
  197. input.charCodeAt(i) & 0xFF);
  198. return output;
  199. }
  200. /*
  201. * Convert a raw string to an array of little-endian words
  202. * Characters >255 have their high-byte silently ignored.
  203. */
  204. function rstr2binl(input)
  205. {
  206. var output = Array(input.length >> 2);
  207. for(var i = 0; i < output.length; i++)
  208. output[i] = 0;
  209. for(var i = 0; i < input.length * 8; i += 8)
  210. output[i>>5] |= (input.charCodeAt(i / 8) & 0xFF) << (i%32);
  211. return output;
  212. }
  213. /*
  214. * Convert an array of little-endian words to a string
  215. */
  216. function binl2rstr(input)
  217. {
  218. var output = "";
  219. for(var i = 0; i < input.length * 32; i += 8)
  220. output += String.fromCharCode((input[i>>5] >>> (i % 32)) & 0xFF);
  221. return output;
  222. }
  223. /*
  224. * Calculate the MD5 of an array of little-endian words, and a bit length.
  225. */
  226. function binl_md5(x, len)
  227. {
  228. /* append padding */
  229. x[len >> 5] |= 0x80 << ((len) % 32);
  230. x[(((len + 64) >>> 9) << 4) + 14] = len;
  231. var a = 1732584193;
  232. var b = -271733879;
  233. var c = -1732584194;
  234. var d = 271733878;
  235. for(var i = 0; i < x.length; i += 16)
  236. {
  237. var olda = a;
  238. var oldb = b;
  239. var oldc = c;
  240. var oldd = d;
  241. a = md5_ff(a, b, c, d, x[i+ 0], 7 , -680876936);
  242. d = md5_ff(d, a, b, c, x[i+ 1], 12, -389564586);
  243. c = md5_ff(c, d, a, b, x[i+ 2], 17, 606105819);
  244. b = md5_ff(b, c, d, a, x[i+ 3], 22, -1044525330);
  245. a = md5_ff(a, b, c, d, x[i+ 4], 7 , -176418897);
  246. d = md5_ff(d, a, b, c, x[i+ 5], 12, 1200080426);
  247. c = md5_ff(c, d, a, b, x[i+ 6], 17, -1473231341);
  248. b = md5_ff(b, c, d, a, x[i+ 7], 22, -45705983);
  249. a = md5_ff(a, b, c, d, x[i+ 8], 7 , 1770035416);
  250. d = md5_ff(d, a, b, c, x[i+ 9], 12, -1958414417);
  251. c = md5_ff(c, d, a, b, x[i+10], 17, -42063);
  252. b = md5_ff(b, c, d, a, x[i+11], 22, -1990404162);
  253. a = md5_ff(a, b, c, d, x[i+12], 7 , 1804603682);
  254. d = md5_ff(d, a, b, c, x[i+13], 12, -40341101);
  255. c = md5_ff(c, d, a, b, x[i+14], 17, -1502002290);
  256. b = md5_ff(b, c, d, a, x[i+15], 22, 1236535329);
  257. a = md5_gg(a, b, c, d, x[i+ 1], 5 , -165796510);
  258. d = md5_gg(d, a, b, c, x[i+ 6], 9 , -1069501632);
  259. c = md5_gg(c, d, a, b, x[i+11], 14, 643717713);
  260. b = md5_gg(b, c, d, a, x[i+ 0], 20, -373897302);
  261. a = md5_gg(a, b, c, d, x[i+ 5], 5 , -701558691);
  262. d = md5_gg(d, a, b, c, x[i+10], 9 , 38016083);
  263. c = md5_gg(c, d, a, b, x[i+15], 14, -660478335);
  264. b = md5_gg(b, c, d, a, x[i+ 4], 20, -405537848);
  265. a = md5_gg(a, b, c, d, x[i+ 9], 5 , 568446438);
  266. d = md5_gg(d, a, b, c, x[i+14], 9 , -1019803690);
  267. c = md5_gg(c, d, a, b, x[i+ 3], 14, -187363961);
  268. b = md5_gg(b, c, d, a, x[i+ 8], 20, 1163531501);
  269. a = md5_gg(a, b, c, d, x[i+13], 5 , -1444681467);
  270. d = md5_gg(d, a, b, c, x[i+ 2], 9 , -51403784);
  271. c = md5_gg(c, d, a, b, x[i+ 7], 14, 1735328473);
  272. b = md5_gg(b, c, d, a, x[i+12], 20, -1926607734);
  273. a = md5_hh(a, b, c, d, x[i+ 5], 4 , -378558);
  274. d = md5_hh(d, a, b, c, x[i+ 8], 11, -2022574463);
  275. c = md5_hh(c, d, a, b, x[i+11], 16, 1839030562);
  276. b = md5_hh(b, c, d, a, x[i+14], 23, -35309556);
  277. a = md5_hh(a, b, c, d, x[i+ 1], 4 , -1530992060);
  278. d = md5_hh(d, a, b, c, x[i+ 4], 11, 1272893353);
  279. c = md5_hh(c, d, a, b, x[i+ 7], 16, -155497632);
  280. b = md5_hh(b, c, d, a, x[i+10], 23, -1094730640);
  281. a = md5_hh(a, b, c, d, x[i+13], 4 , 681279174);
  282. d = md5_hh(d, a, b, c, x[i+ 0], 11, -358537222);
  283. c = md5_hh(c, d, a, b, x[i+ 3], 16, -722521979);
  284. b = md5_hh(b, c, d, a, x[i+ 6], 23, 76029189);
  285. a = md5_hh(a, b, c, d, x[i+ 9], 4 , -640364487);
  286. d = md5_hh(d, a, b, c, x[i+12], 11, -421815835);
  287. c = md5_hh(c, d, a, b, x[i+15], 16, 530742520);
  288. b = md5_hh(b, c, d, a, x[i+ 2], 23, -995338651);
  289. a = md5_ii(a, b, c, d, x[i+ 0], 6 , -198630844);
  290. d = md5_ii(d, a, b, c, x[i+ 7], 10, 1126891415);
  291. c = md5_ii(c, d, a, b, x[i+14], 15, -1416354905);
  292. b = md5_ii(b, c, d, a, x[i+ 5], 21, -57434055);
  293. a = md5_ii(a, b, c, d, x[i+12], 6 , 1700485571);
  294. d = md5_ii(d, a, b, c, x[i+ 3], 10, -1894986606);
  295. c = md5_ii(c, d, a, b, x[i+10], 15, -1051523);
  296. b = md5_ii(b, c, d, a, x[i+ 1], 21, -2054922799);
  297. a = md5_ii(a, b, c, d, x[i+ 8], 6 , 1873313359);
  298. d = md5_ii(d, a, b, c, x[i+15], 10, -30611744);
  299. c = md5_ii(c, d, a, b, x[i+ 6], 15, -1560198380);
  300. b = md5_ii(b, c, d, a, x[i+13], 21, 1309151649);
  301. a = md5_ii(a, b, c, d, x[i+ 4], 6 , -145523070);
  302. d = md5_ii(d, a, b, c, x[i+11], 10, -1120210379);
  303. c = md5_ii(c, d, a, b, x[i+ 2], 15, 718787259);
  304. b = md5_ii(b, c, d, a, x[i+ 9], 21, -343485551);
  305. a = safe_add(a, olda);
  306. b = safe_add(b, oldb);
  307. c = safe_add(c, oldc);
  308. d = safe_add(d, oldd);
  309. }
  310. return Array(a, b, c, d);
  311. }
  312. /*
  313. * These functions implement the four basic operations the algorithm uses.
  314. */
  315. function md5_cmn(q, a, b, x, s, t)
  316. {
  317. return safe_add(bit_rol(safe_add(safe_add(a, q), safe_add(x, t)), s),b);
  318. }
  319. function md5_ff(a, b, c, d, x, s, t)
  320. {
  321. return md5_cmn((b & c) | ((~b) & d), a, b, x, s, t);
  322. }
  323. function md5_gg(a, b, c, d, x, s, t)
  324. {
  325. return md5_cmn((b & d) | (c & (~d)), a, b, x, s, t);
  326. }
  327. function md5_hh(a, b, c, d, x, s, t)
  328. {
  329. return md5_cmn(b ^ c ^ d, a, b, x, s, t);
  330. }
  331. function md5_ii(a, b, c, d, x, s, t)
  332. {
  333. return md5_cmn(c ^ (b | (~d)), a, b, x, s, t);
  334. }
  335. /*
  336. * Add integers, wrapping at 2^32. This uses 16-bit operations internally
  337. * to work around bugs in some JS interpreters.
  338. */
  339. function safe_add(x, y)
  340. {
  341. var lsw = (x & 0xFFFF) + (y & 0xFFFF);
  342. var msw = (x >> 16) + (y >> 16) + (lsw >> 16);
  343. return (msw << 16) | (lsw & 0xFFFF);
  344. }
  345. /*
  346. * Bitwise rotate a 32-bit number to the left.
  347. */
  348. function bit_rol(num, cnt)
  349. {
  350. return (num << cnt) | (num >>> (32 - cnt));
  351. }
  352. </script>
  353. <link rel="stylesheet" type="text/css" href="bearfly.css" />
  354. </head>
  355. <body>
  356. <script type="text/javascript">
  357. document.write("hex_md5 MD5: ",hex_md5("md5Test"));
  358. document.write("<br / >");
  359. document.write("b64_md5 MD5: ",b64_md5("md5Test"));
  360. document.write("<br / >");
  361. document.write("str_md5 MD5: ",any_md5("md5Test","utf-8"));
  362. document.write("<br / >");
  363. document.write("hex_hmac_md5 MD5: ",hex_hmac_md5("md5Test","123"));
  364. </script>
  365. </body>
  366. </html>

说明

b64_md5加密Java、JavaScript实现的更多相关文章

  1. libj 0.8.2 发布,Java/JavaScript API 的 C++ 实现

    libj 0.8.2 增加了一些新的字符串相关的方法. libj 是一个跨平台的运行库,相当于提供了类似 Java/JavaScript API.libj 的内存管理是自动的,基于 shared_pt ...

  2. 【知识积累】DES算法之C#加密&Java解密

    一.前言 在项目需要添加安全模块,客户端调用服务端发布的service必须要经过验证,加密算法采用DES,客户端采用C#进行加密,服务端使用Java进行解密.废话不多说,直接上代码. 二.客户端 客户 ...

  3. Atitit.实现继承的原理and方法java javascript .net c# php ...

    Atitit.实现继承的原理and方法java javascript .net c# php ... 1. 实现继承的问题 1 2. 如何拷贝基类方法?采用prototype原型方式,通过冒充对象 1 ...

  4. paip.提升安全性----Des加密 java php python的实现总结

    paip.提升安全性----Des加密 java php python的实现总结 ///////////    uapi         private static String decryptBy ...

  5. sha1加密java代码

    sha1 加密 java代码 public static String getSha1(String str){ if(str==null||str.length()==0){ return null ...

  6. 分页 工具类 前后台代码 Java JavaScript (ajax) 实现 讲解

    [博客园cnblogs笔者m-yb原创, 转载请加本文博客链接,笔者github: https://github.com/mayangbo666,公众号aandb7,QQ群927113708]http ...

  7. MD5、SHA1加密java 16位32位

    MD5.SHA1加密java 16位32位 import java.math.BigInteger; import java.security.MessageDigest; public class ...

  8. java editor template Eclipse中的快速Java\JavaScript代码模板使用

    java editor template Eclipse中的快速Java\JavaScript代码模板使用 学习了:http://technicalsearch.iteye.com/blog/2150 ...

  9. DES加密 java与.net可以相互加密解密两种方法

    DES加密 java与.net可以相互加密解密两种方法 https://www.cnblogs.com/DrWang/archive/2011/03/30/2000124.html sun.misc. ...

随机推荐

  1. 51nod_1714:B君的游戏(博弈 sg打表)

    题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1714 nim游戏的一个变形,需要打出sg函数的表 #incl ...

  2. [noip 2015]运输计划 [LCA][树链剖分]

    用了luogu上的题目描述 题目背景 公元 2044 年,人类进入了宇宙纪元. 题目描述 L 国有 n 个星球,还有 n-1 条双向航道,每条航道建立在两个星球之间,这 n-1 条航道连通了 L 国的 ...

  3. linux执行sh报错:$’\r’: 未找到命令的解决

    背景 执行.sh脚本时出现$'\r': 未找到命令, 原因 是因为命令直接从windows 复制过来导致的 解决 yum install dos2unix dos2unix **.sh 进行转换 再次 ...

  4. Go语言学习笔记(四)结构体struct & 接口Interface & 反射

    加 Golang学习 QQ群共同学习进步成家立业工作 ^-^ 群号:96933959 结构体struct struct 用来自定义复杂数据结构,可以包含多个字段(属性),可以嵌套: go中的struc ...

  5. TCP/IP协议之ping和traceroute

    Ping程序就是调用的就是ICMP报文.利用的是ICMP的应答和回显请求.来看下具体的ping报文. Request的报文类型为8 Reply的类型为0 通过具体的ping报文可以看到ping报文的大 ...

  6. IBATIS动态SQL(1)

    转:IBATIS动态SQL 直接使用JDBC一个非常普遍的问题就是动态SQL.使用参数值.参数本身和数据列都是动态SQL,通常是非常困难的.典型的解决办法就是用上一堆的IF-ELSE条件语句和一连串的 ...

  7. ORACLE - 系统参数调整

    一.内存调整 oracle 11g中,ORACLE把SGA与PGA统一管理,总和为memory_target参数的设定,也就是MAX(SGA+PGA)<= memory_target(当然可以在 ...

  8. Js 获取时间戳

    //获取时间戳 单位:秒: //1. 获取当前时间戳 function getUnixTime(){ var date = new Date(); //使用getTime方法: var unix_ti ...

  9. C++ STL 双端队列deque详解

    一.解释 Deque(双端队列)是一种具有队列和栈的性质的数据结构.双端队列的元素可以从两端弹出,其限定插入和删除操作在表的两端进行. 二.常用操作: 1.头文件 #include <deque ...

  10. HDU5727 Necklace(二分图匹配)

    Problem Description SJX has 2*N magic gems. N of them have Yin energy inside while others have Yang ...