Java AES Advanced Encryption Standard Encryption and Decryption

Tags: Crypto AES

Java Code Examples for javax.crypto.Cipher.doFinal()

Java AES Advanced Encryption Standard Encryption and Decryption.

package simplesolution.dev.crypto;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.Base64;

public class AESEncryption {

    public static void main(String... args) {
        try {
            String secretKey = "KeyToKeepSecret";
            String dataToEncrypt = "https://simplesolution.dev";
            String dataToDecrypt = "eLR6zVc8ycWr1xfixIkMQ8iL5asv9WyAhuRmz1Wf1u0=";

            System.out.println(encrypt(secretKey, dataToEncrypt));

            System.out.println(decrypt(secretKey, dataToDecrypt));
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    private static String encrypt(String secretKey, String dataToEncrypt) {
        try {
            byte[] key = secretKey.getBytes("UTF-8");
            MessageDigest sha = MessageDigest.getInstance("SHA-1");
            key = sha.digest(key);
            key = Arrays.copyOf(key, 16);
            SecretKeySpec secretKeySpec = new SecretKeySpec(key, "AES");
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
            byte[] encryptedData = cipher.doFinal(dataToEncrypt.getBytes("UTF-8"));
            String encryptedValue = Base64.getEncoder().encodeToString(encryptedData);
            return encryptedValue;
        } catch (UnsupportedEncodingException ex) {
            ex.printStackTrace();
        } catch (NoSuchAlgorithmException ex) {
            ex.printStackTrace();
        } catch (NoSuchPaddingException ex) {
            ex.printStackTrace();
        } catch (InvalidKeyException ex) {
            ex.printStackTrace();
        } catch (BadPaddingException ex) {
            ex.printStackTrace();
        } catch (IllegalBlockSizeException ex) {
            ex.printStackTrace();
        }
        return null;
    }

    private static String decrypt(String secretKey, String dataToDecrypt) {
        try {
            byte[] key = secretKey.getBytes("UTF-8");
            MessageDigest sha = MessageDigest.getInstance("SHA-1");
            key = sha.digest(key);
            key = Arrays.copyOf(key, 16);
            SecretKeySpec secretKeySpec = new SecretKeySpec(key, "AES");
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
            byte[] decryptedData = cipher.doFinal(Base64.getDecoder().decode(dataToDecrypt));
            String decryptedValue = new String(decryptedData);
            return decryptedValue;
        } catch (UnsupportedEncodingException ex) {
            ex.printStackTrace();
        } catch (NoSuchAlgorithmException ex) {
            ex.printStackTrace();
        } catch (NoSuchPaddingException ex) {
            ex.printStackTrace();
        } catch (InvalidKeyException ex) {
            ex.printStackTrace();
        } catch (BadPaddingException ex) {
            ex.printStackTrace();
        } catch (IllegalBlockSizeException ex) {
            ex.printStackTrace();
        }
        return null;
    }
}

Happy Coding 😊

Java AES 256 Encryption and Decryption

Java MD5 Hashing String

Java SHA-1 Hashing String