AES加解密

  • 22254
  • 0
  • Java
  • 2013-11-18

AES

AES加密主要需含有一組KEY及Initial Vector,再加密後通常在以BASE64Encoder之方式encode後傳出!

KEY - 可用KeyGenerator產出或雙方約定好固定16字元之方式轉成ByteArray後組KEY

Initial Vector - 通常為雙方講好之固定IV,以16位元表示出來做KEY


	private static final byte[] miv = {(byte) 0x68, (byte) 0x41, (byte) 0x76, (byte) 0x78,
										(byte) 0x52, (byte) 0x48, (byte) 0x6A, (byte) 0x70,
		                                 (byte) 0x64, (byte) 0x72, (byte) 0x48, (byte) 0x4A,
		                                 (byte) 0x4C, (byte) 0x39, (byte) 0x4F, (byte) 0x76};

 

其AES Class如下:


import javax.crypto.Cipher;
//import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

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

/*******************************************************************************
 * AES加解密算法
 */
public class AES 
{
	private static final String ALGORITHM = "AES";
	private static final String TRANSFORM = "AES/CBC/PKCS5Padding";
	
	private static final byte[] miv = {(byte) 0x68, (byte) 0x41, (byte) 0x76, (byte) 0x78,
										(byte) 0x52, (byte) 0x48, (byte) 0x6A, (byte) 0x70,
		                                 (byte) 0x64, (byte) 0x72, (byte) 0x48, (byte) 0x4A,
		                                 (byte) 0x4C, (byte) 0x39, (byte) 0x4F, (byte) 0x76};
	private static final IvParameterSpec iv = new IvParameterSpec(miv);
	
	private SecretKey key;
	
	public AES() 
	{
	}
	
	public void SetKey(byte[] bkey) throws IllegalArgumentException
	{
		key = new SecretKeySpec(bkey, ALGORITHM);   
	}
	
	public byte[] GetKey()
	{
		if (key == null)
			return null;
		else
			return key.getEncoded();
	}
	
	public String Encrypt(String sSrc) throws Exception 
	{
		// generate key	or using main func to fixed key.
		KeyGenerator keyGenerator = KeyGenerator.getInstance(ALGORITHM);
		keyGenerator.init(128);
		key = keyGenerator.generateKey();
		
		Cipher cipher = Cipher.getInstance(TRANSFORM);
		cipher.init(Cipher.ENCRYPT_MODE, key, iv);
		byte[] encrypted = cipher.doFinal(sSrc.getBytes());
		String enString = new BASE64Encoder().encode(encrypted);	//BASE64
		return enString;
	}
	
	public String Decrypt(String etext, byte[] bkey) throws Exception
	{
		SetKey(bkey);
		return Decrypt(etext);
	}
	
	public String Decrypt(String sSrc) throws Exception 
	{
		if (key == null)
			return null;
			
		Cipher cipher = Cipher.getInstance(TRANSFORM);
		cipher.init(Cipher.DECRYPT_MODE, key, iv);
		byte[] encrypted1 = new BASE64Decoder().decodeBuffer(sSrc);	//BASE64
		byte[] original = cipher.doFinal(encrypted1);
		String originalString = new String(original);
		return originalString;
	}
	
	public static void main(String[] args) throws Exception 
	{
		AES aes = new AES();
		
		/*	SetKey
		byte[] bKey = {(byte) 0x95, (byte) 0xE1, (byte) 0xBD, (byte) 0x58, (byte) 0x7D, (byte) 0xCC, (byte) 0x41, (byte) 0x08,
				(byte) 0xF5, (byte) 0x97, (byte) 0x55, (byte) 0x92, (byte) 0x23, (byte) 0x2E, (byte) 0x5F, (byte) 0x01};
		aes.SetKey(bKey);
		*/
		
		String s = "The quick brown fox jumps";
		System.out.println("String: " + s);
		String e = aes.Encrypt(s);
		System.out.println("Encryption: " + e);
		String d = aes.Decrypt(e);
		System.out.println("Dncryption: " + d);

		/* export key, decrypt 
		byte [] bkey = aes.GetKey();
		AES a1 = new AES();
		d = a1.Decrypt(e, bkey);
		System.out.println("Encryption = " + d);
		*/
	}
}