package com.common.util.CipherUtil; import java.io.ByteArrayOutputStream; import java.io.UnsupportedEncodingException; import java.security.*; import javax.crypto.*; import javax.crypto.spec.SecretKeySpec; /** * 字符串 DESede(3DES) 加密,是對稱加密算法 */ public class TDESUtil { private static final String Algorithm = "DESede"; // 定義 加密算法,可用 DES,DESede,Blowfish private static final String hexString="0123456789ABCDEF"; /** * * @param keybyte 加密密鑰,長度爲24字節 * @param src 字節數組(根據給定的字節數組構造一個密鑰。 ) * @return */ public static byte[] encryptMode(byte[] keybyte, byte[] src) { try { // 根據給定的字節數組和算法構造一個密鑰 SecretKey deskey = new SecretKeySpec(keybyte, Algorithm); // 加密 Cipher c1 = Cipher.getInstance(Algorithm); c1.init(Cipher.ENCRYPT_MODE, deskey); return c1.doFinal(src); } catch (java.security.NoSuchAlgorithmException e1) { e1.printStackTrace(); } catch (javax.crypto.NoSuchPaddingException e2) { e2.printStackTrace(); } catch (java.lang.Exception e3) { e3.printStackTrace(); } return null; } /** * * @param keybyte 密鑰 * @param src 須要解密的數據 * @return */ public static byte[] decryptMode(byte[] keybyte, byte[] src) { try { // 生成密鑰 SecretKey deskey = new SecretKeySpec(keybyte, Algorithm); // 解密 Cipher c1 = Cipher.getInstance(Algorithm); c1.init(Cipher.DECRYPT_MODE, deskey); return c1.doFinal(src); } catch (java.security.NoSuchAlgorithmException e1) { e1.printStackTrace(); } catch (javax.crypto.NoSuchPaddingException e2) { e2.printStackTrace(); } catch (java.lang.Exception e3) { e3.printStackTrace(); } return null; } /** * 字符串轉爲16進制 * @param str * @return */ public static String encode(String str) { //根據默認編碼獲取字節數組 byte[] bytes=str.getBytes(); StringBuilder sb=new StringBuilder(bytes.length*2); //將字節數組中每一個字節拆解成2位16進制整數 for(int i=0;i<bytes.length;i++) { sb.append(hexString.charAt((bytes[i]&0xf0)>>4)); sb.append(hexString.charAt((bytes[i]&0x0f)>>0)); } return sb.toString(); } /** * * @param bytes * @return * 將16進制數字解碼成字符串,適用於全部字符(包括中文) */ public static String decode(String bytes) { ByteArrayOutputStream baos=new ByteArrayOutputStream(bytes.length()/2); //將每2位16進制整數組裝成一個字節 for(int i=0;i<bytes.length();i+=2) baos.write((hexString.indexOf(bytes.charAt(i))<<4 |hexString.indexOf(bytes.charAt(i+1)))); return new String(baos.toByteArray()); } // 轉換成十六進制字符串 public static String byte2hex(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; if (n < b.length - 1) hs = hs + ":"; } return hs.toUpperCase(); } public static void main(String[] args) throws UnsupportedEncodingException { // 添加新安全算法,若是用JCE就要把它添加進去 //這裏addProvider方法是增長一個新的加密算法提供者(我的理解沒有找到好的答案,求補充) Security.addProvider(new com.sun.crypto.provider.SunJCE()); //測試代碼 // String szSrc = "This is a 3DES test. 測試"; String sourcePlatformID = "110000" ; String userID = "21" ; String date = "1350008287462" ; String randomNum = "123456"; String ticket = sourcePlatformID + "&" + userID + "&" + date +randomNum ; System.out.println("加密前的字符串:" + ticket); //字符串密鑰 String aString = "123456123456123456123456"; //也能夠直接定義字節數組 final byte[] keyBytes = { 0x11, 0x22, 0x4F, 0x58, (byte)0x88, 0x10, 0x40, 0x38, 0x28, 0x25, 0x79, 0x51, (byte) 0xCB, (byte) 0xDD, 0x55, 0x66, 0x77, 0x29, 0x74, (byte) 0x98, 0x30, 0x40, 0x36, (byte) 0xE2 }; byte[] encoded = encryptMode(aString.getBytes(), ticket.getBytes()); System.out.println("加密後的字符串:" + new String(encoded,"UTF8")); byte[] srcBytes = decryptMode(aString.getBytes(), encoded); System.out.println("解密後的字符串:" + new String(srcBytes)); /*------------------------------------------------------------*/ String byt="字符串轉換爲16進制測試咯"; byt=encode(byt); System.out.println("字符串轉爲16進制: "+byt); byt=decode(byt); System.out.println("16進制數字解碼成字符串: "+byt); } }