MD5 方式加密文件,判断文件是否被修改的依据:MD5值是否改变

使用MD5的方式对文件进行加密,判断获取文件是否被修改的唯一凭证:MD5值
  1. public class TestFileMD5 {  
  2.       
  3.     public final static String[] hexDigits = { "0", "1", "2", "3", "4", "5",  
  4.         "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };  
  5.       
  6.     /** 
  7.      * 获取文件的MD5值 
  8.      * @param file 
  9.      * @return 
  10.      */  
  11.     public static String getFileMD5(File file){  
  12.         String md5 = null;  
  13.         FileInputStream fis = null;  
  14.         FileChannel fileChannel = null;  
  15.         try {  
  16.             fis = new FileInputStream(file);  
  17.             fileChannel = fis.getChannel();  
  18.             MappedByteBuffer byteBuffer = fileChannel.map(FileChannel.MapMode.READ_ONLY, 0, file.length());  
  19.               
  20.             try {  
  21.                 MessageDigest md = MessageDigest.getInstance("MD5");  
  22.                 md.update(byteBuffer);  
  23.                 md5 = byteArrayToHexString(md.digest());  
  24.             } catch (NoSuchAlgorithmException e) {  
  25.                   
  26.                 e.printStackTrace();  
  27.             }  
  28.         } catch (FileNotFoundException e) {  
  29.               
  30.             e.printStackTrace();  
  31.         } catch (IOException e) {  
  32.               
  33.             e.printStackTrace();  
  34.         }finally{  
  35.             try {  
  36.                 fileChannel.close();  
  37.                 fis.close();  
  38.             } catch (IOException e) {  
  39.                   
  40.                 e.printStackTrace();  
  41.             }  
  42.         }  
  43.           
  44.         return md5;  
  45.     }  
  46.       
  47.     /** 
  48.      * 字节数组转十六进制字符串 
  49.      * @param digest 
  50.      * @return 
  51.      */  
  52.     private static String byteArrayToHexString(byte[] digest) {  
  53.           
  54.         StringBuffer buffer = new StringBuffer();  
  55.         for(int i=0; i<digest.length; i++){  
  56.             buffer.append(byteToHexString(digest[i]));  
  57.         }  
  58.         return buffer.toString();  
  59.     }  
  60.       
  61.     /** 
  62.      * 字节转十六进制字符串 
  63.      * @param b 
  64.      * @return 
  65.      */  
  66.     private static String byteToHexString(byte b) {  
  67.         //  int d1 = n/16;   
  68.              int d1 = (b&0xf0)>>4;  
  69.                
  70.         //   int d2 = n%16;   
  71.              int d2 = b&0xf;  
  72.              return hexDigits[d1] + hexDigits[d2];  
  73.     }  
  74.       
  75.     //入口   
  76.     public static void main(String [] args) throws Exception{  
  77.         System.out.println("-----测试创建文件的md5后缀----------");  
  78.           
  79.         File file = new File("E:/jquery-1.9.1.min.js");  
  80.           
  81.         if(!file.exists()){  
  82.             file.createNewFile();  
  83.         }  
  84.         //获取参数   
  85.         String parent = file.getParent();  
  86.           
  87.         System.out.println(parent);  
  88.         String fileName = file.getName();  
  89.         System.out.println(fileName);  
  90.         //首先获取文件的MD5   
  91.         String md5 = getFileMD5(file);  
  92.           
  93.         //93d97357be249c61407fa21aa434e72f   
  94.         System.out.println("-----获取的md5:" + md5);  
  95.           
  96.         //组装   
  97.         File md5File = new File(parent + fileName +".md5");  
  98.         if(md5File.exists()){  
  99.             md5File.delete();  
  100.             md5File.createNewFile();  
  101.         }  
  102.           
  103.         FileOutputStream fos = new FileOutputStream(md5File);  
  104.         fos.write(md5.getBytes());  
  105.           
  106.         fos.flush();  
  107.         fos.close();  
  108.           
  109.         System.out.println("--------完成---------");  
  110.     }  
  111. }  

注意:
使用md5的方式对文件进行加密,以获取md5值,可以知道该文件的内容是否被修改过,
因为修改过文件内容,再次对该文件进行加密的话,获取的md5值将发生变化。





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值