JAVA AES加密解密

import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

public class MyKeyGenerator {
	
	public static void main(String[] args) {
		try {
			String word = "要加密的";
			String ALGORITHM="AES";
			System.out.println(byteToHexString(word.getBytes()));
			KeyGenerator keyGenerator = KeyGenerator.getInstance(ALGORITHM);
			keyGenerator.init(128,new SecureRandom());  //默認是128  AES要求密鑰長度爲128,192,256位   
			SecretKey secretKey = keyGenerator.generateKey(); //生成密鑰
			byte[] bytes = secretKey.getEncoded();

			String key = byteToHexString(bytes);                        //String key = HexBin.encode(bytes);
			System.out.println("16進制的密鑰:"+key);
			//String key2 = toHexString(bytes);
			//System.out.println(key2);
			//AES加密
			SecretKey secretKey2 = new SecretKeySpec(hexStringToBytes(key), ALGORITHM);
			Cipher cipher = Cipher.getInstance(ALGORITHM);
			cipher.init(Cipher.ENCRYPT_MODE, secretKey2);
			byte[] cipherByte = cipher.doFinal(word.getBytes()); //加密
			
			String result = byteToHexString(cipherByte);
			System.out.println("AES加密結果:"+result);
			
			//AES解密
			SecretKey secretKey3 = new SecretKeySpec(hexStringToBytes(key), ALGORITHM);//恢復密鑰                        //SecretKey secretKey2 = new SecretKeySpec(HexBin.decode(key), ALGORITHM);
			Cipher cipher2 = Cipher.getInstance(ALGORITHM);//Cipher完成加密或解密工做類
			cipher2.init(Cipher.DECRYPT_MODE, secretKey3);//對Cipher初始化,解密模式
			byte[] cipherByte2 = cipher2.doFinal(hexStringToBytes(result));//解密data
			
			System.out.println("AES解密結果:"+byteToHexString(cipherByte2));
			
			System.out.println(new String(cipherByte2));
			
			
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchPaddingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvalidKeyException 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();
		} 
		

	}
	
	 /**
	  * byte數組轉化爲16進制字符串
	  * @param bytes
	  * @return
	  */
	  public static String byteToHexString(byte[] bytes) {
	    StringBuffer sb = new StringBuffer();
	    for (int i = 0; i < bytes.length; i++) {
	      String strHex=Integer.toHexString(bytes[i]);
	      if(strHex.length() > 3) {
	        sb.append(strHex.substring(6));
	      } else {
	        if(strHex.length() < 2) {
	          sb.append("0" + strHex);
	        } else {
	          sb.append(strHex);
	        }
	      }
	    }
	    return sb.toString();
	  }
	  
	  /**
	   * 16進制字符串轉爲byte數組
	   * @param hexString
	   * @return
	   */
	  public static byte[] hexStringToBytes(String hexString) {   
		    if (hexString == null || hexString.equals("")) {   
		        return null;   
		    }   
		    hexString = hexString.toUpperCase();   
		    int length = hexString.length() / 2;   
		    char[] hexChars = hexString.toCharArray();   
		    byte[] d = new byte[length];   
		    for (int i = 0; i < length; i++) {   
		        int pos = i * 2;   
		        d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));   
		    }   
		    return d;   
		}
	  
	  private static byte charToByte(char c) {   
		    return (byte) "0123456789ABCDEF".indexOf(c);   
	  } 

}
相關文章
相關標籤/搜索