java encryption and decryption algorithm --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 {
            // specified algorithm secret key generator
            KeyGenerator kg = KeyGenerator.getInstance("aes");
            // initialize the key generation device, it has to determine the size of the secret key
            kg.init(128);
            // generated keys
            SecretKey secretkey = kg.generateKey();
            return secretkey.getEncoded();
        }

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

            /**
             * Encryption Cipher practice
             */
            // create Cipher object
            Cipher cipher = Cipher.getInstance("aes/ecb/PKCS5Padding");
            // initialize the Cipher
            cipher.init(Cipher.ENCRYPT_MODE,keySpec);
            byte[] data = src.getBytes();
            //encryption
            byte[] encryptedData = cipher.doFinal(data);
            return encryptedData;
        }

        public static void main(String[] args) throws NoSuchAlgorithmException, IllegalBlockSizeException, NoSuchPaddingException, BadPaddingException, InvalidKeySpecException, InvalidKeyException, IOException {
            //byte[] secretKey = initSecretKey();
            // code or key agreement
            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 {
            // generated keys
            SecretKeySpec keySpec = new SecretKeySpec(key, "aes");

            /**
             * Cipher decryption practice
             */
            // create Cipher object
            Cipher cipher = Cipher.getInstance("aes/ecb/PKCS5Padding");
            // initialize the Cipher
            cipher.init(Cipher.DECRYPT_MODE,keySpec);
            //encryption
            byte[] dencryptedData = cipher.doFinal(encryptedData);
            return new String(dencryptedData);
        }

}
  • CBC 

And ECB difference, and with the length of the secret key encryption manner to be filled using an initialization vector, and the same initialization vector. CFB can refer the following example.

  •  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 {
            // specified algorithm secret key generator
            KeyGenerator kg = KeyGenerator.getInstance("aes");
            // initialize the key generation device, it has to determine the size of the secret key
            kg.init(128);
            // generated keys
            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 {
            // generated keys
            SecretKeySpec keySpec = new SecretKeySpec(key, "aes");

            /**
             * Encryption Cipher practice
             */
            // create Cipher object
            Cipher cipher = Cipher.getInstance("aes/cfb/PKCS5Padding");
            // Create an initialization vector
            IvParameterSpec iv = new IvParameterSpec(keyIv);
            // initialize the Cipher
            cipher.init(Cipher.ENCRYPT_MODE,keySpec,iv);
            byte[] data = src.getBytes();
            //encryption
            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();
            // code or key agreement
            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 {
            // generated keys
            SecretKeySpec keySpec = new SecretKeySpec(key, "aes");

            /**
             * Cipher decryption practice
             */
            // create Cipher object
            Cipher cipher = Cipher.getInstance("aes/cfb/PKCS5Padding");
            // Create an initialization vector
            IvParameterSpec iv = new IvParameterSpec(keyIv);
            // initialize the Cipher
            cipher.init(Cipher.DECRYPT_MODE,keySpec,iv);
            //encryption
            byte[] dencryptedData = cipher.doFinal(encryptedData);
            return new String(dencryptedData);
        }

}

  

PS: 128 into 192, or 256 as long as the length of the key into the 1624, or like 32

AES filled with a combination of various modes and AES encryption and decryption as well as online tools: https://www.jianshu.com/p/e8969d8bb6d7  

Guess you like

Origin www.cnblogs.com/ivy-xu/p/12297507.html