1. package com.jackie.security;  
2.
3. import java.io.ByteArrayInputStream;
4. import java.io.File;
5. import java.io.FileInputStream;
6. import java.io.FileOutputStream;
7. import java.security.DigestInputStream;
8. import java.security.DigestOutputStream;
9. import java.security.MessageDigest;
10. import java.util.Arrays;
11.
12. import javax.crypto.Mac;
13. import javax.crypto.SecretKey;
14. import javax.crypto.SecretKeyFactory;
15. import javax.crypto.spec.PBEKeySpec;
16.
17. /**
18. * 消息摘要是一种算法:无论原始数据多长,消息摘要的结果都是固定长度的;是一种不可逆的算法
19. * 原始数据任意bit位的变化,都会导致消息摘要的结果有很大的不同,且根据结果推算出原始数据的概率极低。
20. * 消息摘要可以看作原始数据的指纹,指纹不同则原始数据不同。
21. */
22. public class MD5 {
23.
24.
25. public static void main(String[] args) throws Exception {
26. "中国oP……&*()…&802134…");
27.
28. "中国oP……&*()…&802134…");
29.
30. md5File();
31. }
32.
33. /**
34. * 使用MAC 算法的 消息摘要
35. * @param data
36. * @throws Exception
37. */
38. public static void encodeByMAC(String data) throws Exception{
39. // KeyGenerator keyGen = KeyGenerator.getInstance("HmacMD5");
40. // SecretKey key = keyGen.generateKey(); //这个每次生成的key不一样, 此处不能使用
41.
42. new PBEKeySpec("randomkey^(^&*^%$".toCharArray());
43. "PBEWithMD5AndDES");
44. SecretKey key = keyFactory.generateSecret(keySpec);
45.
46. /*
47. * 此类提供“消息验证码”(Message Authentication Code,MAC)算法的功能。
48. * MAC 基于秘密密钥提供一种方式来检查在不可靠介质上进行传输或存储的信息的完整性。
49. * 通常,消息验证码在共享秘密密钥的两个参与者之间使用,以验证这两者之间传输的信息。
50. * 基于加密哈希函数的 MAC 机制也叫作 HMAC。结合秘密共享密钥,
51. * HMAC 可以用于任何加密哈希函数(如 MD5 或 SHA-1)
52. */
53. "HmacMD5");
54. mac.init(key);
55. byte[] dest = mac.doFinal(data.getBytes());
56. System.out.println(dest.length);
57. "MAC摘要:" + Arrays.toString(dest));
58. }
59.
60. /**
61. * md5加密 使用消息摘要MessageDigest 处理
62. * @throws Exception
63. */
64. public static String encodeByMd5(String str) throws Exception{
65. MessageDigest md5;
66. "MD5");
67.
68. //先更新摘要
69. byte[] digest = md5.digest(); //再通过执行诸如填充之类的最终操作完成哈希计算。在调用此方法之后,摘要被重置。
70.
71. /*
72. * 使用指定的 byte 数组对摘要进行最后更新,然后完成摘要计算。
73. * 也就是说,此方法首先调用 update(input),
74. * 向 update 方法传递 input 数组,然后调用 digest()。
75. */
76. // byte[] digest = md5.digest(str.getBytes());
77.
78. String hex = toHex(digest);
79. "MD5摘要:" + hex);
80. return hex;
81. }
82.
83. /**
84. * 文件数据摘要
85. * @throws Exception
86. */
87. public static void md5File() throws Exception {
88. "MD5");
89. new DigestOutputStream(new FileOutputStream(new File("abc.txt")), messageDigest);
90. "中华人民……&())f*(214)admin*".getBytes());
91. dos.close();
92. byte[] digest = messageDigest.digest();
93. "使用流写文件,该文件的摘要为:" + toHex(digest));
94.
95.
96. new DigestInputStream(new FileInputStream(new File("abc.txt")), messageDigest);
97. byte[] buf = new byte[100];
98. int len;
99. while ((len = dis.read(buf)) != -1) {
100. "读取到的数据为:" + new String(buf, 0, len));
101. }
102. dis.close();
103. byte[] digest2 = messageDigest.digest();
104. //当流读取完毕,即将文件读完了, 这时的摘要 才与 写入时的 一样
105. "使用流读文件,该文件的摘要为:" + toHex(digest2));
106. }
107.
108. /**
109. * md5 摘要转16进制
110. * @param digest
111. * @return
112. */
113. private static String toHex(byte[] digest) {
114. new StringBuilder();
115. int len = digest.length;
116.
117. null;
118. for (int i = 0; i < len; i++) {
119. // out = Integer.toHexString(0xFF & digest[i] + 0xABCDEF); //加任意 salt
120. 0xFF & digest[i]);//原始方法
121. if (out.length() == 1) {
122. "0");//如果为1位 前面补个0
123. }
124. sb.append(out);
125. }
126. return sb.toString();
127. }
128.
129. }