public class MLDUtil { public static Key DEFAULT_KEY = null; public static final String DEFAULT_SECRET_KEY1 = "?:P)(OL><KI*&UJMNHY^%TGBVFR$#EDCXSW@!QAZ"; public static final String DEFAULT_SECRET_KEY2 = "1qaz2wsx3edc4rfv5tgb6yhn7ujm8ik,9ol.0p;/"; public static final String DEFAULT_SECRET_KEY3 = "!QAZ@WSX#EDC$RFV%TGB^YHN&UJM*IK<(OL>)P:?"; public static final String DEFAULT_SECRET_KEY4 = "1qaz@WSX3edc$RFV5tgb^YHN7ujm*IK<9ol.)P:?"; public static final String DEFAULT_SECRET_KEY5 = "!QAZ2wsx#EDC4rfv%TGB6yhn&UJM8ik,(OL>0p;/"; public static final String DEFAULT_SECRET_KEY6 = "1qaz2wsx3edc4rfv5tgb^YHN&UJM*IK<(OL>)P:?"; public static final String DEFAULT_SECRET_KEY = DEFAULT_SECRET_KEY1; public static final String DES = "DES"; public static final Base32 base32 = new Base32(); static { DEFAULT_KEY = obtainKey(DEFAULT_SECRET_KEY); } /** * 获得key **/ public static Key obtainKey(String key) { if (key == null) { return DEFAULT_KEY; } KeyGenerator generator = null; try { generator = KeyGenerator.getInstance(DES); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } generator.init(new SecureRandom(key.getBytes())); Key key1 = generator.generateKey(); generator = null; return key1; } /** * 加密<br> * String明文输入,String密文输出 */ public static String encode(String str) { return encode64(null, str); } /** * 加密<br> * String明文输入,String密文输出 */ public static String encode64(String key, String str) { return Base64.encodeBase64URLSafeString(obtainEncode(key, str.getBytes())); } /** * 加密<br> * String明文输入,String密文输出 */ public static String encode32(String key, String str) { return base32.encodeAsString(obtainEncode(key, str.getBytes())).replaceAll("=", ""); } /** * 加密<br> * String明文输入,String密文输出 */ public static String encode16(String key, String str) { return Hex.encodeHexString(obtainEncode(key, str.getBytes())); } /** * 解密<br> * 以String密文输入,String明文输出 */ public static String decode(String str) { return decode64(null, str); } /** * 解密<br> * 以String密文输入,String明文输出 */ public static String decode64(String key, String str) { return new String(obtainDecode(key, Base64.decodeBase64(str))); } /** * 解密<br> * 以String密文输入,String明文输出 */ public static String decode32(String key, String str) { return new String(obtainDecode(key, base32.decode(str))); } /** * 解密<br> * 以String密文输入,String明文输出 */ public static String decode16(String key, String str) { try { return new String(obtainDecode(key, Hex.decodeHex(str.toCharArray()))); } catch (DecoderException e) { e.printStackTrace(); } return null; } /** * 加密<br> * 以byte[]明文输入,byte[]密文输出 */ private static byte[] obtainEncode(String key, byte[] str) { byte[] byteFina = null; Cipher cipher; try { Key key1 = obtainKey(key); cipher = Cipher.getInstance(DES); cipher.init(Cipher.ENCRYPT_MODE, key1); byteFina = cipher.doFinal(str); } catch (Exception e) { e.printStackTrace(); } finally { cipher = null; } return byteFina; } /** * 解密<br> * 以byte[]密文输入,以byte[]明文输出 */ private static byte[] obtainDecode(String key, byte[] str) { Cipher cipher; byte[] byteFina = null; try { Key key1 = obtainKey(key); cipher = Cipher.getInstance(DES); cipher.init(Cipher.DECRYPT_MODE, key1); byteFina = cipher.doFinal(str); } catch (Exception e) { e.printStackTrace(); } finally { cipher = null; } return byteFina; } public static byte[] decrypt(byte[] src, byte[] key) throws Exception { // DES算法要求有一个可信任的随机数源 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(src); } public final static String decrypt(String data, String key) { try { // 这里就没走 return new String(decrypt(String2byte(data.getBytes()), key.getBytes())); } catch (Exception e) { e.printStackTrace(); } return null; } public static byte[] String2byte(byte[] b) { if ((b.length % 2) != 0) throw new IllegalArgumentException("长度不是偶数"); byte[] b2 = new byte[b.length / 2]; for (int n = 0; n < b.length; n += 2) { String item = new String(b, n, 2); b2[n / 2] = (byte) Integer.parseInt(item, 16); } return b2; } public static String DataDecrypt(String str, byte[] key) { String decrypt = null; try { byte[] ret = decrypt(Base64.decodeBase64(str), key); decrypt = new String(ret, "UTF-8"); } catch (Exception e) { System.out.print(e); decrypt = str; } return decrypt; } public static byte[] encrypt(byte[] src, byte[] key) throws Exception { // DES算法要求有一个可信任的随机数源 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(src); } public final static String encrypt(String password, String key) { try { return byte2String(encrypt(password.getBytes(), key.getBytes())); } catch (Exception e) { } return null; } public static String byte2String(byte[] b) { String hs = ""; String stmp = ""; for (int n = 0; n < b.length; n++) { stmp = (java.lang.Integer.toHexString(b[n] & 0XFF)); if (stmp.length() == 1) hs = hs + "0" + stmp; else hs = hs + stmp; } return hs.toUpperCase(); } public static String DataEncrypt(String str, byte[] key) { String encrypt = null; try { byte[] ret = encrypt(str.getBytes("UTF-8"), key); encrypt = new String(Base64.decodeBase64(ret)); } catch (Exception e) { System.out.print(e); encrypt = str; } return encrypt; } public static void main(String[] args) throws InterruptedException { String encrypt = encrypt("dxjkadmin", DEFAULT_SECRET_KEY2); System.out.println(encrypt); String desencryptString = decrypt("00E64BC6DB5530391EE1B6092E213772", DEFAULT_SECRET_KEY2); System.out.println(desencryptString); } }