3DES加密解密的例子

package com.snailteam.adserver.until;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;



/**
 * @author :xiaofancn
 * @version :2011-11-11 上午11:02:22
 * 
 */

 
public class Tool3DES {
	private static final String Algorithm = "DESede"; // 定义 加密算法,可用
	private static final int Keysize = 168;
	
	/**
	 * 将byte[]转化成16进制字符串
	 * @param buf
	 * @return
	 */
	public static String parseByte2HexStr(byte buf[]) {
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < buf.length; i++) {
			String hex = Integer.toHexString(buf[i] & 0xFF);
			if (hex.length() == 1) {
				hex = '0' + hex;
			}
			sb.append(hex.toUpperCase());
		}
		return sb.toString();
	}
	/**
	 * 将16进制字符串转化成byte[]
	 * @param buf
	 * @return
	 */
	public static byte[] parseHexStr2Byte(String hexStr) {
		if (hexStr.length() < 1)
			return null;
		byte[] result = new byte[hexStr.length() / 2];
		for (int i = 0; i < hexStr.length() / 2; i++) {
			int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);
			int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2),
					16);
			result[i] = (byte) (high * 16 + low);
		}
		return result;
	}

	/**
	 *  加密
	 *   message->密文byte[]->16进制密文字符串
	 * @param password
	 * @param message
	 * @return
	 */
	public static String encrypt(String password, String message) {
		try {
			KeyGenerator kgen = KeyGenerator.getInstance(Algorithm);
			kgen.init(Keysize, new SecureRandom(password.getBytes()));
			SecretKey secretKey = kgen.generateKey();
			byte[] enCodeFormat = secretKey.getEncoded();
			SecretKeySpec key = new SecretKeySpec(enCodeFormat, Algorithm);
			Cipher cipher = Cipher.getInstance(Algorithm);// 创建密码器
			byte[] byteContent = message.getBytes("utf-8");
			cipher.init(Cipher.ENCRYPT_MODE, key);// 初始化
			byte[] result = cipher.doFinal(byteContent);
			return parseByte2HexStr(result);
		} catch (NoSuchAlgorithmException e) {
			
			e.printStackTrace();
		} catch (NoSuchPaddingException e) {
			
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			
			e.printStackTrace();
		} catch (InvalidKeyException e) {
			
			e.printStackTrace();
		} catch (IllegalBlockSizeException e) {
			
			e.printStackTrace();
		} catch (BadPaddingException e) {
			
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 解密
	 *  16进制密文字符串->byte[]->解密
	 * @param password
	 * @param message
	 * @return
	 */
	public static String decrypt(String password, String message) {
		KeyGenerator kgen;
		try {
			kgen = KeyGenerator.getInstance(Algorithm);
			kgen.init(Keysize, new SecureRandom(password.getBytes()));
			SecretKey secretKey = kgen.generateKey();
			byte[] enCodeFormat = secretKey.getEncoded();
			SecretKeySpec key = new SecretKeySpec(enCodeFormat, Algorithm);
			Cipher cipher = Cipher.getInstance(Algorithm);// 创建密码器
			byte[] byteContent = parseHexStr2Byte(message);
			cipher.init(Cipher.DECRYPT_MODE, key);// 初始化
			byte[] result = cipher.doFinal(byteContent);
			return new String(result);
		} catch (NoSuchAlgorithmException e) {
			
			e.printStackTrace();
		} catch (NoSuchPaddingException e) {
			
			e.printStackTrace();
		} catch (InvalidKeyException e) {
			
			e.printStackTrace();
		} catch (IllegalBlockSizeException e) {
			
			e.printStackTrace();
		} catch (BadPaddingException e) {
			
			e.printStackTrace();
		}
		return null;
	}

	public static void main(String[] args) {
		String content = "你好";
		String password = "123456789";
		System.out.println("加密前:"+content);
		String temp = encrypt(password, content);
		System.out.println("加密后:"+temp);
		temp = decrypt(password, temp);
		System.out.println("解密后:"+temp);
	}
}
 

猜你喜欢

转载自xiaofancn.iteye.com/blog/1254435