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 }