Package org.confng.encryption
Class AesEncryptionProvider
- java.lang.Object
-
- org.confng.encryption.AesEncryptionProvider
-
- All Implemented Interfaces:
EncryptionProvider
public class AesEncryptionProvider extends Object implements EncryptionProvider
AES-256-GCM encryption provider for secure configuration values.This provider uses AES-256 in GCM mode, which provides both confidentiality and authenticity. The IV is prepended to the ciphertext for storage.
Example Usage:
// Create provider with a 256-bit key byte[] keyBytes = ... // 32 bytes for AES-256 AesEncryptionProvider provider = new AesEncryptionProvider(keyBytes); // Or from Base64 encoded key AesEncryptionProvider provider = AesEncryptionProvider.fromBase64Key("base64EncodedKey"); // Or from environment variable AesEncryptionProvider provider = AesEncryptionProvider.fromEnvironment(); // Register with ConfNG ConfNG.setEncryptionProvider(provider);- Since:
- 1.1.0
- Author:
- Bharat Kumar Malviya, GitHub: github.com/imBharatMalviya
-
-
Constructor Summary
Constructors Constructor Description AesEncryptionProvider(byte[] keyBytes)Creates a new AES encryption provider with the given key bytes.AesEncryptionProvider(SecretKey secretKey)Creates a new AES encryption provider with the given SecretKey.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description Stringdecrypt(String encryptedValue)Decrypts an encrypted value.Stringencrypt(String plainValue)Encrypts a plain text value.static AesEncryptionProviderfromBase64Key(String base64Key)Creates an AES encryption provider from a Base64-encoded key.static AesEncryptionProviderfromEnvironment()Creates an AES encryption provider from the CONFNG_ENCRYPTION_KEY environment variable.static AesEncryptionProviderfromProperty(String propertyName)Creates an AES encryption provider from a system property or environment variable.StringgetName()Gets the name of this encryption provider.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.confng.encryption.EncryptionProvider
extractPayload, isEncrypted
-
-
-
-
Constructor Detail
-
AesEncryptionProvider
public AesEncryptionProvider(byte[] keyBytes)
Creates a new AES encryption provider with the given key bytes.- Parameters:
keyBytes- the AES key (must be 16, 24, or 32 bytes for AES-128/192/256)
-
AesEncryptionProvider
public AesEncryptionProvider(SecretKey secretKey)
Creates a new AES encryption provider with the given SecretKey.- Parameters:
secretKey- the AES secret key
-
-
Method Detail
-
fromBase64Key
public static AesEncryptionProvider fromBase64Key(String base64Key)
Creates an AES encryption provider from a Base64-encoded key.- Parameters:
base64Key- the Base64-encoded key- Returns:
- the encryption provider
-
fromEnvironment
public static AesEncryptionProvider fromEnvironment()
Creates an AES encryption provider from the CONFNG_ENCRYPTION_KEY environment variable.- Returns:
- the encryption provider
- Throws:
EncryptionException- if the environment variable is not set
-
fromProperty
public static AesEncryptionProvider fromProperty(String propertyName)
Creates an AES encryption provider from a system property or environment variable.- Parameters:
propertyName- the system property or environment variable name- Returns:
- the encryption provider
-
decrypt
public String decrypt(String encryptedValue) throws EncryptionException
Description copied from interface:EncryptionProviderDecrypts an encrypted value.- Specified by:
decryptin interfaceEncryptionProvider- Parameters:
encryptedValue- the encrypted value (typically Base64 encoded)- Returns:
- the decrypted plain text value
- Throws:
EncryptionException- if decryption fails
-
encrypt
public String encrypt(String plainValue) throws EncryptionException
Description copied from interface:EncryptionProviderEncrypts a plain text value.- Specified by:
encryptin interfaceEncryptionProvider- Parameters:
plainValue- the plain text value to encrypt- Returns:
- the encrypted value (typically Base64 encoded)
- Throws:
EncryptionException- if encryption fails
-
getName
public String getName()
Description copied from interface:EncryptionProviderGets the name of this encryption provider.- Specified by:
getNamein interfaceEncryptionProvider- Returns:
- the provider name
-
-