`
absolute
  • 浏览: 188250 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

DES 加密

阅读更多
java 代码
  1. public class CryptoUtils {   
  2.   
  3.     private static final String KEY = "12345678123456781234567811111111";   
  4.        
  5.     public static String encrypt(String strDataToEncrypt) {   
  6.         byte[] key = KEY.getBytes();   
  7.            
  8.         Provider sunJCE = new com.sun.crypto.provider.SunJCE();   
  9.         Security.addProvider(sunJCE);   
  10.            
  11.         String strAlgorithm = "DES";   
  12.         SecretKeySpec keySpec = null;   
  13.         DESKeySpec deskey = null;   
  14.         String strResult = "";   
  15.            
  16.         try {   
  17.             deskey = new DESKeySpec(key);   
  18.             keySpec = new SecretKeySpec(deskey.getKey(), "DES");   
  19.                
  20.             Cipher cipher = Cipher.getInstance(strAlgorithm);   
  21.                
  22.             cipher.init(Cipher.ENCRYPT_MODE, keySpec);   
  23.                
  24.             byte[] utf8 = strDataToEncrypt.getBytes("UTF8");   
  25.                
  26.             byte[] enc = cipher.doFinal(utf8);   
  27.                
  28.             strResult = new sun.misc.BASE64Encoder().encode(enc);   
  29.         } catch (Exception e) {   
  30.             e.printStackTrace();   
  31.         }   
  32.            
  33.         return strResult;   
  34.     }   
  35.        
  36.     /**  
  37.      * This function decrypt a given string using the DES algorithm.  
  38.      * @param strDataToDecrypt The String to decrypt  
  39.      * @param strKey The generated key used to decrypt  
  40.      * @return The encrypted string  
  41.      */  
  42.     public static String decrypt(String strDataToDecrypt) {   
  43.         byte[] key = KEY.getBytes();   
  44.         Provider sunJCE = new com.sun.crypto.provider.SunJCE();   
  45.         Security.addProvider(sunJCE);   
  46.            
  47.         String strAlgorithm = "DES";   
  48.         SecretKeySpec keySpec = null;   
  49.         DESKeySpec deskey = null;   
  50.         String strResult = "";   
  51.            
  52.         try {   
  53.             deskey = new DESKeySpec(key);   
  54.             keySpec = new SecretKeySpec(deskey.getKey(), "DES");   
  55.                
  56.             Cipher cipher = Cipher.getInstance(strAlgorithm);   
  57.             cipher.init(Cipher.DECRYPT_MODE, keySpec);   
  58.                
  59.             byte[] dec = new sun.misc.BASE64Decoder().decodeBuffer(strDataToDecrypt);   
  60.                
  61.             byte[] utf8 = cipher.doFinal(dec);   
  62.                
  63.             return new String(utf8, "UTF8");   
  64.         }   
  65.            
  66.         catch (Exception e) {   
  67.            e.printStackTrace();   
  68.         }   
  69.            
  70.         return strResult;   
  71.     }   
  72. }  
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics