Package at.letto.basespringboot.security
Class BaseCrypto
java.lang.Object
at.letto.basespringboot.security.BaseCrypto
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic String
Decrypts a Base64-encoded encrypted string using AES decryption with a password.private static SecretKey
Derives a secret key from the given password and salt using PBKDF2.static String
Encrypts a plain text string using AES encryption with a password.private static byte[]
generateRandomBytes
(int length) Generates a random byte array of the specified length.
-
Field Details
-
ENCRYPTION_ALGO
- See Also:
-
KEY_DERIVATION_FUNCTION
- See Also:
-
KEY_LENGTH
private static final int KEY_LENGTH- See Also:
-
ITERATIONS
private static final int ITERATIONS- See Also:
-
SALT_LENGTH
private static final int SALT_LENGTH- See Also:
-
IV_LENGTH
private static final int IV_LENGTH- See Also:
-
-
Constructor Details
-
BaseCrypto
public BaseCrypto()
-
-
Method Details
-
encrypt
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 encryptpassword
- the password used for encryption- Returns:
- Base64-encoded encrypted string
- Throws:
Exception
- if encryption fails
-
decrypt
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 stringpassword
- the password used for decryption- Returns:
- decrypted plain text string
- Throws:
Exception
- if decryption fails
-
deriveKey
Derives a secret key from the given password and salt using PBKDF2.- Parameters:
password
- the password to derive the key fromsalt
- 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
-