现在的位置: 首页 > 综合 > 正文

使用DES加密解密的工具类

2018年05月28日 ⁄ 综合 ⁄ 共 4468字 ⁄ 字号 评论关闭

一个工具类,很常用,不做深入研究了,那来可直接用

 

DesUtil.java

 

[java] view
plain
copy

  1. package lsy;  
  2.   
  3. import java.security.Key;  
  4. import java.security.SecureRandom;  
  5.   
  6. import javax.crypto.Cipher;  
  7. import javax.crypto.KeyGenerator;  
  8. import javax.crypto.SecretKey;  
  9.   
  10. import sun.misc.BASE64Decoder;  
  11. import sun.misc.BASE64Encoder;  
  12.   
  13. public class DesUtil {  
  14.   
  15.     /** 
  16.      * @param args 
  17.      */   
  18.     public static void main(String[] args) {  
  19.         //以下是加密方法algorithm="AES"的测试  
  20.           
  21.         System.out.println(DesUtil.getInstance("lushuaiyin").getEnCodeString("hello"));  
  22.         //输出  LDewGAZkmWHeYFjBz56ylw==  
  23.         //将上面的密文解密:  
  24.         System.out.println(DesUtil.getInstance("lushuaiyin").getDecodeString("LDewGAZkmWHeYFjBz56ylw=="));  
  25.         //输出   hello  
  26.           
  27.         //改变密钥测试  
  28.         System.out.println(DesUtil.getInstance("suolong").getEnCodeString("hello"));  
  29.         //输出  /RLowOJ+Fr3KdMcdJeNatg==  
  30.         System.out.println(DesUtil.getInstance("suolong").getDecodeString("/RLowOJ+Fr3KdMcdJeNatg=="));  
  31.         //输出   hello  
  32.           
  33.         //如果使用不正确的密钥解密,将会:  
  34.         System.out.println(DesUtil.getInstance("suolong").getDecodeString("LDewGAZkmWHeYFjBz56ylw=="));  
  35.           
  36.     }  
  37.       
  38.     private  SecretKey key=null;//密钥  
  39.       
  40.     //定义 加密算法,可用 DES,DESede,Blowfish,AES   
  41.     //不同的加密方式结果会不同  
  42.     private  static String algorithm="AES";  
  43.   
  44.     private  static DesUtil desUtil=null;  
  45.       
  46.       
  47.     public DesUtil(){}  
  48.       
  49.     public static DesUtil getInstance(String strKey){  
  50.         desUtil=new DesUtil();  
  51.         desUtil.createKey(strKey);  
  52.         return desUtil;  
  53.     }  
  54.     /** 
  55.      * algorithm 算法 
  56.      * @param strKey 
  57.      */  
  58.     public void createKey(String strKey){  
  59.           
  60.         try{  
  61.             KeyGenerator kg=KeyGenerator.getInstance(DesUtil.algorithm);  
  62.             byte[] bt=strKey.getBytes("UTF-8");  
  63.             SecureRandom sr=new SecureRandom(bt);  
  64.             kg.init(sr);  
  65.             this.setKey(kg.generateKey());  
  66.         }catch(Exception e){  
  67.               
  68.         }  
  69.     }  
  70.     /** 
  71.      * 加密方法,返回密文 
  72.      * cipher 密码 
  73.      * @param dataStr 
  74.      */  
  75.     public  String getEnCodeString(String dataStr){  
  76.         byte[] miwen=null;//密文  
  77.         byte[] mingwen=null;//明文  
  78.         Cipher cipher;  
  79.         String result="";//密文字符串  
  80.         try{  
  81.             mingwen=dataStr.getBytes("UTF-8");  
  82.             cipher=Cipher.getInstance(DesUtil.algorithm);  
  83.             cipher.init(Cipher.ENCRYPT_MODE, this.getKey());  
  84.             miwen=cipher.doFinal(mingwen);  
  85.               
  86.             BASE64Encoder base64en = new BASE64Encoder();   
  87.             result=base64en.encodeBuffer(miwen);//或者可以用下面的方法得到密文,结果是不一样的,都可以正常解密  
  88. //          result=byte2hex(miwen);//密文结果类似2C:37:B0:18:06:64:99:61:DE:60:58:C1:CF:9E:B2:97  
  89.         }catch(Exception e){  
  90.             e.printStackTrace();  
  91.         }  
  92.         return result;  
  93.     }  
  94.       
  95.     /** 
  96.      * 解密方法,返回明文 
  97.      * @param codeStr 
  98.      * @return 
  99.      */  
  100.     public  String getDecodeString(String codeStr){  
  101.         BASE64Decoder base64De = new BASE64Decoder();  
  102.         byte[] miwen=null;  
  103.         byte[] mingwen=null;  
  104.         String resultData="";//返回的明文  
  105.         Cipher cipher;  
  106.         try{  
  107.             miwen=base64De.decodeBuffer(codeStr);  
  108.             cipher=Cipher.getInstance(DesUtil.algorithm);  
  109.             cipher.init(Cipher.DECRYPT_MODE, this.getKey());  
  110.               
  111.             mingwen=cipher.doFinal(miwen);  
  112.             resultData = new String(mingwen,"UTF-8");    
  113.         }catch(Exception e){  
  114.             return "密钥不正确或其他原因导致异常,无法解密!";  
  115.         }  
  116.           
  117.         return resultData;  
  118.     }  
  119.       
  120.      //二行制转字符串  
  121.     public String byte2hex(byte[] b) {  
  122.         String hs = "";  
  123.         String stmp = "";  
  124.         for (int n = 0; n < b.length; n++) {  
  125.             stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));  
  126.             if (stmp.length() == 1)  
  127.                 hs = hs + "0" + stmp;  
  128.             else  
  129.                 hs = hs + stmp;  
  130.             if (n < b.length - 1)  
  131.                 hs = hs + ":";  
  132.         }  
  133.         return hs.toUpperCase();  
  134.     }   
  135.   
  136.   
  137.        
  138.     public  SecretKey getKey() {  
  139.         return key;  
  140.     }  
  141.     public  void setKey(SecretKey key) {  
  142.         this.key = key;  
  143.     }  
  144.     public static String getAlgorithm() {  
  145.         return algorithm;  
  146.     }  
  147.     public static void setAlgorithm(String algorithm) {  
  148.         algorithm = algorithm;  
  149.     }  
  150.       
  151.       
  152. }  


 

打印:

LDewGAZkmWHeYFjBz56ylw==

hello
/RLowOJ+Fr3KdMcdJeNatg==

hello
密钥不正确或其他原因导致异常,无法解密!

抱歉!评论已关闭.