AES JAVA平台版

来源:互联网 发布:智睿高清网络电视安卓 编辑:程序博客网 时间:2024/06/08 14:16

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


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 blocke.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 blocke.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 blocke.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 blocke.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/CTRprivate 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 surportprivate 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);}}


0 0
原创粉丝点击