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

java的des加密

2017年11月19日 ⁄ 综合 ⁄ 共 2977字 ⁄ 字号 评论关闭

工作中用到的,希望对大家有帮助.

 

 

  1. import java.security.Key;  
  2. import java.security.spec.AlgorithmParameterSpec;  
  3.   
  4. import javax.crypto.Cipher;  
  5. import javax.crypto.SecretKeyFactory;  
  6. import javax.crypto.spec.DESKeySpec;  
  7. import javax.crypto.spec.IvParameterSpec;  
  8.   
  9. import sun.misc.BASE64Decoder;  
  10. import sun.misc.BASE64Encoder;  
  11.   
  12.   
  13.   
  14. public class CryptoTools {  
  15.     private static final String key_ = "ising@ws";   //  
  16.     private static final byte [] DESkey = key_.getBytes();//设置密钥,略去  
  17.     private static final byte[] DESIV = { (byte)0x12,(byte0x34, (byte)0x56, (byte)0x78, (byte)0x90,  
  18.             (byte)0xAB, (byte)0xCD,(byte0xEF };//设置向量,略去  
  19.   
  20.     private static AlgorithmParameterSpec iv =null;//加密算法的参数接口,IvParameterSpec是它的一个实现  
  21.     private static Key key =null;  
  22.       
  23.     public  CryptoTools() throws  Exception {  
  24.          DESKeySpec keySpec = new DESKeySpec(DESkey);//设置密钥参数  
  25.          iv = new IvParameterSpec(DESIV);//设置向量  
  26.          SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");//获得密钥工厂  
  27.          key = keyFactory.generateSecret(keySpec);//得到密钥对象  
  28.   
  29.     }  
  30.   
  31.     public  String encode(String data) throws Exception {  
  32.         Cipher enCipher  =  Cipher.getInstance("DES/CBC/PKCS5Padding");//得到加密对象Cipher  
  33.         enCipher.init(Cipher.ENCRYPT_MODE,key,iv);//设置工作模式为加密模式,给出密钥和向量  
  34.         byte[] pasByte = enCipher.doFinal(data.getBytes("utf-8"));  
  35.         BASE64Encoder base64Encoder = new BASE64Encoder();  
  36.         return base64Encoder.encode(pasByte);  
  37.     }  
  38.   
  39.     public  String decode(String data) throws Exception{  
  40.         Cipher deCipher   =  Cipher.getInstance("DES/CBC/PKCS5Padding");  
  41.         deCipher.init(Cipher.DECRYPT_MODE,key,iv);  
  42.         BASE64Decoder base64Decoder = new BASE64Decoder();  
  43.         byte[] pasByte=deCipher.doFinal(base64Decoder.decodeBuffer(data));  
  44.         return new String(pasByte,"UTF-8");  
  45.     }  
  46.     public static void main(String[] args) throws Exception{  
  47.         String value = "hello";  
  48.         CryptoTools tools =new  CryptoTools();  
  49.         System.out.println(tools.encode(value));  
  50.         System.out.println("解密"+tools.decode(tools.encode(value)));  
  51.     }  
  52.   
  53. }   
  54. }  

 

 

修改后的:

 

  1. public String encode(String data) throws Exception   
  2.     {  
  3.         byte [] DESkey = key_.getBytes();  
  4.         byte[] DESIV = { (byte)0x12,(byte0x34, (byte)0x56, (byte)0x78, (byte)0x90,(byte)0xAB, (byte)0xCD,(byte0xEF  };  
  5.         DESKeySpec keySpec = new DESKeySpec(DESkey);  
  6.         iv = new IvParameterSpec(DESIV);  
  7.         SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");  
  8.         key = keyFactory.generateSecret(keySpec);  
  9.         SecureRandom sr = new SecureRandom();   
  10.         Cipher enCipher = Cipher.getInstance("DES");  
  11.         enCipher.init(Cipher.ENCRYPT_MODE, key,sr);  
  12.         byte[] pasByte = enCipher.doFinal(data.getBytes("utf-8"));  
  13.         BASE64Encoder base64Encoder = new BASE64Encoder();  
  14.         return base64Encoder.encode(pasByte);  
  15.     }  

2009-04-30

抱歉!评论已关闭.