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


package com.des.test; 
 
import java.io.IOException; 
import java.security.InvalidAlgorithmParameterException; 
import java.security.InvalidKeyException; 
import java.security.NoSuchAlgorithmException; 
import java.security.SecureRandom; 
import java.security.spec.InvalidKeySpecException; 
 
import javax.crypto.BadPaddingException; 
import javax.crypto.Cipher; 
import javax.crypto.IllegalBlockSizeException; 
import javax.crypto.NoSuchPaddingException; 
import javax.crypto.SecretKey; 
import javax.crypto.SecretKeyFactory; 
import javax.crypto.spec.DESedeKeySpec; 
import javax.crypto.spec.IvParameterSpec; 
 
import org.apache.commons.codec.DecoderException; 
import org.apache.commons.codec.digest.DigestUtils; 
import org.apache.commons.codec.binary.Base64; 
import org.apache.commons.codec.binary.Hex; 
import sun.misc.BASE64Decoder; 
import sun.misc.BASE64Encoder; 
import org.apache.commons.codec.digest.DigestUtils;; 
 
public class TEST 
{ 
 
	public static void main(String[] args) 
	{ 
		byte[] md5 = DigestUtils.md5("abc".getBytes()); 
		 
		TEST tripleTest = new TEST(); 
		String key_string = "123400000000000000000000"; 
		String src_string = "12345678"; 
		String des_string = ""; 
		String information = "DESEDE";// "DESEDE/ECB/PKCS5Padding"; 
		String information2 = "DESEDE/CBC/PKCS5Padding"; 
		try 
		{ 
			SecretKey key = tripleTest.getDESKey(key_string); 
			SecureRandom random = new SecureRandom(); 
 
			byte[] iv = new byte[] { (byte) 0x00, (byte) 0x00, (byte) 0x00, 
					(byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, 
					(byte) 0x00 }; 
 
			byte[] vi = new byte[8]; 
			byte[] tt = new byte[8]; 
			int COUNT = 0; 
			// while (!"Brsl0etq4fQ1fVmYgeeVdw==".equals(des_string)) 
			// { 
			System.out.println("**********************" + (++COUNT) 
					+ "***********************"); 
 
			random.nextBytes(vi); 
			IvParameterSpec iv_param_spec = new IvParameterSpec(iv); 
			// 加密 
			Cipher cipher1 = Cipher.getInstance(information); 
			cipher1.init(Cipher.ENCRYPT_MODE, key); 
			byte[] des_byte = cipher1.doFinal(src_string.getBytes()); 
			System.out.println("src_string.getBytes()" 
					+ src_string.getBytes().length + "\n加密后数组长度des_byte:" 
					+ des_byte.length); 
			des_string = tripleTest.encodeBase64(des_byte); 
			System.out.println("des_string:" + des_string); 
 
			// CBC加密,带初始化向量的加密方法 
			Cipher cipher3 = Cipher.getInstance(information2); 
			cipher3.init(Cipher.ENCRYPT_MODE, key, iv_param_spec); 
			byte[] des_byte_cbc = cipher3.doFinal(Hex.decodeHex(src_string 
					.toCharArray())); 
			System.out.println("Hex.decodeHexString::::" 
					+ new String(Base64.encodeBase64(des_byte_cbc))); 
			System.out.println(des_byte_cbc.length); 
			String des_string_cbc = tripleTest.encodeBase64(des_byte_cbc); 
			System.out.println("des_string_cbc:" + des_string_cbc); 
 
			// 解密 
			cipher1.init(Cipher.DECRYPT_MODE, key); 
			des_byte = tripleTest.decodeBase64(des_string); 
			byte[] src_byte = cipher1.doFinal(des_byte); 
			src_string = new String(src_byte); 
			System.out.println("src_string:" + src_string); 
			System.out.println("MD5::" 
					+ tripleTest.encodeBase64(new String(md5).getBytes())); 
 
			// } 
 
		} catch (InvalidKeyException e) 
		{ 
			// TODO Auto-generated catch block 
			e.printStackTrace(); 
		} catch (NoSuchAlgorithmException e) 
		{ 
			// TODO Auto-generated catch block 
			e.printStackTrace(); 
		} catch (InvalidKeySpecException e) 
		{ 
			// TODO Auto-generated catch block 
			e.printStackTrace(); 
		} catch (NoSuchPaddingException e) 
		{ 
			// TODO Auto-generated catch block 
			e.printStackTrace(); 
		} catch (IllegalStateException e) 
		{ 
			// TODO Auto-generated catch block 
			e.printStackTrace(); 
		} catch (IllegalBlockSizeException e) 
		{ 
			// TODO Auto-generated catch block 
			e.printStackTrace(); 
		} catch (BadPaddingException e) 
		{ 
			// TODO Auto-generated catch block 
			e.printStackTrace(); 
		} catch (InvalidAlgorithmParameterException e) 
		{ 
			// TODO Auto-generated catch block 
			e.printStackTrace(); 
		} catch (DecoderException e) 
		{ 
			// TODO Auto-generated catch block 
			e.printStackTrace(); 
		} 
 
	} // base64转码 
 
	public static String encodeBase64(byte[] src_byte) 
	{ 
		BASE64Encoder base64Encoder = new BASE64Encoder(); 
 
		try 
		{ 
			// 经过BASE64加密后的密文 
			String base64String = base64Encoder.encodeBuffer(src_byte); 
			String a = base64Encoder.encodeBuffer(src_byte); 
			String b = base64Encoder.encode(src_byte); 
			System.out.println("a:" + a + "b:" + b); 
			return base64String; 
		} catch (Exception e) 
		{ 
			e.printStackTrace(); 
			return null; 
		} 
 
	} 
 
	// base64解码 
	public static byte[] decodeBase64(String base64_string) 
	{ 
		BASE64Decoder base64Decoder = new BASE64Decoder(); 
		try 
		{ 
			// 将BASE64转码过的字符串进行解码,获取明文 
			byte[] src_byte = base64Decoder.decodeBuffer(base64_string); 
			return src_byte; 
		} catch (Exception e) 
		{ 
			e.printStackTrace(); 
			return null; 
		} 
 
	} 
 
	// 获取密钥 
 
	public static SecretKey getDESKey(String key_string) 
			throws InvalidKeyException, NoSuchAlgorithmException, 
			InvalidKeySpecException, DecoderException 
	{ 
		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_string = encodeBase64(key_string.getBytes()); 
		key_byte = key_string.getBytes(); 
		System.out.println("new key_string::" + new String(key_byte)); 
 
		DESedeKeySpec dks = new DESedeKeySpec(key_string.getBytes()); 
		byte[] newbyte = Hex.decodeHex(key_string.toCharArray()); 
		for (int i = 0; i < newbyte.length; i++) 
		{ 
			System.out.println("newbyte[" + i + "]" + newbyte[i]); 
		} 
		SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede"); 
		key = keyFactory.generateSecret(dks); 
 
		return key; 
	} 
 
}