Class EncryptionUtil

java.lang.Object
vaultWeb.security.EncryptionUtil

public class EncryptionUtil extends Object
Utility class for AES-GCM encryption and decryption of strings.

Provides methods to encrypt plaintext into a Base64-encoded ciphertext along with a randomly generated initialization vector (IV), and to decrypt it back to the original text. Uses AES encryption with Galois/Counter Mode (GCM) for authenticated encryption.

The constructor accepts a Base64-encoded secret key used as the master key for encryption and decryption.

Example usage:


 EncryptionUtil util = new EncryptionUtil(base64Key);
 EncryptionUtil.EncryptResult result = util.encrypt("Hello World");
 String decrypted = util.decrypt(result.cipherTextBase64, result.ivBase64);
 
  • Constructor Details

    • EncryptionUtil

      public EncryptionUtil(String base64Key)
      Creates an EncryptionUtil instance with a given Base64-encoded AES key.
      Parameters:
      base64Key - Base64-encoded secret key used for AES-GCM encryption/decryption
  • Method Details

    • encrypt

      public EncryptionUtil.EncryptResult encrypt(String plaintext) throws Exception
      Encrypts a plaintext string using AES-GCM with a randomly generated IV.
      Parameters:
      plaintext - the plaintext string to encrypt
      Returns:
      an EncryptionUtil.EncryptResult containing Base64-encoded ciphertext and IV
      Throws:
      Exception - if encryption fails
    • decrypt

      public String decrypt(String base64CipherText, String base64Iv) throws Exception
      Decrypts a Base64-encoded ciphertext using the provided Base64-encoded IV.
      Parameters:
      base64CipherText - the Base64-encoded ciphertext
      base64Iv - the Base64-encoded initialization vector used during encryption
      Returns:
      the original plaintext string
      Throws:
      Exception - if decryption fails