DES加解密(Java)

  1. /** 
  2.  * DES加密算法的演示验证 
  3.  * DES为对称加密算法,加密、解密双方使用同一个密钥 
  4.  * 密钥的长度固定为8byte,其中字节的最后一位为校验,因此实际长度为7byte(56bit) 
  5.  * 加密也是采用分组加密,每8byte明文加密成密文 
  6.  */  
  7. package encrypto;  
  8.   
  9. import java.io.BufferedInputStream;  
  10. import java.io.BufferedOutputStream;  
  11. import java.io.File;  
  12. import java.io.FileInputStream;  
  13. import java.io.FileOutputStream;  
  14. import java.io.IOException;  
  15. import java.io.ObjectInputStream;  
  16. import java.io.ObjectOutputStream;  
  17. import java.security.InvalidKeyException;  
  18. import java.security.NoSuchAlgorithmException;  
  19.   
  20. import javax.crypto.BadPaddingException;  
  21. import javax.crypto.Cipher;  
  22. import javax.crypto.IllegalBlockSizeException;  
  23. import javax.crypto.KeyGenerator;  
  24. import javax.crypto.NoSuchPaddingException;  
  25. import javax.crypto.SecretKey;  
  26.   
  27. /** 
  28.  * @author liuhuabai,liuhuabai@163.com 
  29.  * 
  30.  */  
  31. public class DESDemo {  
  32.     /** 
  33.      * 密钥 
  34.      */  
  35.     private SecretKey secretKey;  
  36.     /** 
  37.      * 加解密 
  38.      */  
  39.     private Cipher cipher;  
  40.       
  41.     /** 
  42.      * 明文块的长度 
  43.      */  
  44.     private int originLength = 8;  
  45.     /** 
  46.      * 密文块的长度 
  47.      */  
  48.     private int encrytLength = 16;  
  49.     /** 
  50.      * 得到密钥 
  51.      * @throws NoSuchAlgorithmException 
  52.      */  
  53.     public void initKey() throws NoSuchAlgorithmException {  
  54.         KeyGenerator kengen = KeyGenerator.getInstance("DES");  
  55.         secretKey = kengen.generateKey();  
  56.     }  
  57.     /** 
  58.      * 将密钥保存至文件 
  59.      * @param file 待写入的文件 
  60.      * @return true 写入成功;false 写入失败 
  61.      */  
  62.     public boolean saveKey(File file) {  
  63.         boolean write;  
  64.         FileOutputStream fos = null;  
  65.         try {  
  66.             fos = new FileOutputStream(file);  
  67.             ObjectOutputStream oos = new ObjectOutputStream(fos);  
  68.             System.out.println(secretKey.getFormat());  
  69.             //注意,此处采用writeObject方法,读取时也要采用readObject方法  
  70.             oos.writeObject(secretKey);  
  71.             write = true;  
  72.         } catch (IOException e) {  
  73.             write = false;  
  74.         } finally {  
  75.                 try {  
  76.                     if(fos != null) fos.close();  
  77.                 } catch (IOException e) {  
  78.                     e.printStackTrace();  
  79.                 }  
  80.         }  
  81.         return write;  
  82.     }  
  83.     /** 
  84.      * 从文件中得到密钥 
  85.      * @param file 保存密钥的文件 
  86.      */  
  87.     public void getKey(File file) {  
  88.         FileInputStream fis;  
  89.         try {  
  90.             //读取数据  
  91.             fis = new FileInputStream(file);  
  92.             ObjectInputStream ois = new ObjectInputStream(fis);  
  93.             secretKey = (SecretKey) ois.readObject();  
  94.             //关闭资源  
  95.             ois.close();  
  96.         } catch (IOException e) {  
  97.             e.printStackTrace();  
  98.         } catch (ClassNotFoundException e) {  
  99.             e.printStackTrace();  
  100.         }  
  101.     }  
  102.     /** 
  103.      * DES算法加密 
  104.      * @param origin 明文 
  105.      * @return 密文 
  106.      */  
  107.     protected byte [] encrypt(byte [] origin) {  
  108.         byte [] enc = null;  
  109.         try {  
  110.             cipher = Cipher.getInstance("DES");  
  111.             cipher.init(Cipher.ENCRYPT_MODE, secretKey);  
  112.             enc = cipher.doFinal(origin);  
  113.         } catch (NoSuchAlgorithmException e) {  
  114.             e.printStackTrace();  
  115.         } catch (NoSuchPaddingException e) {  
  116.             e.printStackTrace();  
  117.         } catch (InvalidKeyException e) {  
  118.             e.printStackTrace();  
  119.         } catch (IllegalBlockSizeException e) {  
  120.             e.printStackTrace();  
  121.         } catch (BadPaddingException e) {  
  122.             e.printStackTrace();  
  123.         }  
  124.         return enc;  
  125.     }  
  126.     /** 
  127.      * DES算法解密 
  128.      * @param enc 密文 
  129.      * @return 明文 
  130.      */  
  131.     protected byte [] decrypt(byte [] enc) {  
  132.         byte [] origin = null;  
  133.         try {  
  134.             cipher = Cipher.getInstance("DES");  
  135.             cipher.init(Cipher.DECRYPT_MODE, secretKey);  
  136.             origin = cipher.doFinal(enc);  
  137.         } catch (NoSuchAlgorithmException e) {  
  138.             e.printStackTrace();  
  139.         } catch (NoSuchPaddingException e) {  
  140.             e.printStackTrace();  
  141.         } catch (InvalidKeyException e) {  
  142.             e.printStackTrace();  
  143.         } catch (IllegalBlockSizeException e) {  
  144.             e.printStackTrace();  
  145.         } catch (BadPaddingException e) {  
  146.             e.printStackTrace();  
  147.         }  
  148.         return origin;  
  149.     }  
  150.     /** 
  151.      * 加密文件 
  152.      * @param origin 明文件 
  153.      * @throws IOException 
  154.      */  
  155.     public void encryptFile(File origin) throws IOException {  
  156.         FileInputStream fis = null;  
  157.         FileOutputStream fos = null;  
  158.           
  159.         //读入  
  160.         fis = new FileInputStream(origin);  
  161.         BufferedInputStream bis = new BufferedInputStream(fis);  
  162.         byte [] originbyte = new byte [originLength];  
  163.         //写出  
  164.         fos = new FileOutputStream(new File(origin+".encrypt"));  
  165.         BufferedOutputStream bos = new BufferedOutputStream(fos);  
  166.         byte [] encryptbyte;  
  167.         //int k;  
  168.         while(bis.read(originbyte) > 0) {  
  169.             encryptbyte = this.encrypt(originbyte);  
  170.             bos.write(encryptbyte);  
  171.             originbyte = new byte[originLength];  
  172.         }  
  173.         //压入  
  174.         bos.flush();  
  175.         //关闭资源  
  176.         if(fis != null) fis.close();  
  177.         if(fos != null) fos.close();  
  178.     }  
  179.     /** 
  180.      * 解密文件 
  181.      * @param encrypt 密文 
  182.      * @throws IOException 
  183.      */  
  184.     public void decryptFile(File encrypt) throws IOException {  
  185.         FileInputStream fis = null;  
  186.         FileOutputStream fos = null;  
  187.         //读入  
  188.         fis = new FileInputStream(encrypt);  
  189.         BufferedInputStream bis = new BufferedInputStream(fis);  
  190.         byte [] encryptbyte = new byte [encrytLength];  
  191.         //写出  
  192.         fos = new FileOutputStream(new File(encrypt+".decrypt"));  
  193.         BufferedOutputStream bos = new BufferedOutputStream(fos);  
  194.         byte [] originbyte;  
  195.           
  196.         //int k;  
  197.         while(bis.read(encryptbyte) > 0) {  
  198.             originbyte = this.decrypt(encryptbyte);  
  199.             bos.write(originbyte);  
  200.             encryptbyte = new byte [encrytLength];  
  201.         }  
  202.         //压入  
  203.         bos.flush();  
  204.         //关闭资源  
  205.         if(fis != null) fis.close();  
  206.         if(fos != null) fos.close();  
  207.     }  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值