java 实现文件内容的加密和解密

转载:http://xiaoxiaokuang.iteye.com/blog/1440031


  1. package com.umapp.test;  
  2.   
  3. import java.io.FileInputStream;  
  4. import java.io.FileOutputStream;  
  5. import java.io.InputStream;  
  6. import java.io.OutputStream;  
  7. import java.security.Key;  
  8. import java.security.SecureRandom;  
  9.   
  10. import javax.crypto.Cipher;  
  11. import javax.crypto.CipherInputStream;  
  12. import javax.crypto.CipherOutputStream;  
  13. import javax.crypto.KeyGenerator;  
  14.   
  15. public class TestDES {   
  16.   Key key;   
  17.   public TestDES(String str) {   
  18.     getKey(str);//生成密匙   
  19.   }   
  20.   /**  
  21.   * 根据参数生成KEY  
  22.   */   
  23.   public void getKey(String strKey) {   
  24.     try {   
  25.         KeyGenerator _generator = KeyGenerator.getInstance("DES");   
  26.         _generator.init(new SecureRandom(strKey.getBytes()));   
  27.         this.key = _generator.generateKey();   
  28.         _generator = null;   
  29.     } catch (Exception e) {   
  30.         throw new RuntimeException("Error initializing SqlMap class. Cause: " + e);   
  31.     }   
  32.   }   
  33.   
  34.   /**  
  35.   * 文件file进行加密并保存目标文件destFile中  
  36.   *  
  37.   * @param file   要加密的文件 如c:/test/srcFile.txt  
  38.   * @param destFile 加密后存放的文件名 如c:/加密后文件.txt  
  39.   */   
  40.   public void encrypt(String file, String destFile) throws Exception {   
  41.     Cipher cipher = Cipher.getInstance("DES");   
  42.     // cipher.init(Cipher.ENCRYPT_MODE, getKey());   
  43.     cipher.init(Cipher.ENCRYPT_MODE, this.key);   
  44.     InputStream is = new FileInputStream(file);   
  45.     OutputStream out = new FileOutputStream(destFile);   
  46.     CipherInputStream cis = new CipherInputStream(is, cipher);   
  47.     byte[] buffer = new byte[1024];   
  48.     int r;   
  49.     while ((r = cis.read(buffer)) > 0) {   
  50.         out.write(buffer, 0, r);   
  51.     }   
  52.     cis.close();   
  53.     is.close();   
  54.     out.close();   
  55.   }   
  56.   /**  
  57.   * 文件采用DES算法解密文件  
  58.   *  
  59.   * @param file 已加密的文件 如c:/加密后文件.txt  
  60.   *         * @param destFile  
  61.   *         解密后存放的文件名 如c:/ test/解密后文件.txt  
  62.   */   
  63.   public void decrypt(String file, String dest) throws Exception {   
  64.     Cipher cipher = Cipher.getInstance("DES");   
  65.     cipher.init(Cipher.DECRYPT_MODE, this.key);   
  66.     InputStream is = new FileInputStream(file);   
  67.     OutputStream out = new FileOutputStream(dest);   
  68.     CipherOutputStream cos = new CipherOutputStream(out, cipher);   
  69.     byte[] buffer = new byte[1024];   
  70.     int r;   
  71.     while ((r = is.read(buffer)) >= 0) {   
  72.         System.out.println();  
  73.         cos.write(buffer, 0, r);   
  74.     }   
  75.     cos.close();   
  76.     out.close();   
  77.     is.close();   
  78.   }   
  79.   public static void main(String[] args) throws Exception {   
  80.     TestDES td = new TestDES("aaa");   
  81.     td.encrypt("e:/r.txt""e:/r解密.txt"); //加密   
  82.     td.decrypt("e:/r解密.txt""e:/r1.txt"); //解密   
  83.       
  84.   }   
  85. }  

  • 9
    点赞
  • 45
    收藏
    觉得还不错? 一键收藏
  • 8
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值