• Java实现文件MD5加密


    代码实现:

    [java] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. import java.io.File;  
    2.   
    3. import java.io.FileInputStream;  
    4.   
    5. import java.io.IOException;  
    6.   
    7. import java.security.MessageDigest;  
    8.   
    9.    
    10.   
    11. public class MD5Util {  
    12.   
    13.     static char hexdigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };  
    14.   
    15.       
    16.   
    17.     /** 
    18.  
    19.      * @funcion 对文件全文生成MD5摘要  
    20.  
    21.      * @param file:要加密的文件 
    22.  
    23.      * @return MD5摘要码 
    24.  
    25.      */  
    26.   
    27.     public static String getMD5(File file) {  
    28.   
    29.         FileInputStream fis = null;  
    30.   
    31.         try {  
    32.   
    33.             MessageDigest md = MessageDigest.getInstance("MD5");  
    34.   
    35.             fis = new FileInputStream(file);  
    36.   
    37.             byte[] buffer = new byte[2048];  
    38.   
    39.             int length = -1;  
    40.   
    41.             while ((length = fis.read(buffer)) != -1) {  
    42.   
    43.                 md.update(buffer, 0, length);  
    44.   
    45.             }  
    46.   
    47.             byte[] b = md.digest();  
    48.   
    49.             return byteToHexString(b);  
    50.   
    51.         } catch (Exception e) {  
    52.   
    53.             e.printStackTrace();  
    54.   
    55.             return null;  
    56.   
    57.         } finally {  
    58.   
    59.             try {  
    60.   
    61.                 fis.close();  
    62.   
    63.             } catch (IOException e) {  
    64.   
    65.                 e.printStackTrace();  
    66.   
    67.             }  
    68.   
    69.         }  
    70.   
    71.     }   
    72.   
    73.    
    74.   
    75.     /** 
    76.  
    77.      * @function 把byte[]数组转换成十六进制字符串表示形式 
    78.  
    79.      * @param tmp  要转换的byte[] 
    80.  
    81.      * @return 十六进制字符串表示形式 
    82.  
    83.      */  
    84.   
    85.     private static String byteToHexString(byte[] tmp) {  
    86.   
    87.         String s;  
    88.   
    89.         // 用字节表示就是 16 个字节  
    90.   
    91.         // 每个字节用 16 进制表示的话,使用两个字符,所以表示成 16 进制需要 32 个字符  
    92.   
    93.         // 比如一个字节为01011011,用十六进制字符来表示就是“5b”  
    94.   
    95.         char str[] = new char[16 * 2];  
    96.   
    97.         int k = 0; // 表示转换结果中对应的字符位置  
    98.   
    99.         for (int i = 0; i < 16; i++) { // 从第一个字节开始,对 MD5 的每一个字节转换成 16 进制字符的转换  
    100.   
    101.             byte byte0 = tmp[i]; // 取第 i 个字节  
    102.   
    103.             str[k++] = hexdigits[byte0 >>> 4 & 0xf]; // 取字节中高 4 位的数字转换, >>> 为逻辑右移,将符号位一起右移  
    104.   
    105.             str[k++] = hexdigits[byte0 & 0xf]; // 取字节中低 4 位的数字转换  
    106.   
    107.         }  
    108.   
    109.    
    110.   
    111.         s = new String(str); // 换后的结果转换为字符串  
    112.   
    113.         return s;  
    114.   
    115.     }  
    116.   
    117.    
    118.   
    119.     public static void main(String arg[]) {  
    120.   
    121.         String a = getMD5(new File("e:/a.txt"));  
    122.   
    123.         String b = getMD5(new File("e:/b.txt"));  
    124.   
    125.         String c = getMD5(new File("e:/c.txt"));  
    126.   
    127.           
    128.   
    129.         System.out.println("a.txt的摘要值为:" + a);  
    130.   
    131.         System.out.println("b.txt的摘要值为:" + b);  
    132.   
    133.         System.out.println("c.txt的摘要值为:" + c);  
    134.   
    135.           
    136.   
    137.         if(a.equals(b)) {  
    138.   
    139.             System.out.println("a.txt中的内容与b.txt中的内容一致");  
    140.   
    141.         } else {  
    142.   
    143.             System.out.println("a.txt中的内容与b.txt中的内容不一致");  
    144.   
    145.         }  
    146.   
    147.           
    148.   
    149.         if(a.equals(c)) {  
    150.   
    151.             System.out.println("a.txt中的内容与c.txt中的内容一致");  
    152.   
    153.         } else {  
    154.   
    155.             System.out.println("a.txt中的内容与c.txt中的内容不一致");  
    156.   
    157.         }  
    158.   
    159.     }  
    160.   
    161. }  


     

    运行之前建立文件:

    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算法处理后得到的摘要值也相同。

  • 相关阅读:
    POJ 2411 状态压缩递,覆盖方案数
    POJ 2774 最长公共子串
    POJ 1743 不可重叠的最长重复子串
    POJ 3294 出现在至少K个字符串中的子串
    POJ 3261 出现至少K次的可重叠最长子串
    POJ 1741/1987 树的点分治
    HDU1556 Color the ball
    解决linux系统时间不对的问题
    CentOS 6.9使用Setup配置网络(解决dhcp模式插入网线不自动获取IP的问题)
    Linux网络配置(setup)
  • 原文地址:https://www.cnblogs.com/grimm/p/6732683.html
Copyright © 2020-2023  润新知