JAVA中3des加密算法

 
  1. package test;   
  2.   
  3. /*  
  4.  * 当前文件:ThreeDES.java  
  5.  * 作者:fenglingcompany  
  6.  */  
  7.   
  8. import java.security.Key;   
  9. import java.security.SecureRandom;   
  10. import javax.crypto.Cipher;   
  11. import javax.crypto.KeyGenerator;   
  12.   
  13. import sun.misc.BASE64Decoder;   
  14. import sun.misc.BASE64Encoder;   
  15.   
  16. /**  
  17.  *   
  18.  * 使用DES加密与解密,可对byte[],String类型进行加密与解密 密文可使用String,byte[]存储.  
  19.  *   
  20.  * 方法: void getKey(String strKey)从strKey的字条生成一个Key  
  21.  *   
  22.  * String getEncString(String strMing)对strMing进行加密,返回String密文 String  
  23.  * getDesString(String strMi)对strMin进行解密,返回String明文  
  24.  *   
  25.  * byte[] getEncCode(byte[] byteS)byte[]型的加密 byte[] getDesCode(byte[]  
  26.  * byteD)byte[]型的解密  
  27.  */  
  28.   
  29. public class ThreeDES{   
  30.     Key key;   
  31.   
  32.     /**  
  33.      * 根据参数生成KEY  
  34.      *   
  35.      * @param strKey  
  36.      */  
  37.     public void getKey(String strKey) {   
  38.         try {   
  39.             KeyGenerator _generator = KeyGenerator.getInstance("DES");   
  40.             _generator.init(new SecureRandom(strKey.getBytes()));   
  41.             this.key = _generator.generateKey();   
  42.             _generator = null;   
  43.         } catch (Exception e) {   
  44.             e.printStackTrace();   
  45.         }   
  46.     }   
  47.   
  48.     /**  
  49.      * 加密String明文输入,String密文输出  
  50.      *   
  51.      * @param strMing  
  52.      * @return  
  53.      */  
  54.     public String getEncString(String strMing) {   
  55.         byte[] byteMi = null;   
  56.         byte[] byteMing = null;   
  57.         String strMi = "";   
  58.         BASE64Encoder base64en = new BASE64Encoder();   
  59.         try {   
  60.             byteMing = strMing.getBytes("UTF8");   
  61.             byteMi = this.getEncCode(byteMing);   
  62.             strMi = base64en.encode(byteMi);   
  63.         } catch (Exception e) {   
  64.             e.printStackTrace();   
  65.         } finally {   
  66.             base64en = null;   
  67.             byteMing = null;   
  68.             byteMi = null;   
  69.         }   
  70.         return strMi;   
  71.     }   
  72.   
  73.     /**  
  74.      * 解密 以String密文输入,String明文输出  
  75.      *   
  76.      * @param strMi  
  77.      * @return  
  78.      */  
  79.     public String getDesString(String strMi) {   
  80.         BASE64Decoder base64De = new BASE64Decoder();   
  81.         byte[] byteMing = null;   
  82.         byte[] byteMi = null;   
  83.         String strMing = "";   
  84.         try {   
  85.             byteMi = base64De.decodeBuffer(strMi);   
  86.             byteMing = this.getDesCode(byteMi);   
  87.             strMing = new String(byteMing, "UTF8");   
  88.         } catch (Exception e) {   
  89.             e.printStackTrace();   
  90.         } finally {   
  91.             base64De = null;   
  92.             byteMing = null;   
  93.             byteMi = null;   
  94.         }   
  95.         return strMing;   
  96.     }   
  97.   
  98.     /**  
  99.      * 加密以byte[]明文输入,byte[]密文输出  
  100.      *   
  101.      * @param byteS  
  102.      * @return  
  103.      */  
  104.     private byte[] getEncCode(byte[] byteS) {   
  105.         byte[] byteFina = null;   
  106.         Cipher cipher;   
  107.         try {   
  108.             cipher = Cipher.getInstance("DES");   
  109.             cipher.init(Cipher.ENCRYPT_MODE, key);   
  110.             byteFina = cipher.doFinal(byteS);   
  111.         } catch (Exception e) {   
  112.             e.printStackTrace();   
  113.         } finally {   
  114.             cipher = null;   
  115.         }   
  116.         return byteFina;   
  117.     }   
  118.   
  119.     /**  
  120.      * 解密以byte[]密文输入,以byte[]明文输出  
  121.      *   
  122.      * @param byteD  
  123.      * @return  
  124.      */  
  125.     private byte[] getDesCode(byte[] byteD) {   
  126.         Cipher cipher;   
  127.         byte[] byteFina = null;   
  128.         try {   
  129.             cipher = Cipher.getInstance("DES");   
  130.             cipher.init(Cipher.DECRYPT_MODE, key);   
  131.             byteFina = cipher.doFinal(byteD);   
  132.         } catch (Exception e) {   
  133.             e.printStackTrace();   
  134.         } finally {   
  135.             cipher = null;   
  136.         }   
  137.         return byteFina;   
  138.   
  139.     }   
  140.   
  141.     public static void main(String[] args) {   
  142.         ThreeDES des = new ThreeDES();// 实例化一个对像   
  143.         des.getKey("fenglingcompany");// 生成密匙   
  144.   
  145.         String strEnc = des.getEncString("fengye666666666");// 加密字符串,返回String的密文   
  146.         System.out.println(strEnc);   
  147.   
  148.         String strDes = des.getDesString(strEnc);// 把String 类型的密文解密   
  149.         System.out.println(strDes);   
  150.     }   
  151.   
  152. }  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值