Class CryptoGenerics

java.lang.Object
br.net.dd.netherwingcore.common.cryptography.CryptoGenerics

public final class CryptoGenerics extends Object
Generic cryptographic utilities, not specific to any particular use case.
  • Method Details

    • generateRandomIV

      public static byte[] generateRandomIV(int ivLenBytes)
      Generates a random IV of the specified length in bytes.
      Parameters:
      ivLenBytes - The length of the IV in bytes (e.g., 12 for AES-GCM).
      Returns:
      A byte array containing the random IV.
    • appendToBack

      public static byte[] appendToBack(byte[] data, byte[] tail)
      Appends the given tail byte array to the back of the data byte array.
      Parameters:
      data - The original byte array.
      tail - The byte array to append to the back of data.
      Returns:
      A new byte array containing data followed by tail.
    • aeEncryptWithRandomIV_AesGcm

      public static byte[] aeEncryptWithRandomIV_AesGcm(byte[] plaintext, SecretKey key, int ivLenBytes, int tagLenBytes) throws GeneralSecurityException
      Encrypts the given plaintext using AES-GCM with a random IV. The output format is: [ciphertext][IV][tag]
      Parameters:
      plaintext - The plaintext to encrypt.
      key - The secret key for encryption.
      ivLenBytes - The length of the IV in bytes (e.g., 12 for AES-GCM).
      tagLenBytes - The length of the authentication tag in bytes (e.g., 16 for 128-bit tag).
      Returns:
      A byte array containing the ciphertext followed by the IV and then the tag.
      Throws:
      GeneralSecurityException - If encryption fails or if the ciphertext is shorter than the tag.
    • aeDecrypt_AesGcm

      public static byte[] aeDecrypt_AesGcm(byte[] data, SecretKey key, int ivLenBytes, int tagLenBytes) throws GeneralSecurityException
      Decrypts the given data using AES-GCM. The input format is expected to be: [ciphertext][IV][tag]
      Parameters:
      data - The data to decrypt, containing the ciphertext followed by the IV and then the tag.
      key - The secret key for decryption.
      ivLenBytes - The length of the IV in bytes (e.g., 12 for AES-GCM).
      tagLenBytes - The length of the authentication tag in bytes (e.g., 16 for 128-bit tag).
      Returns:
      A byte array containing the decrypted plaintext.
      Throws:
      GeneralSecurityException - If decryption fails or if the input data is too short to contain the IV and tag.