www.pudn.com > DestTest.rar > My3DESTOOL.java


package com.des.test; 
 
import java.security.SecureRandom; 
 
import javax.crypto.Cipher; 
import javax.crypto.SecretKey; 
import javax.crypto.SecretKeyFactory; 
import javax.crypto.spec.DESedeKeySpec; 
import javax.crypto.spec.IvParameterSpec; 
import javax.crypto.spec.SecretKeySpec; 
 
import sun.misc.BASE64Decoder; 
import sun.misc.BASE64Encoder; 
import javax.crypto.KeyGenerator; 
 
public class My3DESTOOL 
{ 
 
	public static String chargSet = "UTF-8"; 
 
	public static void main(String[] args) 
	{ 
		String key_String = "1234"; 
		String src_String = "12345678"; 
		String des_String = "";// vRzo6rN3XNg=//O2uWCKMP/CE= 
		String new_Src = null; 
		try 
		{ 
			// System.out.println("--------------------加密----------------------"); 
			// des_String = desEncrypt(key_String, src_String); 
		} catch (Exception e) 
		{ 
			// TODO Auto-generated catch block 
			e.printStackTrace(); 
		} 
		System.out.println("密文::::" + des_String); 
		// 4V9zJIiXLDpOcwGBeaihPw== 
		try 
		{ 
			// System.out.println("--------------------解密-----------------------"); 
			// new_Src = desDecrypt(key_String, des_String); 
			// System.out.println("明文::" + new_Src); 
		} catch (Exception e) 
		{ 
			// TODO Auto-generated catch block 
			e.printStackTrace(); 
		} 
		try 
		{ 
 
			modeTest(key_String, src_String); 
			// test(key_String, src_String); 
 
		} catch (Exception e) 
		{ 
			// TODO Auto-generated catch block 
			e.printStackTrace(); 
		} 
 
	} 
 
	// 进行3DES加密 
	public static String desEncrypt(String key_string, String src_string) 
			throws Exception 
	{ 
 
		// 生成DES密钥 
		SecretKey deskey; 
		// 加密后的密文 
		String des_String; 
		deskey = getDESKey(key_string);// 
		Cipher cipher = Cipher.getInstance("DESede", "SunJCE"); 
		cipher.init(Cipher.ENCRYPT_MODE, deskey); 
		byte srt_byte[] = src_string.getBytes(); 
		// BASE64转码 
		byte[] by = cipher.doFinal(srt_byte); 
 
		StringBuffer sb = new StringBuffer(); 
		for (int i = 0; i < by.length; i++) 
		{ 
			sb.append(by[i]); 
		} 
		System.out.println("srt_byt:::" + new String(by, "unicode")); 
		des_String = base64Encode(cipher.doFinal(srt_byte)); 
		// des_String = new String(cipher.doFinal(srt_byte)); 
		System.out.println("未转码的密文:::" + new String(cipher.doFinal(srt_byte))); 
		byte[] test = des_String.getBytes();// (byte[]) 
		// cipher.doFinal(srt_byte).clone(); 
 
		for (int i = 0; i < test.length; i++) 
		{ 
			System.out.println("test[" + i + "]" + test[i]); 
		} 
		// des_String = new String(test); 
		System.out.println("test:::::::::::::;" + des_String); 
		return des_String; 
	} 
 
	public static void test(String key_string, String src_string) 
			throws Exception 
	{ 
		System.out.println("================test================== "); 
		// String information = "DESEDE/CBC/PKCS5Padding"; 
		String information = "DESEDE/ECB/PKCS5Padding"; 
 
		// 生成DES密钥 
		SecretKey deskey; 
		deskey = getDESKey(key_string); 
		// 加密 
		Cipher cipher1 = Cipher.getInstance(information); 
		cipher1.init(Cipher.ENCRYPT_MODE, deskey); 
		byte[] t1 = cipher1.doFinal(src_string.getBytes()); 
		String desstring = new String(t1); 
		System.out.println("desstring" + ":" + desstring); 
		System.out.println("BASE_DESTSTRING:" + base64Encode(t1)); 
 
		// 解密 
		Cipher cipher2 = Cipher.getInstance(information); 
 
		cipher2.init(Cipher.DECRYPT_MODE, deskey); 
 
		byte[] t2 = cipher2.doFinal(base64Decode(base64Encode(t1))); 
		String srcString = new String(t2); 
		System.out.println("srcString::" + srcString); 
		for (int i = 0; i < t1.length; i++) 
		{ 
			System.out.println("t1[" + i + "]:" + t1[i] + "   "); 
		} 
 
	} 
 
	public static void paddtest(String key_string, String src_string, 
			String alom, String mode, String padding) throws Exception 
	{ 
		String information = alom + "/" + mode + "/" + padding; 
 
		System.out.println("================test================== "); 
		// 生成DES密钥 
		SecretKey deskey; 
		deskey = testGetDESKey(key_string, information); 
		// 加密 
		Cipher cipher1 = Cipher.getInstance(information); 
 
		// 创建IV并初始化cipher 
 
		System.out.print("正在初始化加密器...\n"); 
		byte[] iv = new byte[8]; // IV需占8字节 
		SecureRandom random = new SecureRandom(); 
		byte[] vi = new byte[8]; 
		random.nextBytes(vi); 
		for (int i = 0; i < vi.length; i++) 
		{ 
			System.out.println("vi[" + i + "]:" + vi[i]); 
		} 
		// iv = new byte[] ;//{ (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 
		// 0x00, 
		// (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00 }; 
 
		IvParameterSpec iv_param_spec = new IvParameterSpec(iv); 
		cipher1.init(Cipher.ENCRYPT_MODE, deskey, iv_param_spec); 
		byte[] iv2 = cipher1.getIV(); 
		if (iv2 != null) 
		{ 
 
			for (int i = 0; i < iv2.length; i++) 
			{ 
				System.out.println("iv2[" + i + "]" + iv2[i]); 
			} 
		} else 
		{ 
			System.out.println("iv2 is null!"); 
		} 
 
		byte[] t1 = cipher1.doFinal(src_string.getBytes(chargSet)); 
 
		String desstring = new String(t1, chargSet); 
		System.out.println("desstring" + ":" + desstring); 
		System.out.println("BASE_DESTSTRING:" + base64Encode(t1)); 
 
		/* 
		 *  
		 */ 
		byte[] basebytes = base64Encode(t1).getBytes(); 
		for (int i = 0; i < basebytes.length; i++) 
		{ 
			System.out.println("basebytes[" + i + "]" + basebytes[i]); 
		} 
		// 解密 
		Cipher cipher2 = Cipher.getInstance(information); 
		cipher2.init(Cipher.DECRYPT_MODE, deskey, iv_param_spec); 
		byte[] t2 = cipher2.doFinal(base64Decode(base64Encode(t1))); 
		String srcString = new String(t2); 
		System.out.println("srcString::" + srcString); 
		for (int i = 0; i < t1.length; i++) 
		{ 
			System.out.println("t1[" + i + "]:" + t1[i] + "   "); 
		} 
 
	} 
 
	public static String modeTest(String key_string, String src_string) 
	{ 
 
		String[] alom = { "DESEDE" }; 
		String[] modes = { "", "NONE", "CBC", "CFB", "ECB", "OFB", "PCBC" }; 
		String[] paddings = { "", "ISO10126Padding", "NoPadding", 
				"PKCS5Padding", "SSL3Padding", "OAEPWithMD5AndMGF1Padding" }; 
		int count = 0; 
		for (int h = 0; h < alom.length; h++) 
		{ 
			for (int i = 0; i < modes.length; i++) 
			{ 
				for (int j = 0; j < paddings.length; j++) 
				{ 
					System.out 
							.println("\n********************************count:" 
									+ (++count) 
									+ "********************************"); 
					System.out.println("START***************************" 
							+ alom[h] + "/" + modes[i] + "/" + paddings[j] 
							+ "***********************************"); 
					try 
					{ 
						paddtest(key_string, src_string, alom[h], modes[i], 
								paddings[j]); 
					} catch (Exception e) 
					{ 
						// TODO Auto-generated catch block 
						System.out.println("Exception:" + e.toString()); 
						// e.printStackTrace(); 
					} 
					System.out.println("END***************************" 
							+ modes[i] + "/" + paddings[j] 
							+ "***********************************"); 
				} 
			} 
		} 
 
		return null; 
	} 
 
	// 进行3DES解密 
	public static String desDecrypt(String key_string, String des_string) 
			throws Exception 
	{ 
 
		// 生成DES密钥 
		SecretKey deskey; 
		// 未解密的明文 
		String desede_String; 
		// 解密后的明文 
		String src_String; 
		BASE64Encoder base64Encoder = new BASE64Encoder(); 
		deskey = getDESKey(key_string); 
		Cipher cipher = Cipher.getInstance("DESede", "SunJCE"); 
		cipher.init(Cipher.DECRYPT_MODE, deskey); 
		byte[] test = des_string.getBytes(); 
		for (int i = 0; i < test.length; i++) 
		{ 
			System.out.println("test[" + i + "]" + test[i]); 
		} 
		byte srt_byte[] = base64Decode(des_string); 
		System.out.println("srt_byte[]::" + srt_byte.length); 
		src_String = new String(cipher.doFinal(srt_byte)); 
		// src_String = new String(cipher.doFinal(test)); 
		return src_String; 
	} 
 
	/** 
	 * 生成3DES密钥. 
	 *  
	 * @param key_byte 
	 *            seed key 
	 * @throws Exception 
	 * @return javax.crypto.SecretKey Generated DES key 
	 */ 
	public static SecretKey getDESKey(String key_string) throws Exception 
	{ 
		SecretKey key = null; 
		byte[] key_byte = null; 
		// 判断密钥的长度,如果不是24位,则以"0"补齐 
		String zeros = "000000000000000000000000"; 
		if (key_string != null) 
		{ 
			int keylength = key_string.getBytes().length; 
			if (keylength < 24) 
			{ 
				key_string += zeros.substring(keylength); 
			} 
		} else 
		{ 
			return null; 
		} 
		key_byte = key_string.getBytes(chargSet); 
		DESedeKeySpec dks = new DESedeKeySpec(key_byte); 
		SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede"); 
		key = keyFactory.generateSecret(dks); 
		System.out.println("密钥:key_string:" + key_string); 
		return key; 
	} 
 
	/** 
	 * 生成3DES密钥. 
	 *  
	 * @param key_byte 
	 *            seed key 
	 * @throws Exception 
	 * @return javax.crypto.SecretKey Generated DES key 
	 */ 
	public static SecretKey testGetDESKey(String key_string, 
			String modeAndPadding) throws Exception 
	{ 
		SecretKey key = null; 
		byte[] key_byte = null; 
		// 判断密钥的长度,如果不是24位,则以"0"补齐 
		String zeros = "000000000000000000000000"; 
 
		if (key_string != null) 
		{ 
			int keylength = key_string.getBytes().length; 
			if (keylength < 24) 
			{ 
				key_string += zeros.substring(keylength); 
			} 
		} else 
		{ 
			return null; 
		} 
 
		System.out.println("key_string:" + key_string); 
		key_byte = key_string.getBytes(); 
		DESedeKeySpec dks = new DESedeKeySpec(generateDESKeyBytes(key_string)); 
		SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede"); 
		key = keyFactory.generateSecret(dks); 
		return key; 
	} 
 
	/** 
	 * 生成byte[24]的数组,不足用0补足 
	 *  
	 * @param keyWord 
	 * @return 
	 */ 
	private static byte[] generateDESKeyBytes(String keyWord) 
	{ 
		byte[] key = new byte[24]; 
 
		byte[] tmp = keyWord.getBytes(); 
		for (int i = 0; i < key.length; i++) 
		{ 
			if (i < tmp.length) 
				key[i] = tmp[i]; 
		} 
 
		System.out.println(); 
		return key; 
	} 
 
	/* 
	 * 对普通字符串进行BASE64转码 srcString :普通字符串 
	 */ 
	public static String base64Encode(byte[] srcString)// 加base64 
	{ 
		BASE64Encoder base64Encoder = new BASE64Encoder(); 
		try 
		{ 
			// 经过BASE64加密后的密文 
			String base64String = base64Encoder.encode(srcString); 
			return base64String; 
		} catch (Exception e) 
		{ 
			e.printStackTrace(); 
			return null; 
		} 
	} 
 
	/* 
	 * 将BASE64字符串解码 base64String:BASE64字符串 
	 */ 
	public static byte[] base64Decode(String base64String)// 解base64 
	{ 
		BASE64Decoder base64Decoder = new BASE64Decoder(); 
		try 
		{ 
			// 将BASE64转码过的字符串进行解码,获取明文 
			byte[] src_byte = base64Decoder.decodeBuffer(base64String); 
			return src_byte; 
		} catch (Exception e) 
		{ 
			e.printStackTrace(); 
			return null; 
		} 
	} 
 
}