代码实现:

  1. import java.io.File;
  2. import java.io.FileInputStream;
  3. import java.io.IOException;
  4. import java.security.MessageDigest;
  5. public class MD5Util {
  6. static char hexdigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
  7. /**
  8. * @funcion 对文件全文生成MD5摘要
  9. * @param file:要加密的文件
  10. * @return MD5摘要码
  11. */
  12. public static String getMD5(File file) {
  13. FileInputStream fis = null;
  14. try {
  15. MessageDigest md = MessageDigest.getInstance("MD5");
  16. fis = new FileInputStream(file);
  17. byte[] buffer = new byte[2048];
  18. int length = -1;
  19. while ((length = fis.read(buffer)) != -1) {
  20. md.update(buffer, 0, length);
  21. }
  22. byte[] b = md.digest();
  23. return byteToHexString(b);
  24. } catch (Exception e) {
  25. e.printStackTrace();
  26. return null;
  27. } finally {
  28. try {
  29. fis.close();
  30. } catch (IOException e) {
  31. e.printStackTrace();
  32. }
  33. }
  34. }
  35. /**
  36. * @function 把byte[]数组转换成十六进制字符串表示形式
  37. * @param tmp  要转换的byte[]
  38. * @return 十六进制字符串表示形式
  39. */
  40. private static String byteToHexString(byte[] tmp) {
  41. String s;
  42. // 用字节表示就是 16 个字节
  43. // 每个字节用 16 进制表示的话,使用两个字符,所以表示成 16 进制需要 32 个字符
  44. // 比如一个字节为01011011,用十六进制字符来表示就是“5b”
  45. char str[] = new char[16 * 2];
  46. int k = 0; // 表示转换结果中对应的字符位置
  47. for (int i = 0; i < 16; i++) { // 从第一个字节开始,对 MD5 的每一个字节转换成 16 进制字符的转换
  48. byte byte0 = tmp[i]; // 取第 i 个字节
  49. str[k++] = hexdigits[byte0 >>> 4 & 0xf]; // 取字节中高 4 位的数字转换, >>> 为逻辑右移,将符号位一起右移
  50. str[k++] = hexdigits[byte0 & 0xf]; // 取字节中低 4 位的数字转换
  51. }
  52. s = new String(str); // 换后的结果转换为字符串
  53. return s;
  54. }
  55. public static void main(String arg[]) {
  56. String a = getMD5(new File("e:/a.txt"));
  57. String b = getMD5(new File("e:/b.txt"));
  58. String c = getMD5(new File("e:/c.txt"));
  59. System.out.println("a.txt的摘要值为:" + a);
  60. System.out.println("b.txt的摘要值为:" + b);
  61. System.out.println("c.txt的摘要值为:" + c);
  62. if(a.equals(b)) {
  63. System.out.println("a.txt中的内容与b.txt中的内容一致");
  64. } else {
  65. System.out.println("a.txt中的内容与b.txt中的内容不一致");
  66. }
  67. if(a.equals(c)) {
  68. System.out.println("a.txt中的内容与c.txt中的内容一致");
  69. } else {
  70. System.out.println("a.txt中的内容与c.txt中的内容不一致");
  71. }
  72. }
  73. }

运行之前建立文件:

在E盘根目录下建立a.txt、b.txt和c.txt。

a.txt中的内容为“123456”。

b.txt中的内容为“123456”。

c.txt中的内容为“654321”。

运行结果:

a.txt的摘要值为:c4ca4238a0b923820dcc509a6f75849b

b.txt的摘要值为:e10adc3949ba59abbe56e057f20f883e

c.txt的摘要值为:c33367701511b4f6020ec61ded352059

a.txt中的内容与b.txt中的内容不一致

a.txt中的内容与c.txt中的内容不一致

结论:

从代码本身和运行结果都可以看出,MD5对文件的加密是加密文件中的内容,不管文件名是什么,相同的文件内容经过MD5算法处理后得到的摘要值也相同。

最新文章

  1. Java基础学习总结 -- 多线程的实现
  2. 修改RectTransform的宽度和高度
  3. 使用jquery修改css中带有!important的样式属性
  4. mac安装软件管家homebrew
  5. 一个Nodejs的简单计算測试程序
  6. MySQL的零碎知识点
  7. 深入探索C++对象模型-语义
  8. thymeleaf模板引擎shiro集成框架
  9. Time complexity of ArrayList in Java
  10. Linq 集合操作
  11. System.getProperty()参数大全
  12. elasticsearch单例模式连接
  13. spring的依赖注入是什么意思
  14. dva
  15. .net core 利用日志查看ef生成的SQL语句
  16. 【转】 Linux常用文件操作命令
  17. Mysql读写分离-Amoeba Proxy
  18. Android RxJava+Retrofit完美封装
  19. redis aof文件过大问题
  20. [4] 算法之路 - 插入排序之Shell间隔与Sedgewick间隔

热门文章

  1. AC日记——采花 洛谷 P2056
  2. CentOS 7 Docker
  3. ps aux 状态介绍
  4. python 正则表达式匹配中文(转)
  5. 【转载】SwipeRefreshLayout源码解析
  6. 洛谷P4331 [BOI2004] Sequence 数字序列 [左偏树]
  7. Flask实战第48天:首页轮播图实现
  8. poj 3464(Trie)Approximations
  9. BZOJ 4653 [Noi2016]区间(Two pointers+线段树)
  10. [Luogu1843]奶牛晒衣服