AES JAVA平台版

废话不多说了,请看代码把。


package base_crypt;

import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class base_crypt {

	/*********************************************
	 * ecb_base_encrypt("AES/ECB/PKCS5Padding","AES",s_buf,p_pass);
	 * @throws NoSuchPaddingException 
	 * @throws NoSuchAlgorithmException 
	 * @throws InvalidKeyException 
	 * @throws BadPaddingException 
	 * @throws IllegalBlockSizeException 
	 *********************************************/
	protected static byte[] ecb_base_encrypt(String p_formation,String p_algorithm,byte[] s_buf,byte[] p_pass) {
		try{
			Cipher cipher = Cipher.getInstance(p_formation);
			SecretKeySpec keyspec = new SecretKeySpec(p_pass, p_algorithm);
			cipher.init(Cipher.ENCRYPT_MODE,keyspec);
			byte[] d_buf = cipher.doFinal(s_buf);
			return d_buf;
		} catch (InvalidKeyException | NoSuchAlgorithmException
				| NoSuchPaddingException | IllegalBlockSizeException
				| BadPaddingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	/*********************************************
	 * ecb_base_decrypt("AES/ECB/PKCS5Padding","AES",s_buf,p_pass);
	 * @throws NoSuchPaddingException 
	 * @throws NoSuchAlgorithmException 
	 * @throws InvalidKeyException 
	 * @throws BadPaddingException 
	 * @throws IllegalBlockSizeException 
	 *********************************************/
	protected static byte[] ecb_base_decrypt(String p_formation,String p_algorithm,byte[] s_buf,byte[] p_pass) {
		
		try{
			Cipher cipher = Cipher.getInstance(p_formation);
			SecretKeySpec keyspec = new SecretKeySpec(p_pass, p_algorithm);
			cipher.init(Cipher.DECRYPT_MODE,keyspec);
			byte[] d_buf = cipher.doFinal(s_buf);
			return d_buf;
		} catch (InvalidKeyException | NoSuchAlgorithmException
				| NoSuchPaddingException | IllegalBlockSizeException
				| BadPaddingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	/*********************************************
	 * base_encrypt("AES/CBC/PKCS5Padding","AES",s_buf,p_pass,p_iv);
	 * @throws NoSuchPaddingException 
	 * @throws NoSuchAlgorithmException 
	 * @throws InvalidKeyException 
	 * @throws BadPaddingException 
	 * @throws IllegalBlockSizeException 
	 *********************************************/
	protected static byte[] base_encrypt(String p_formation,String p_algorithm,byte[] s_buf,byte[] p_pass,byte[] p_iv) {
		
		try{
			Cipher cipher = Cipher.getInstance(p_formation);
			SecretKeySpec keyspec = new SecretKeySpec(p_pass, p_algorithm);
			IvParameterSpec iv = new IvParameterSpec(p_iv);
			cipher.init(Cipher.ENCRYPT_MODE,keyspec,iv);
			byte[] d_buf = cipher.doFinal(s_buf);
			return d_buf;
		} catch (InvalidKeyException | NoSuchAlgorithmException
				| NoSuchPaddingException | IllegalBlockSizeException
				| BadPaddingException | InvalidAlgorithmParameterException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	/*********************************************
	 * base_decrypt("AES/CBC/PKCS5Padding","AES",s_buf,p_pass,p_iv);
	 * @throws NoSuchPaddingException 
	 * @throws NoSuchAlgorithmException 
	 * @throws InvalidKeyException 
	 * @throws BadPaddingException 
	 * @throws IllegalBlockSizeException 
	 *********************************************/
	protected static byte[] base_decrypt(String p_formation,String p_algorithm,byte[] s_buf,byte[] p_pass,byte[] p_iv){
		try{
			Cipher cipher = Cipher.getInstance(p_formation);
			SecretKeySpec keyspec = new SecretKeySpec(p_pass, p_algorithm);
			IvParameterSpec iv = new IvParameterSpec(p_iv);
			cipher.init(Cipher.DECRYPT_MODE,keyspec,iv);
			byte[] d_buf = cipher.doFinal(s_buf);
			return d_buf;
		} catch (InvalidKeyException | NoSuchAlgorithmException
				| NoSuchPaddingException | IllegalBlockSizeException
				| BadPaddingException | InvalidAlgorithmParameterException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
}


package base_crypt;

public class base_aes extends base_crypt{
	
	/* this class just provide the interface that crypt algorithm which paddinged with PKCS5Padding.*/
	
	private static String aes_mode_ecb_pkcs5 = "AES/ECB/PKCS5Padding";	// mode ECB/CBC/OFB/CFB/CTR
	private static String aes_mode_cbc_pkcs5 = "AES/CBC/PKCS5Padding";
	//private static String aes_mode_cfb_pkcs5 = "AES/CFB/PKCS5Padding";	// java jdk is not surport
	//private static String aes_mode_ofb_pkcs5 = "AES/OFB/PKCS5Padding";	// java jdk is not surport
	//private static String aes_mode_ctr_pkcs5 = "AES/CTR/PKCS5Padding";	// java jdk is not surport
	
	private static String aes_algorithm = "AES";
	
	private static String aes_key_padding_str = "1234567890ABCDEF";
	private static String aes_iv_padding_str = "ABCDEFGH12345678";
	
	private static byte[] default_iv = { 0x12, 0x34, 0x56, 0x78, 
		(byte) Integer.parseInt("90", 16),
		(byte) Integer.parseInt("AB", 16),
		(byte) Integer.parseInt("CD", 16),
		(byte) Integer.parseInt("EF", 16),
		 								 0x12, 0x34, 0x56, 0x78, 
		(byte) Integer.parseInt("90", 16),
		(byte) Integer.parseInt("AB", 16),
		(byte) Integer.parseInt("CD", 16),
		(byte) Integer.parseInt("EF", 16)};
	
	private static byte[] key_generator(String p_key){
		byte[] key = (p_key +aes_key_padding_str).substring(0, 16).getBytes();
		return key;
	}
	private static byte[] iv_generator(String p_iv){
		byte[] iv = (p_iv + aes_iv_padding_str).substring(0,16).getBytes();
		return iv;
	}
	
	public static byte[] ecb_aes_encrypt(byte[] s_buf,byte[] p_pass){
		return ecb_base_encrypt(aes_mode_ecb_pkcs5, aes_algorithm,s_buf, p_pass);
	}
	public static byte[] ecb_aes_decrypt(byte[] s_buf,byte[] p_pass){
		return ecb_base_decrypt(aes_mode_ecb_pkcs5, aes_algorithm, s_buf, p_pass);
	}
	public static byte[] ecb_aes_encrypt(byte[] s_buf,String p_pass) {
		byte[] p_key = key_generator(p_pass);
		return ecb_base_encrypt(aes_mode_ecb_pkcs5, aes_algorithm, s_buf, p_key);
	}
	public static byte[] ecb_aes_decrypt(byte[] s_buf,String p_pass){
		
		byte[] p_key = key_generator(p_pass);
		return ecb_base_decrypt(aes_mode_ecb_pkcs5, aes_algorithm, s_buf, p_key);
	}
	
	
	public static byte[] aes_cbc_encrypt(byte[] s_buf,byte[] p_pass,byte[] p_iv){
		return base_encrypt(aes_mode_cbc_pkcs5, aes_algorithm, s_buf, p_pass, p_iv);
	}
	public static byte[] aes_cbc_decrypt(byte[] s_buf,byte[] p_pass,byte[] p_iv) {
		return base_decrypt(aes_mode_cbc_pkcs5, aes_algorithm, s_buf, p_pass, p_iv);
	}
	public static byte[] aes_cbc_encrypt(byte[] s_buf,byte[] p_pass){
		return base_encrypt(aes_mode_cbc_pkcs5, aes_algorithm, s_buf, p_pass, default_iv);
	}
	public static byte[] aes_cbc_decrypt(byte[] s_buf,byte[] p_pass){
		return base_decrypt(aes_mode_cbc_pkcs5, aes_algorithm, s_buf, p_pass, default_iv);
	}
	public static byte[] aes_cbc_encrypt(byte[] s_buf,String p_pass,String p_iv){
		byte[] key = key_generator(p_pass);
		byte[] iv = iv_generator(p_iv);
		return base_encrypt(aes_mode_cbc_pkcs5, aes_algorithm, s_buf, key, iv);
	}
	public static byte[] aes_cbc_decrypt(byte[] s_buf,String p_pass,String p_iv){
		byte[] key = key_generator(p_pass);
		byte[] iv = iv_generator(p_iv);
		return base_decrypt(aes_mode_cbc_pkcs5, aes_algorithm, s_buf, key, iv);
	}
	public static byte[] aes_cbc_encrypt(byte[] s_buf,String p_pass){
		byte[] p_key = key_generator(p_pass);
		return base_encrypt(aes_mode_cbc_pkcs5, aes_algorithm, s_buf, p_key,default_iv);
	}
	public static byte[] aes_cbc_decrypt(byte[] s_buf,String p_pass){
		byte[] p_key = key_generator(p_pass);
		return base_decrypt(aes_mode_cbc_pkcs5, aes_algorithm, s_buf, p_key,default_iv);
	}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值