Class AesGcmBytesEncryptor
java.lang.Object
org.springframework.security.crypto.encrypt.AesGcmBytesEncryptor
- All Implemented Interfaces:
BytesEncryptor
BytesEncryptor that uses 256-bit AES/GCM/NoPadding with a random 16-byte
initialization vector and a 128-bit authentication tag. The IV is prepended to the
ciphertext on encrypt and stripped on decrypt. GCM provides authenticated encryption
(AEAD): both confidentiality and integrity are protected, and decryption throws if the
ciphertext has been tampered with.
This class uses a 16-byte (128-bit) IV rather than the 12-byte (96-bit) IV recommended by NIST SP 800-38D for GCM. Both lengths are cryptographically valid; the 16-byte choice maintains consistency with the rest of the Spring Security crypto module. For additional guidance, please see the OWASP Cryptographic Storage Cheat Sheet.
When key derivation is used via withPassword(String, CharSequence), the key is
derived using PBKDF2WithHmacSHA256 with DEFAULT_PBKDF2_ITERATIONS iterations
per the
OWASP Password Storage Cheat Sheet. Because derivation is intentionally expensive,
the encryptor instance should be created once and reused rather than constructed
per-operation.
- Since:
- 5.7.26
- See Also:
-
Nested Class Summary
Nested Classes -
Method Summary
Modifier and TypeMethodDescriptionbyte[]decrypt(byte[] encryptedBytes) Decrypt the byte array.byte[]encrypt(byte[] bytes) Encrypt the byte array.static AesGcmBytesEncryptor.BuilderwithPassword(String password, CharSequence salt) Creates an encryptor that derives its key from the given password and hex-encoded salt using PBKDF2WithHmacSHA1.static AesGcmBytesEncryptor.BuilderwithSecretKey(SecretKey secretKey) Creates an encryptor using the suppliedSecretKey.
-
Method Details
-
withPassword
Creates an encryptor that derives its key from the given password and hex-encoded salt using PBKDF2WithHmacSHA1.- Parameters:
password- the password valuesalt- the hex-encoded salt value
-
withSecretKey
Creates an encryptor using the suppliedSecretKey.- Parameters:
secretKey- the secret (symmetric) key
-
encrypt
public byte[] encrypt(byte[] bytes) Description copied from interface:BytesEncryptorEncrypt the byte array.- Specified by:
encryptin interfaceBytesEncryptor
-
decrypt
public byte[] decrypt(byte[] encryptedBytes) Description copied from interface:BytesEncryptorDecrypt the byte array.- Specified by:
decryptin interfaceBytesEncryptor
-