Class BaseCrypto

java.lang.Object
at.letto.basespringboot.security.BaseCrypto

public class BaseCrypto extends Object
  • Field Details

  • Constructor Details

    • BaseCrypto

      public BaseCrypto()
  • Method Details

    • encrypt

      public static String encrypt(String plainText, String password) throws Exception
      Encrypts a plain text string using AES encryption with a password. The method generates a random salt and IV, derives a key from the password, and returns the encrypted data as a Base64-encoded string.
      Parameters:
      plainText - the text to encrypt
      password - the password used for encryption
      Returns:
      Base64-encoded encrypted string
      Throws:
      Exception - if encryption fails
    • decrypt

      public static String decrypt(String encryptedBase64, String password) throws Exception
      Decrypts a Base64-encoded encrypted string using AES decryption with a password. The method extracts the salt and IV from the encrypted data, derives the key, and returns the decrypted plain text.
      Parameters:
      encryptedBase64 - the Base64-encoded encrypted string
      password - the password used for decryption
      Returns:
      decrypted plain text string
      Throws:
      Exception - if decryption fails
    • deriveKey

      private static SecretKey deriveKey(String password, byte[] salt) throws Exception
      Derives a secret key from the given password and salt using PBKDF2.
      Parameters:
      password - the password to derive the key from
      salt - the salt used in key derivation
      Returns:
      the derived SecretKey
      Throws:
      Exception - if key derivation fails
    • generateRandomBytes

      private static byte[] generateRandomBytes(int length)
      Generates a random byte array of the specified length.
      Parameters:
      length - the length of the byte array to generate
      Returns:
      a byte array filled with random bytes