DES算法全称为Data Encryption Standard,即数据加密算法,它是IBM公司于1975年研究成功并公开发表的。
DES算法入口参数有三个:
key:8个字节共64位的工作密钥 data:8个字节共64位的需要被加密或被解密的数据 mode:DES工作方式,加密或者解密 DES算法原理: 如Mode为加密,则用Key 去把数据Data进行加密, 生成Data的密码形式(64位)作为DES的输出结果; 如 Mode为解密,则用Key去把密码形式的数据Data解密,还原为Data的明码形式(64位)作为DES的输出结果。 在通信网络的两端,双方约定一致 的Key,在通信的源点用Key对核心数据进行DES加密,然后以密码形式在公共通信网 (如电话网)中传输到通信网络的终点,数据到达目的地后,用同样的 Key对密码数据进行解密,便再现了明码形式的核心数据。 这样,便保证了核心数据(如PIN、MAC等)在公共通信网中传输的安全性和可靠性。 DES算法可靠性: 通过定期在通信网络的源端和目的端同时改用新的Key,便能更进一步提高数据的保密性, 这正是现在金融交易网络的流行做法。 代码: import sun.misc.BASE64Decoder; import sun.misc.BASE64Encoder; import javax.crypto.Cipher; import javax.crypto.SecretKey; import javax.crypto.SecretKeyFactory; import javax.crypto.spec.DESKeySpec; import java.io.IOException; import java.security.SecureRandom; /** * DES加密算法 * Created by lipeng on 2017/6/6. */ public class DesUtil { private final static String DES = "DES"; private final static String ENCODE = "GBK"; private final static String defaultKey = "qkxTskldu"; public static void main(String[] args) throws Exception { String data = "测试ss"; System.out.println(encrypt(data)); System.out.println(decrypt(encrypt(data))); } /** * 使用 默认key 加密 * * @return String * @author lifq * @date 2015-3-17 下午02:46:43 */ public static String encrypt(String data) throws Exception { byte[] bt = encrypt(data.getBytes(ENCODE), defaultKey.getBytes(ENCODE)); String strs = new BASE64Encoder().encode(bt); return strs; } /** * 使用 默认key 解密 * * @return String * @author lifq * @date 2015-3-17 下午02:49:52 */ public static String decrypt(String data) throws IOException, Exception { if (data == null) return null; BASE64Decoder decoder = new BASE64Decoder(); byte[] buf = decoder.decodeBuffer(data); byte[] bt = decrypt(buf, defaultKey.getBytes(ENCODE)); return new String(bt, ENCODE); } /** * Description 根据键值进行加密 * * @param data * @param key * 加密键byte数组 * @return * @throws Exception */ public static String encrypt(String data, String key) throws Exception { byte[] bt = encrypt(data.getBytes(ENCODE), defaultKey.getBytes(ENCODE)); String strs = new BASE64Encoder().encode(bt); return strs; } /** * Description 根据键值进行解密 * * @param data * @param key * 加密键byte数组 * @return * @throws IOException * @throws Exception */ public static String decrypt(String data, String key) throws IOException, Exception { if (data == null) return null; BASE64Decoder decoder = new BASE64Decoder(); byte[] buf = decoder.decodeBuffer(data); byte[] bt = decrypt(buf, key.getBytes(ENCODE)); return new String(bt, ENCODE); } /** * Description 根据键值进行加密 * * @param data * @param key * 加密键byte数组 * @return * @throws Exception */ private static byte[] encrypt(byte[] data, byte[] key) throws Exception { // 生成一个可信任的随机数源 SecureRandom sr = new SecureRandom(); // 从原始密钥数据创建DESKeySpec对象 DESKeySpec dks = new DESKeySpec(key); // 创建一个密钥工厂,然后用它把DESKeySpec转换成SecretKey对象 SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES); SecretKey securekey = keyFactory.generateSecret(dks); // Cipher对象实际完成加密操作 Cipher cipher = Cipher.getInstance(DES); // 用密钥初始化Cipher对象 cipher.init(Cipher.ENCRYPT_MODE, securekey, sr); return cipher.doFinal(data); } /** * Description 根据键值进行解密 * * @param data * @param key * 加密键byte数组 * @return * @throws Exception */ private static byte[] decrypt(byte[] data, byte[] key) throws Exception { // 生成一个可信任的随机数源 SecureRandom sr = new SecureRandom(); // 从原始密钥数据创建DESKeySpec对象 DESKeySpec dks = new DESKeySpec(key); // 创建一个密钥工厂,然后用它把DESKeySpec转换成SecretKey对象 SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES); SecretKey securekey = keyFactory.generateSecret(dks); // Cipher对象实际完成解密操作 Cipher cipher = Cipher.getInstance(DES); // 用密钥初始化Cipher对象 cipher.init(Cipher.DECRYPT_MODE, securekey, sr); return cipher.doFinal(data); } }