1 / 24
文档名称:

MD5加解密及测试方法.doc

格式:doc   页数:24页
下载后只包含 1 个 DOC 格式的文档,没有任何的图纸或源代码,查看文件列表

如果您已付费下载过本站文档,您可以点这里二次下载

分享

预览

MD5加解密及测试方法.doc

上传人:xgs758698 2015/5/23 文件大小:0 KB

下载得到文件列表

MD5加解密及测试方法.doc

相关文档

文档介绍

文档介绍:Java实现MD5加密解密类收藏
转载请注明:来自http://blog./M_ChangGong/ 作者:张燕广
Java实现MD5加密以及解密类,附带测试类,具体见代码。
MD5加密解密类——MyMD5Util,代码如下:
view plaincopy to clipboardprint?
01..;
02.
;
;
;
;
;
08.
class MyMD5Util {
10.
11. private static final String HEX_NUMS_STR="0123456789ABCDEF";
12. private static final Integer SALT_LENGTH = 12;
13.
14. /**
15. * 将16进制字符串转换成字节数组
16. * ***@param hex
17. * ***@return
18. */
19. public static byte[] hexStringToByte(String hex) {
20. int len = (() / 2);
21. byte[] result = new byte[len];
22. char[] hexChars = ();
23. for (int i = 0; i < len; i++) {
24. int pos = i * 2;
25. result[i] = (byte) ((hexChars[pos]) << 4
26. | (hexChars[pos + 1]));
27. }
28. return result;
29. }
30.
31.
32. /**
33. * 将指定byte数组转换成16进制字符串
34. * ***@param b
35. * ***@return
36. */
37. public static String byteToHexString(byte[] b) {
38. StringBuffer hexString = new StringBuffer();
39. for (int i = 0; i < ; i++) {
40. String hex = (b[i] & 0xFF);
41. if (() == 1) {
42. hex = '0' + hex;
43. }
44. (());
45. }
46. return ();
47. }
48.
49. /**
50. * 验证口令是否合法
51. * ***@param password
52. * ***@param passwordInDb
53. * ***@return
54. * ***@throws NoSuchAlgorithmException
55. * ***@throws UnsupportedEncodingException
56. */
57. public static boolean validPassword(String password, String passwordInDb)
58. throws NoSuchAlgorithmException, UnsupportedEncodingException {
59. //将16进制字符串格式口令转换成字节数组
60. byte[] pwdInDb = hexStringToByte(passwordInDb);
61. //声明盐变量
62. byte[] salt = new byte[SALT_LENGTH];
63. //将盐从数据库中保存的口令字节数组中提取出来
64. System.