AES

5130 단어 자바aes
package com.forever.util;

import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.Security;

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

public class AES {
	
	//KeyGenerator             ,      
	private KeyGenerator keygen;
	//SecretKey         
	private SecretKey deskey;
	//Cipher           
	private Cipher c;
	//              
	private byte[] cipherByte;
	
	private static final String password = "12345678"; //     
	
	public AES() throws NoSuchAlgorithmException, NoSuchPaddingException{
		Security.addProvider(new com.sun.crypto.provider.SunJCE());
		//     DES        (          ,      )
		keygen = KeyGenerator.getInstance("AES");
		keygen.init(128, new SecureRandom(password.getBytes())); 
		//    
		deskey = keygen.generateKey();
		//  Cipher  ,      DES  
		c = Cipher.getInstance("AES");
	}
	
	/**
	 *       
	 * 
	 * @param str
	 * @return
	 * @throws InvalidKeyException
	 * @throws IllegalBlockSizeException
	 * @throws BadPaddingException
	 * @throws UnsupportedEncodingException 
	 */
	public byte[] Encrytor(String str) throws InvalidKeyException,
			IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException {
		//     , Cipher       ,ENCRYPT_MODE      
		c.init(Cipher.ENCRYPT_MODE, deskey);
		byte[] src = str.getBytes();
		//   ,     cipherByte
		cipherByte = c.doFinal(src);
		return cipherByte;
	}
	
	public String encrytor(String str) throws InvalidKeyException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException{
		return parseByte2HexStr(Encrytor(str));
	}
	

	/**
	 *       
	 * 
	 * @param buff
	 * @return
	 * @throws InvalidKeyException
	 * @throws IllegalBlockSizeException
	 * @throws BadPaddingException
	 */
	public byte[] Decryptor(byte[] buff) throws InvalidKeyException,
			IllegalBlockSizeException, BadPaddingException {
		//     , Cipher       ,DECRYPT_MODE      
		c.init(Cipher.DECRYPT_MODE, deskey);
		cipherByte = c.doFinal(buff);
		return cipherByte;
	}
	
	public String decryptor(String str) throws InvalidKeyException, IllegalBlockSizeException, BadPaddingException{
		return new String(Decryptor(parseHexStr2Byte(str)));
		
	}
	
	
	
	/**       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; 
    } 
	    
	
	

	/**
	 * @param args
	 * @throws NoSuchPaddingException 
	 * @throws NoSuchAlgorithmException 
	 * @throws BadPaddingException 
	 * @throws IllegalBlockSizeException 
	 * @throws InvalidKeyException 
	 */
	public static void main(String[] args) throws Exception {
		AES de1 = new AES();
		String msg ="  ";
		byte[] encontent = de1.Encrytor(msg);
		byte[] decontent = de1.Decryptor(encontent);
		System.out.println("   :" + msg);
		System.out.println("   :" + new String(parseByte2HexStr(encontent)));
		System.out.println("   :" + new String(decontent));
		
		System.out.println("------------");
		
		System.out.println("   :" + msg);
		String e = de1.encrytor(msg);
		System.out.println("   :" + e);
		String d = de1.decryptor(e);
		System.out.println("   :" + d);
	}

}

좋은 웹페이지 즐겨찾기