CryptUtil

개발 자 블 로그: http://www.developsearch.com
/**
 *   、        cipher     
 *   javax.crypto.*、java.security.*、org.apache.commons.codec.binary.Base64  
 *   、    。               ,      API  ,         、  
 *   ;    、        JDK      ;
 * 
 * @author chenxin
 * @version [   , 2012-5-21]
 * @see [   /  ]
 * @since [  /    ]
 */
public class CryptUtil {

	/**
	 *       
	 */
	private static final CryptUtil instance = new CryptUtil();

	/**
	 * Base64    
	 */
	private static final BASE64Decoder base64De = new BASE64Decoder();
	
	/**
	 * Base64    
	 */
	private static final BASE64Encoder base64En = new BASE64Encoder();
	
	/**
	 *     
	 * @author ahli
	 * @version IMPV100R001DA0, Nov 18, 2009 
	 * @since CMS IMPV100R001DA0
	 */
	public enum CipherType{
		AES,
		DES
	}
	
	/**
	 *       ,        
	 */
	private CryptUtil()
	{
	};

	/**
	 *         
	 * @return
	 */
	public static CryptUtil getInstance()
	{
		return instance;
	}

	/**
	 *        
	 *   :“xjPk2rOSU1n5v70a84M+vw==”
	 * @return        
	 */
	public String genRandomKeyStr(CipherType type)
	{
		SecretKey key;
		try
		{
			key = KeyGenerator.getInstance(type.toString()).generateKey();
			return base64En.encode(key.getEncoded());
		}
		catch (NoSuchAlgorithmException e)
		{
			return null;
		}
	}

	/**
	 *     
	 * @param str        
	 * @param key        
	 * @param type         
	 * @return           
	 */
	public String encrypt(String str, String key, CipherType type)
	{
		try
		{
			Cipher cipher = Cipher.getInstance(type.toString());
			cipher.init(Cipher.ENCRYPT_MODE, initKey(type.toString(), key));
			return new String(Base64.encodeBase64(cipher.doFinal(str.getBytes("UTF-8"))));
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
		catch (GeneralSecurityException e)
		{
			e.printStackTrace();
		}
		return null;
	}

	/**
	 *   
	 * @param str       
	 * @param key       
	 * @param type        
	 * @return          
	 */
	public String decrypt(String str, String key, CipherType type)
	{
		try
		{
			Cipher cipher = Cipher.getInstance(type.toString());
			cipher.init(Cipher.DECRYPT_MODE, initKey(type.toString(), key));
			return new String(cipher.doFinal( base64De.decodeBuffer(str)), "UTF-8");
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
		catch (GeneralSecurityException e)
		{
			e.printStackTrace();
		}
		return null;
	}

	/**
	 *      
	 * @param cryptType          
	 * @param key            
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	private Key initKey(String cryptType, String key) throws UnsupportedEncodingException{
		SecretKey secretKey = new SecretKeySpec(Base64.decodeBase64(key.getBytes("UTF-8")), cryptType);
		return secretKey;
	}
	
	public static void main(String[] args)
	{
//		String randomKey = CryptUtil.getInstance().genRandomKeyStr(CipherType.AES);
		String randomKey = "C1ZZsBNd0JY/8Yx2nPEg2g==";
		System.out.println("randomKey:" + randomKey);
		String message = "cms4a";
		System.out.println("     :" + message);
		String s1 = CryptUtil.getInstance().encrypt(message, randomKey, CipherType.AES);
		System.out.println("      :" + s1);
		String s2 = CryptUtil.getInstance().decrypt(s1, randomKey, CipherType.AES);
		System.out.println("      :" + s2);
	}


}

좋은 웹페이지 즐겨찾기