天天看點

AES加密和解密

package com.jn;

import java.net.URLDecoder;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;



import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class AESUtil {
	
	public static final String DEFAULT_KEY = "123!@#abcs59aYGM";
	/**
	 * 加密--把加密後的byte數組先進行二進制轉16進制在進行base64編碼
	 * 
	 * @param sSrc
	 * @param sKey
	 * @return
	 * @throws Exception
	 */
	public static String encrypt(String sSrc, String sKey) throws Exception {
		if (sKey == null) {
			throw new IllegalArgumentException("Argument sKey is null.");
		}
		if (sKey.length() != 16) {
			throw new IllegalArgumentException(
					"Argument sKey'length is not 16.");
		}
		byte[] raw = sKey.getBytes("ASCII");
		SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
		Cipher cipher = Cipher.getInstance("AES");
		cipher.init(Cipher.ENCRYPT_MODE, skeySpec);

		byte[] encrypted = cipher.doFinal(sSrc.getBytes("UTF-8"));
		String tempStr = parseByte2HexStr(encrypted);

		BASE64Encoder encoder = new BASE64Encoder();
		return encoder.encode(tempStr.getBytes("UTF-8"));
	}

	/**
	 * 解密--先 進行base64解碼,在進行16進制轉為2進制然後再解碼
	 * 
	 * @param sSrc
	 * @param sKey
	 * @return
	 * @throws Exception
	 */
	public static String decrypt(String sSrc, String sKey) throws Exception {

		if (sKey == null) {
			throw new IllegalArgumentException("499");
		}
		if (sKey.length() != 16) {
			throw new IllegalArgumentException("498");
		}

		byte[] raw = sKey.getBytes("ASCII");
		SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");

		Cipher cipher = Cipher.getInstance("AES");
		cipher.init(Cipher.DECRYPT_MODE, skeySpec);

		BASE64Decoder encoder = new BASE64Decoder();
		byte[] encrypted1 = encoder.decodeBuffer(sSrc);

		String tempStr = new String(encrypted1, "utf-8");
		encrypted1 = parseHexStr2Byte(tempStr);
		byte[] original = cipher.doFinal(encrypted1);
		String originalString = new String(original, "utf-8");
		return originalString;
	}

	/**
	 * 将二進制轉換成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進制轉換為二進制
	 * 
	 * @param hexStr
	 * @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;
	}
	
	
	
	public static void main(String[] args) throws Exception {
//		String te = "action=register#mobile=13168766551#giftID=2707#workID=lengDeng#url=http://www.163.com/";
		String te = "action=regist";
		String decString =AESUtil.encrypt(te,DEFAULT_KEY);
		System.out.println("加密 : "+decString);
//		decString = URLDecoder.decode(decString, "GBK");
//		System.err.println(decString);
		String s = AESUtil.decrypt(decString, DEFAULT_KEY);
		System.out.println("解密 : " + s);

	}
}