package com.gzcb.creditcard.txcard.cashout.util; import sun.misc.BASE64Decoder; import javax.crypto.Cipher; import javax.crypto.SecretKey; import javax.crypto.SecretKeyFactory; import javax.crypto.spec.DESKeySpec; import javax.crypto.spec.DESedeKeySpec; import javax.crypto.spec.IvParameterSpec; /** * @author xingguanghui * @create 2018-03-21 16:41 **/ @SuppressWarnings({ "restriction" }) public class ThreeDES { private static final String IV = "1234567-"; public static final String KEY = "123456781234567812345678"; /** * DESCBC加密 * * @param src * 數據源 * @param key * 密鑰,長度必須是8的倍數 * @return 返回加密後的數據 * @throws Exception */ public String encryptDESCBC(final String src, final String key) throws Exception { // --生成key,同時制定是des仍是DESede,二者的key長度要求不一樣 final DESKeySpec desKeySpec = new DESKeySpec(key.getBytes("UTF-8")); final SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); final SecretKey secretKey = keyFactory.generateSecret(desKeySpec); // --加密向量 final IvParameterSpec iv = new IvParameterSpec(IV.getBytes("UTF-8")); // --經過Chipher執行加密獲得的是一個byte的數組,Cipher.getInstance("DES")就是採用ECB模式,cipher.init(Cipher.ENCRYPT_MODE, // secretKey)就能夠了. final Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE, secretKey, iv); final byte[] b = cipher.doFinal(src.getBytes("UTF-8")); // --經過base64,將加密數組轉換成字符串 String encode = Base64.encode(b); return encode; } /** * DESCBC解密 * * @param src * 數據源 * @param key * 密鑰,長度必須是8的倍數 * @return 返回解密後的原始數據 * @throws Exception */ public String decryptDESCBC(final String src, final String key) throws Exception { // --經過base64,將字符串轉成byte數組 byte[] bytesrc = Base64.decode(src); // --解密的key final DESKeySpec desKeySpec = new DESKeySpec(key.getBytes("UTF-8")); final SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); final SecretKey secretKey = keyFactory.generateSecret(desKeySpec); // --向量 final IvParameterSpec iv = new IvParameterSpec(IV.getBytes("UTF-8")); // --Chipher對象解密Cipher.getInstance("DES")就是採用ECB模式,cipher.init(Cipher.DECRYPT_MODE, // secretKey)就能夠了. final Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding"); // cipher.init(Cipher.DECRYPT_MODE, secretKey, iv); cipher.init(Cipher.DECRYPT_MODE, secretKey); final byte[] retByte = cipher.doFinal(bytesrc); return new String(retByte); } // 3DESECB加密,key必須是長度大於等於 3*8 = 24 位哈 public static String encryptThreeDESECB(final String src, final String key) throws Exception { // final DESedeKeySpec dks = new DESedeKeySpec(key.getBytes("UTF-8")); final DESedeKeySpec dks = new DESedeKeySpec(hex2byte(key)); final SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede"); final SecretKey securekey = keyFactory.generateSecret(dks); final Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE, securekey); final byte[] b = cipher.doFinal(hex2byte(src)); // final byte[] b = cipher.doFinal(src.getBytes()); String s = byte2hex(b); System.out.println("-->"+s); String encode = Base64.encode(b); return encode.replaceAll("\r", "").replaceAll("\n", ""); } public static void main(String[] args) { try { String s = encryptThreeDESECB("3132333435360000", "209DD68C90A2E89032908F0909821343209DD68C90A2E890"); // String s = encryptThreeDESECB("3132333435360000", "209DD68C90A2E89032908F0909821343209DD68C90A2E890"); System.out.println(s); } catch (Exception e) { e.printStackTrace(); } } // 3DESECB解密,key必須是長度大於等於 3*8 = 24 位哈 public String decryptThreeDESECB(final String src, final String key) throws Exception { // --經過base64,將字符串轉成byte數組 final BASE64Decoder decoder = new BASE64Decoder(); final byte[] bytesrc = decoder.decodeBuffer(src); // --解密的key final DESedeKeySpec dks = new DESedeKeySpec(key.getBytes("UTF-8")); final SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede"); final SecretKey securekey = keyFactory.generateSecret(dks); // --Chipher對象解密 final Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding"); cipher.init(Cipher.DECRYPT_MODE, securekey); final byte[] retByte = cipher.doFinal(bytesrc); return new String(retByte); } /** * 字節數組轉換成16進制字符串 * * @param b * @return */ public static String byte2hex(byte[] b) {// 二行制轉字符串 String hs = ""; String stmp = ""; for (int n = 0; n < b.length; n++) { stmp = byteHEX(b[n]); hs = hs + stmp; } return hs; } static String byteHEX(byte ib) { char[] Digit = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; char[] ob = new char[2]; ob[0] = Digit[(ib >>> 4) & 0X0F]; ob[1] = Digit[ib & 0X0F]; String s = new String(ob); return s; } /** * 16進制字符串轉換成字節數組 * * @param hex * @return */ public static byte[] hex2byte(String hex) { int len = (hex.length() / 2); byte[] result = new byte[len]; char[] achar = hex.toCharArray(); for (int i = 0; i < len; i++) { int pos = i * 2; result[i] = (byte) (toByte(achar[pos]) << 4 | toByte(achar[pos + 1])); } return result; } static byte toByte(char c) { byte b = (byte) "0123456789ABCDEF".indexOf(c); return b; } }