Package utils

Class EncryptionUtils

java.lang.Object
utils.EncryptionUtils

public class EncryptionUtils extends Object
Utility class for encryption and decryption using AES algorithm. Provides methods for key generation, encryption, and decryption of text.
  • Constructor Details

    • EncryptionUtils

      public EncryptionUtils()
  • Method Details

    • generateKey

      public static String generateKey() throws NoSuchAlgorithmException
      Generates a new AES key and returns it as a Base64 encoded string.
      Returns:
      Base64 encoded string representation of the generated key
      Throws:
      NoSuchAlgorithmException - if the AES algorithm is not available
    • base64ToKey

      public static SecretKey base64ToKey(String base64Key)
      Converts a Base64 encoded string key to a SecretKey object.
      Parameters:
      base64Key - the Base64 encoded key string
      Returns:
      a SecretKey object for use with AES encryption/decryption
    • encrypt

      public static String encrypt(String plainText, String base64Key)
      Encrypts a plain text string using AES encryption.
      Parameters:
      plainText - the text to encrypt
      base64Key - the Base64 encoded encryption key
      Returns:
      Base64 encoded encrypted string, or null if encryption fails
    • decrypt

      public static String decrypt(String encryptedText, String base64Key)
      Decrypts an encrypted string using AES decryption.
      Parameters:
      encryptedText - the Base64 encoded encrypted text
      base64Key - the Base64 encoded decryption key
      Returns:
      the decrypted text, or null if decryption fails
    • main

      public static void main(String[] args) throws Exception
      Main method for demonstrating encryption/decryption functionality. Generates a key, encrypts a sample text, and decrypts it back.
      Parameters:
      args - command line arguments (not used)
      Throws:
      Exception - if any cryptographic operations fail