Skip to content

Instantly share code, notes, and snippets.

@jhuamanchumo
Created October 14, 2021 17:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jhuamanchumo/51f538012e003594933575693801da33 to your computer and use it in GitHub Desktop.
Save jhuamanchumo/51f538012e003594933575693801da33 to your computer and use it in GitHub Desktop.
AES Encryption - Java
package com.example.util;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.time.temporal.ChronoUnit;
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 CryptoUtil {
private static final String SECRET_KEY = "secretKey";
private static final String ALGORITHM = "AES";
private static final String VECTOR_INIT = "vectorInit";
private static final String ENCRYPT_ALGORITHM = "AES/CBC/PKCS5Padding";
private static final String DECRYPT_ALGORITHM = "AES/CBC/PKCS5Padding";
private static Logger logger = LoggerFactory.getLogger(CryptoUtil.class);
public static String decrypt(String encryptedText)
throws IllegalBlockSizeException, InvalidKeyException, InvalidAlgorithmParameterException,
NoSuchAlgorithmException, NoSuchPaddingException, UnsupportedEncodingException, BadPaddingException {
String decryptedText = null;
logger.info("[CryptoUtil] decrypt() input: {}", encryptedText);
byte[] decodedEncryptedText = Base64.decodeBase64(encryptedText.getBytes());
Cipher cipher = Cipher.getInstance(DECRYPT_ALGORITHM);
SecretKeySpec key = new SecretKeySpec(SECRET_KEY.getBytes(StandardCharsets.UTF_8.name()), ALGORITHM);
IvParameterSpec ivParameterSpec = new IvParameterSpec(VECTOR_INIT.getBytes(StandardCharsets.UTF_8.name()));
cipher.init(Cipher.DECRYPT_MODE, key, ivParameterSpec);
decryptedText = new String(cipher.doFinal(decodedEncryptedText), StandardCharsets.UTF_8.name()).trim();
logger.info("[CryptoUtil] decrypt() output: {}", decryptedText);
return decryptedText;
}
public static String encrypt(String plainText) {
String result = null;
try {
logger.info("[CryptoUtil] encrypt() input: {}", plainText);
SecretKeySpec key = new SecretKeySpec(SECRET_KEY.getBytes(StandardCharsets.UTF_8.name()), ALGORITHM);
Cipher cipher = Cipher.getInstance(ENCRYPT_ALGORITHM);
IvParameterSpec ivParameterSpec = new IvParameterSpec(VECTOR_INIT.getBytes(StandardCharsets.UTF_8.name()));
cipher.init(Cipher.ENCRYPT_MODE, key, ivParameterSpec);
byte[] resultBytes = cipher.doFinal(plainText.getBytes(StandardCharsets.UTF_8.name()));
result = Base64.encodeBase64String(resultBytes);
logger.info("[CryptoUtil] encrypt() output: {}", result);
} catch (Exception e) {
logger.error("an error occurred while encrypting: ", e);
e.printStackTrace();
}
return result;
}
public static void main(String[] args) throws InvalidKeyException, IllegalBlockSizeException, InvalidAlgorithmParameterException, NoSuchAlgorithmException, NoSuchPaddingException, UnsupportedEncodingException, BadPaddingException {
String plain = "hola";
String encrypted = encrypt(plain);
String decrypted = decrypt(encrypted);
System.out.println("plain: " + plain);
System.out.println("encrypted: " + encrypted);
System.out.println("decrypted: " + decrypted);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment