Below is a working class on how to encrypt and decrypt using AES in Java. Aside from using the javax.crypto package, you should also add the apache commons code dependency. You can download and import the jar file to your dependency or add it to your maven pom:
<dependency> <groupId>commons-codec</groupId> <artifactId>commons-codec</artifactId> <version>1.10</version> </dependency>
The apache commons codec Base64 methods were used to encode/decode string. The keys were hard coded to prevent anyone to get the keys and use to decrypt string.
Below is the example class of EncryptDecrypt.java. Change SECRET_KEY_1 and SECRET_KEY_2 to your own secret keys.
package com.javapointers; import org.apache.commons.codec.binary.Base64; 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; import java.io.UnsupportedEncodingException; import java.security.InvalidAlgorithmParameterException; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; public class EncryptDecrypt { private static final String SECRET_KEY_1 = "ssdkF$HUy2A#D%kd"; private static final String SECRET_KEY_2 = "weJiSEvR5yAC5ftB"; private IvParameterSpec ivParameterSpec; private SecretKeySpec secretKeySpec; private Cipher cipher; public EncryptDecrypt() throws UnsupportedEncodingException, NoSuchPaddingException, NoSuchAlgorithmException { ivParameterSpec = new IvParameterSpec(SECRET_KEY_1.getBytes("UTF-8")); secretKeySpec = new SecretKeySpec(SECRET_KEY_2.getBytes("UTF-8"), "AES"); cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING"); } /** * Encrypt the string with this internal algorithm. * * @param toBeEncrypt string object to be encrypt. * @return returns encrypted string. * @throws NoSuchPaddingException * @throws NoSuchAlgorithmException * @throws InvalidAlgorithmParameterException * @throws InvalidKeyException * @throws BadPaddingException * @throws IllegalBlockSizeException */ public String encrypt(String toBeEncrypt) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException { cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivParameterSpec); byte[] encrypted = cipher.doFinal(toBeEncrypt.getBytes()); return Base64.encodeBase64String(encrypted); } /** * Decrypt this string with the internal algorithm. The passed argument should be encrypted using * {@link #encrypt(String) encrypt} method of this class. * * @param encrypted encrypted string that was encrypted using {@link #encrypt(String) encrypt} method. * @return decrypted string. * @throws InvalidAlgorithmParameterException * @throws InvalidKeyException * @throws BadPaddingException * @throws IllegalBlockSizeException */ public String decrypt(String encrypted) throws InvalidAlgorithmParameterException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException { cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, ivParameterSpec); byte[] decryptedBytes = cipher.doFinal(Base64.decodeBase64(encrypted)); return new String(decryptedBytes); } }
Remember to always use the same keys when trying to decode to avoid getting different value from the one that was encoded. So that is how to encrypt and decrypt using AES in Java.