CryptoUtils.java
package model.security;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.GCMParameterSpec;
import java.nio.charset.StandardCharsets;
import java.security.GeneralSecurityException;
import java.security.SecureRandom;
import java.util.Base64;
public class CryptoUtils {
private static final SecureRandom random = new SecureRandom();
private static final int IV_LENGTH = 12;
private static final int TAG_LENGTH = 128;
private CryptoUtils() {
// static class
}
public static String encrypt(SecretKey key, String plaintext) throws GeneralSecurityException {
byte[] iv = new byte[IV_LENGTH];
random.nextBytes(iv);
Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
cipher.init(Cipher.ENCRYPT_MODE, key, new GCMParameterSpec(TAG_LENGTH, iv));
byte[] ciphertext = cipher.doFinal(plaintext.getBytes(StandardCharsets.UTF_8));
byte[] result = new byte[iv.length + ciphertext.length];
System.arraycopy(iv, 0, result, 0, iv.length);
System.arraycopy(ciphertext, 0, result, iv.length, ciphertext.length);
return Base64.getEncoder().encodeToString(result);
}
public static String decrypt(SecretKey key, String encoded) throws GeneralSecurityException {
byte[] input = Base64.getDecoder().decode(encoded);
byte[] iv = new byte[IV_LENGTH];
System.arraycopy(input, 0, iv, 0, IV_LENGTH);
byte[] ciphertext = new byte[input.length - IV_LENGTH];
System.arraycopy(input, IV_LENGTH, ciphertext, 0, ciphertext.length);
Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
cipher.init(Cipher.DECRYPT_MODE, key, new GCMParameterSpec(TAG_LENGTH, iv));
byte[] plain = cipher.doFinal(ciphertext);
return new String(plain, StandardCharsets.UTF_8);
}
}