java 實現 DES加密 解密算法


DES算法的入口參數有三個:Key、Data、Mode。其中Key為8個字節共64位,是DES算法的工作密鑰;Data也為8個字節64位,是要被加密或被解密的數據;Mode為DES的工作方式,有兩種:加密或解密。 
   DES算法是這樣工作的:如Mode為加密,則用Key 去把數據Data進行加密, 生成Data的密碼形式(64位)作為DES的輸出結果;如 Mode為解密,則用Key去把密碼形式的數據Data解密,還原為Data的明碼形式(64位)作為DES的輸出結果。在通信網絡的兩端,雙方約定一致 的Key,在通信的源點用Key對核心數據進行DES加密,然后以密碼形式在公共通信網(如電話網)中傳輸到通信網絡的終點,數據到達目的地后,用同樣的 Key對密碼數據進行解密,便再現了明碼形式的核心數據。這樣,便保證了核心數據(如PIN、MAC等)在公共通信網中傳輸的安全性和可靠性。 
  通過定期在通信網絡的源端和目的端同時改用新的Key,便能更進一步提高數據的保密性,這正是現在金融交易網絡的流行做法。

 

下面是具體代碼:(切記切記 字符串轉字節或字節轉字符串時 一定要加上編碼,否則可能出現亂碼)

 1 import java.io.IOException;  2 import java.security.SecureRandom;  3 
 4 import javax.crypto.Cipher;  5 import javax.crypto.SecretKey;  6 import javax.crypto.SecretKeyFactory;  7 import javax.crypto.spec.DESKeySpec;  8 
 9 import sun.misc.BASE64Decoder;  10 import sun.misc.BASE64Encoder;  11 
 12 /**
 13  * DES加密 解密算法  14  *  15  * @author lifq  16  * @date 2015-3-17 上午10:12:11  17  */
 18 public class DesUtil {  19 
 20     private final static String DES = "DES";  21     private final static String ENCODE = "GBK";  22     private final static String defaultKey = "test1234";  23 
 24     public static void main(String[] args) throws Exception {  25         String data = "測試ss";  26         // System.err.println(encrypt(data, key));  27         // System.err.println(decrypt(encrypt(data, key), key));
 28  System.out.println(encrypt(data));  29  System.out.println(decrypt(encrypt(data)));  30 
 31  }  32 
 33     /**
 34  * 使用 默認key 加密  35  *  36  * @return String  37  * @author lifq  38  * @date 2015-3-17 下午02:46:43  39      */
 40     public static String encrypt(String data) throws Exception {  41         byte[] bt = encrypt(data.getBytes(ENCODE), defaultKey.getBytes(ENCODE));  42         String strs = new BASE64Encoder().encode(bt);  43         return strs;  44  }  45 
 46     /**
 47  * 使用 默認key 解密  48  *  49  * @return String  50  * @author lifq  51  * @date 2015-3-17 下午02:49:52  52      */
 53     public static String decrypt(String data) throws IOException, Exception {  54         if (data == null)  55             return null;  56         BASE64Decoder decoder = new BASE64Decoder();  57         byte[] buf = decoder.decodeBuffer(data);  58         byte[] bt = decrypt(buf, defaultKey.getBytes(ENCODE));  59         return new String(bt, ENCODE);  60  }  61 
 62     /**
 63  * Description 根據鍵值進行加密  64  *  65  * @param data  66  * @param key  67  * 加密鍵byte數組  68  * @return
 69  * @throws Exception  70      */
 71     public static String encrypt(String data, String key) throws Exception {  72         byte[] bt = encrypt(data.getBytes(ENCODE), defaultKey.getBytes(ENCODE));  73         String strs = new BASE64Encoder().encode(bt);  74         return strs;  75  }  76 
 77     /**
 78  * Description 根據鍵值進行解密  79  *  80  * @param data  81  * @param key  82  * 加密鍵byte數組  83  * @return
 84  * @throws IOException  85  * @throws Exception  86      */
 87     public static String decrypt(String data, String key) throws IOException,  88  Exception {  89         if (data == null)  90             return null;  91         BASE64Decoder decoder = new BASE64Decoder();  92         byte[] buf = decoder.decodeBuffer(data);  93         byte[] bt = decrypt(buf, key.getBytes(ENCODE));  94         return new String(bt, ENCODE);  95  }  96 
 97     /**
 98  * Description 根據鍵值進行加密  99  * 100  * @param data 101  * @param key 102  * 加密鍵byte數組 103  * @return
104  * @throws Exception 105      */
106     private static byte[] encrypt(byte[] data, byte[] key) throws Exception { 107         // 生成一個可信任的隨機數源
108         SecureRandom sr = new SecureRandom(); 109 
110         // 從原始密鑰數據創建DESKeySpec對象
111         DESKeySpec dks = new DESKeySpec(key); 112 
113         // 創建一個密鑰工廠,然后用它把DESKeySpec轉換成SecretKey對象
114         SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES); 115         SecretKey securekey = keyFactory.generateSecret(dks); 116 
117         // Cipher對象實際完成加密操作
118         Cipher cipher = Cipher.getInstance(DES); 119 
120         // 用密鑰初始化Cipher對象
121  cipher.init(Cipher.ENCRYPT_MODE, securekey, sr); 122 
123         return cipher.doFinal(data); 124  } 125 
126     /**
127  * Description 根據鍵值進行解密 128  * 129  * @param data 130  * @param key 131  * 加密鍵byte數組 132  * @return
133  * @throws Exception 134      */
135     private static byte[] decrypt(byte[] data, byte[] key) throws Exception { 136         // 生成一個可信任的隨機數源
137         SecureRandom sr = new SecureRandom(); 138 
139         // 從原始密鑰數據創建DESKeySpec對象
140         DESKeySpec dks = new DESKeySpec(key); 141 
142         // 創建一個密鑰工廠,然后用它把DESKeySpec轉換成SecretKey對象
143         SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES); 144         SecretKey securekey = keyFactory.generateSecret(dks); 145 
146         // Cipher對象實際完成解密操作
147         Cipher cipher = Cipher.getInstance(DES); 148 
149         // 用密鑰初始化Cipher對象
150  cipher.init(Cipher.DECRYPT_MODE, securekey, sr); 151 
152         return cipher.doFinal(data); 153  } 154 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM