1 package com.util; 2 import java.io.IOException; 3 import java.security.SecureRandom; 4 5 import javax.crypto.Cipher; 6 import javax.crypto.SecretKey; 7 import javax.crypto.SecretKeyFactory; 8 import javax.crypto.spec.DESKeySpec; 9 10 import sun.misc.BASE64Decoder; 11 import sun.misc.BASE64Encoder; 12 public class DesUtil { 13 private final static String DES = "DES"; 14 private final static String ENCODE = "UTF-8"; 15 private final static String defaultKey = "nextdactive"; 16 17 /** 18 * 使用 默认key 加密 19 * @param data 待加密数据 20 * @return 21 * @throws Exception 22 */ 23 public static String encrypt(String data) { 24 byte[] bt = null; 25 try { 26 bt = encrypt(data.getBytes(ENCODE), defaultKey.getBytes(ENCODE)); 27 } catch (Exception e) { 28 e.printStackTrace(); 29 } 30 String strs = new BASE64Encoder().encode(bt); 31 return strs; 32 } 33 34 /** 35 * 使用 默认key 解密 36 * @param data 待解密数据 37 * @return 38 * @throws IOException 39 * @throws Exception 40 */ 41 public static String decrypt(String data) { 42 if (data == null) 43 return null; 44 BASE64Decoder decoder = new BASE64Decoder(); 45 byte[] buf; 46 byte[] bt; 47 String string = null; 48 try { 49 buf = decoder.decodeBuffer(data); 50 bt = decrypt(buf, defaultKey.getBytes(ENCODE)); 51 string = new String(bt, ENCODE); 52 } catch (Exception e) { 53 e.printStackTrace(); 54 } 55 return string; 56 } 57 58 /** 59 * Description 根据键值进行加密 60 * @param data 待加密数据 61 * @param key 密钥 62 * @return 63 * @throws Exception 64 */ 65 public static String encrypt(String data, String key) throws Exception { 66 byte[] bt = encrypt(data.getBytes(ENCODE), key.getBytes(ENCODE)); 67 String strs = new BASE64Encoder().encode(bt); 68 return strs; 69 } 70 71 /** 72 * 根据键值进行解密 73 * @param data 待解密数据 74 * @param key 密钥 75 * @return 76 * @throws IOException 77 * @throws Exception 78 */ 79 public static String decrypt(String data, String key) throws IOException, 80 Exception { 81 if (data == null) 82 return null; 83 BASE64Decoder decoder = new BASE64Decoder(); 84 byte[] buf = decoder.decodeBuffer(data); 85 byte[] bt = decrypt(buf, key.getBytes(ENCODE)); 86 return new String(bt, ENCODE); 87 } 88 89 /** 90 * Description 根据键值进行加密 91 * 92 * @param data 93 * @param key 94 * 加密键byte数组 95 * @return 96 * @throws Exception 97 */ 98 private static byte[] encrypt(byte[] data, byte[] key) throws Exception { 99 // 生成一个可信任的随机数源 100 SecureRandom sr = new SecureRandom(); 101 102 // 从原始密钥数据创建DESKeySpec对象 103 DESKeySpec dks = new DESKeySpec(key); 104 105 // 创建一个密钥工厂,然后用它把DESKeySpec转换成SecretKey对象 106 SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES); 107 SecretKey securekey = keyFactory.generateSecret(dks); 108 109 // Cipher对象实际完成加密操作 110 Cipher cipher = Cipher.getInstance(DES); 111 112 // 用密钥初始化Cipher对象 113 cipher.init(Cipher.ENCRYPT_MODE, securekey, sr); 114 115 return cipher.doFinal(data); 116 } 117 118 /** 119 * Description 根据键值进行解密 120 * 121 * @param data 122 * @param key 加密键byte数组 123 * @return 124 * @throws Exception 125 */ 126 private static byte[] decrypt(byte[] data, byte[] key) throws Exception { 127 // 生成一个可信任的随机数源 128 SecureRandom sr = new SecureRandom(); 129 130 // 从原始密钥数据创建DESKeySpec对象 131 DESKeySpec dks = new DESKeySpec(key); 132 133 // 创建一个密钥工厂,然后用它把DESKeySpec转换成SecretKey对象 134 SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES); 135 SecretKey securekey = keyFactory.generateSecret(dks); 136 137 // Cipher对象实际完成解密操作 138 Cipher cipher = Cipher.getInstance(DES); 139 140 // 用密钥初始化Cipher对象 141 cipher.init(Cipher.DECRYPT_MODE, securekey, sr); 142 143 return cipher.doFinal(data); 144 } 145 146 public static void main(String[] args){ 147 String data = "hello world"; 148 String key ="qfdrrdwf"; 149 System.out.println("加密前:"+data); 150 try { 151 System.out.println("加密后:"+encrypt(data, key)); 152 System.out.println("解密后:"+decrypt(encrypt(data, key), key)); 153 } catch (Exception e) { 154 e.printStackTrace(); 155 } 156 } 157 }