java加解密算法--AES

  • ECB
import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;

public class AESDecrypt {

        public static byte[] initSecretKey() throws NoSuchAlgorithmException {
            //指定算法秘钥生成器
            KeyGenerator kg = KeyGenerator.getInstance("aes");
            //初始化秘钥生成器,使其具有确定到秘钥大小
            kg.init(128);
            //生成秘钥
            SecretKey secretkey = kg.generateKey();
            return secretkey.getEncoded();
        }

        public static byte[] encrypt(byte[] key, String src) throws InvalidKeyException, NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, BadPaddingException, IllegalBlockSizeException {
            //生成秘钥
            SecretKeySpec keySpec = new SecretKeySpec(key, "aes");

            /**
             * 加密实际操作Cipher
             */
            //创建Cipher对象
            Cipher cipher = Cipher.getInstance("aes/ecb/PKCS5Padding");
            //初始化Cipher
            cipher.init(Cipher.ENCRYPT_MODE,keySpec);
            byte[] data = src.getBytes();
            //加密
            byte[] encryptedData = cipher.doFinal(data);
            return encryptedData;
        }

        public static void main(String[] args) throws NoSuchAlgorithmException, IllegalBlockSizeException, NoSuchPaddingException, BadPaddingException, InvalidKeySpecException, InvalidKeyException, IOException {
            //byte[] secretKey = initSecretKey();
            //或者代码中约定key
            String key = "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4";
            byte[] secretKey = key.getBytes();
            String str = "abc";
            byte[] encryptedData = encrypt(secretKey, str);
            String decrypteData = decrypt(secretKey, encryptedData);
        }

        public static String decrypt(byte[] key,byte[] encryptedData) throws InvalidKeyException, NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, BadPaddingException, IllegalBlockSizeException, InvalidKeyException {
            //生成秘钥
            SecretKeySpec keySpec = new SecretKeySpec(key, "aes");

            /**
             * 解密实际操作Cipher
             */
            //创建Cipher对象
            Cipher cipher = Cipher.getInstance("aes/ecb/PKCS5Padding");
            //初始化Cipher
            cipher.init(Cipher.DECRYPT_MODE,keySpec);
            //加密
            byte[] dencryptedData = cipher.doFinal(encryptedData);
            return new String(dencryptedData);
        }

}
  • CBC 

与ECB差别,同秘钥长度和填充方式加解密都要使用初始化向量,且初始化向量一致。也可参考下面CFB例子。

  •  CFB
import javax.crypto.*;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.IOException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;

public class AESDecrypt {

        public static byte[] initSecretKey() throws NoSuchAlgorithmException {
            //指定算法秘钥生成器
            KeyGenerator kg = KeyGenerator.getInstance("aes");
            //初始化秘钥生成器,使其具有确定到秘钥大小
            kg.init(128);
            //生成秘钥
            SecretKey secretkey = kg.generateKey();
            return secretkey.getEncoded();
        }

        public static byte[] encrypt(byte[] key, String src,byte[] keyIv) throws InvalidKeyException, NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, BadPaddingException, IllegalBlockSizeException, InvalidAlgorithmParameterException {
            //生成秘钥
            SecretKeySpec keySpec = new SecretKeySpec(key, "aes");

            /**
             * 加密实际操作Cipher
             */
            //创建Cipher对象
            Cipher cipher = Cipher.getInstance("aes/cfb/PKCS5Padding");
            //创建初始化向量
            IvParameterSpec iv = new IvParameterSpec(keyIv);
            //初始化Cipher
            cipher.init(Cipher.ENCRYPT_MODE,keySpec,iv);
            byte[] data = src.getBytes();
            //加密
            byte[] encryptedData = cipher.doFinal(data);
            return encryptedData;
        }

        public static void main(String[] args) throws NoSuchAlgorithmException, IllegalBlockSizeException, NoSuchPaddingException, BadPaddingException, InvalidKeySpecException, InvalidKeyException, IOException, InvalidAlgorithmParameterException {
            //byte[] secretKey = initSecretKey();
            //或者代码中约定key
            String key = "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4";
            byte[] secretKey = key.getBytes();
            byte[] iv = {1,2,3,4,5,6,7,8,9,11,10,12,13,14,15,16};
            String str = "abc";
            byte[] encryptedData = encrypt(secretKey, str,iv);
            String decrypteData = decrypt(secretKey, encryptedData,iv);
        }

        public static String decrypt(byte[] key,byte[] encryptedData,byte[] keyIv) throws InvalidKeyException, NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, BadPaddingException, IllegalBlockSizeException, InvalidKeyException {
            //生成秘钥
            SecretKeySpec keySpec = new SecretKeySpec(key, "aes");

            /**
             * 解密实际操作Cipher
             */
            //创建Cipher对象
            Cipher cipher = Cipher.getInstance("aes/cfb/PKCS5Padding");
            //创建初始化向量
            IvParameterSpec iv = new IvParameterSpec(keyIv);
            //初始化Cipher
            cipher.init(Cipher.DECRYPT_MODE,keySpec,iv);
            //加密
            byte[] dencryptedData = cipher.doFinal(encryptedData);
            return new String(dencryptedData);
        }

}

  

PS:由128改成192,或256只要把密钥长度由16改成24,或32就好

AES各种模式和填充组合以及在线AES加解密工具:https://www.jianshu.com/p/e8969d8bb6d7  

原文地址:https://www.cnblogs.com/ivy-xu/p/12297507.html