package com.zhx.base.util; import org.apache.commons.codec.binary.Base64; import javax.crypto.Cipher; import javax.crypto.KeyGenerator; import javax.crypto.SecretKey; import javax.crypto.SecretKeyFactory; import javax.crypto.spec.DESedeKeySpec; import java.security.Key; public class TripleDES { public static final String CIPHERMODE = "DESede/ECB/PKCS5Padding"; public static final String ENCODE = "utf-8"; /** * 初始化密钥 * @return * @throws Exception */ public static String getKeyStr() throws Exception{ return Base64.encodeBase64String(getKey().getEncoded()); } /** * 获取密钥 * @return * @throws Exception */ public static Key getKey() throws Exception{ //实例化 KeyGenerator kg = KeyGenerator.getInstance("DESede"); kg.init(168); //生成密钥 SecretKey secretKey = kg.generateKey(); return secretKey; } public static String encrypt(String key, String src) { try { DESedeKeySpec dks = new DESedeKeySpec(key.getBytes(ENCODE)); SecretKeyFactory keyFactory = SecretKeyFactory .getInstance("DESede"); SecretKey securekey = keyFactory.generateSecret(dks); Cipher cipher = Cipher.getInstance(CIPHERMODE); cipher.init(Cipher.ENCRYPT_MODE, securekey); byte[] b = cipher.doFinal(src.getBytes()); return Base64.encodeBase64String(b).replaceAll(" ", "").replaceAll(" ", ""); } catch (Exception e) { e.printStackTrace(); } return null; } public static String decrypt(String key, String src) { try { byte[] bytesrc = Base64.decodeBase64(src); DESedeKeySpec dks = new DESedeKeySpec(key.getBytes(ENCODE)); SecretKeyFactory keyFactory = SecretKeyFactory .getInstance("DESede"); SecretKey securekey = keyFactory.generateSecret(dks); Cipher cipher = Cipher.getInstance(CIPHERMODE); cipher.init(Cipher.DECRYPT_MODE, securekey); byte[] retByte = cipher.doFinal(bytesrc); return new String(retByte, "utf-8"); } catch (Exception e) { e.printStackTrace(); } return null; } public static void main(String[] args) throws Exception { String key = getKeyStr(); System.out.println(key); System.out.println(key.getBytes().length); String test = TripleDES.encrypt(key, "1232135sdsadsasdshgjhkjhkadsadsdgdfhgdjhkbvnbcnbhgfjhgjhgkhghfdsgfdsgadsadadsad"); System.out.println(test); test = test.replace("=",""); System.out.println(test); // System.out.println(Base64Utils.getBase64(test)); System.out.println(TripleDES.decrypt(key,test)); } }
package com.zhx.base.util; public class TripleDESUtil { // public static String key = PropertiesUtil.getValue("THREEDESKEY"); public static String key = "asdsaffjhdfgdbv^&%^**hhjad"; /** * 3DES加密 * * @param value * @return */ public static String getEncryptString(String value,String key) { return TripleDES.encrypt(key, value).replace("=",""); } /** * 3DES解密 * * @param value * @return */ public static String decryptString(String value,String key) { return TripleDES.decrypt(key, value); } /** * 3DES加密 带Base64 * * @param value * @return */ public static String getEncryptStringIncludeBase64(String value,String key) { value = Base64Utils.getBase64(value); value = TripleDES.encrypt(key, value); // System.out.println("生成密文:" + value); return value.replace("=",""); } /** * 3DES解密 带Base64 * * @param str * @return */ public static String decryptDataIncludeBase64(String str,String key) { if (str == null) { str = ""; } str = str.replaceAll(" ", "+"); return Base64Utils.getFromBase64(TripleDES.decrypt(key, str)); } public static void main(String[] args) { String key = "5j3EAa7qp/R8xLquBKQskSaAtp224McC"; System.out.println("*******************BASE64****************************"); System.out.println(Base64Utils.getBase64("我的12312314")); System.out.println(Base64Utils.getFromBase64(Base64Utils.getBase64("我的12312314"))); System.out.println("*******************3des****************************"); System.out.println(TripleDESUtil.getEncryptString("我的12312314",key)); System.out.println(TripleDESUtil.decryptString("+rEVEmiHZKcgIKVV2NcYNw",key)); System.out.println(TripleDESUtil.decryptString(TripleDESUtil.getEncryptString("我的12312314",key), key)); System.out.println("*******************3des+Base64****************************"); System.out.println(TripleDESUtil.getEncryptStringIncludeBase64("我的12312314",key)); System.out.println(TripleDESUtil.decryptDataIncludeBase64(TripleDESUtil.getEncryptStringIncludeBase64("我的12312314",key),key)); } }
package com.zhx.base.util; import org.apache.commons.codec.binary.Base64; import java.io.UnsupportedEncodingException; public class Base64Utils { /** * 加密 * * @param str * @return */ public static String getBase64(String str) { byte[] b = null; String s = null; try { b = str.getBytes("utf-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } if (b != null) { s = Base64.encodeBase64String(b); } return s; } /** * 解密 * * @param s * @return */ public static String getFromBase64(String s) { byte[] b = null; String result = null; if (s != null) { try { b = Base64.decodeBase64(s); result = new String(b, "utf-8"); } catch (Exception e) { e.printStackTrace(); } } return result; } }