Package com.axelor.common.crypto
Class BytesEncryptorPbkdf2Sha512
java.lang.Object
com.axelor.common.crypto.BytesEncryptorPbkdf2Sha512
- All Implemented Interfaces:
Encryptor<byte[],byte[]>
AES encryptor operating on raw byte arrays using PBKDF2WithHmacSHA512 key derivation, supporting
CBC and GCM operation modes.
A fresh 16-byte salt and IV are generated for every Encryptor.encrypt(byte[]) call, ensuring
unique ciphertext even for identical plaintext with the same password.
Both CBC and GCM modes share the same binary payload layout. The IV is always randomly generated and stored in the payload regardless of the mode. The IV size differs per mode: 12 bytes for GCM (NIST SP 800-38D recommendation) and 16 bytes for CBC (AES block size):
| $AESv2$ (7 bytes) | iterations (4 bytes) | salt_size (1 byte) | iv_size (1 byte) | salt (n bytes) | iv (n bytes) | encrypted_data |
Use the factory methods to select the AES operation mode:
gcm(String)— AES/GCM/NoPadding (recommended: provides authenticated encryption)cbc(String)— AES/CBC/PKCS5Padding
The default constructor uses GCM.
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionBytesEncryptorPbkdf2Sha512(OperationMode mode, String password) Creates an encryptor for the given mode and passwordBytesEncryptorPbkdf2Sha512(OperationMode mode, String password, int iterations) Creates an encryptor for the given mode, password, and iteration count.BytesEncryptorPbkdf2Sha512(OperationMode mode, String password, int iterations, int saltSize) Creates an encryptor with a custom salt size.BytesEncryptorPbkdf2Sha512(String password) Creates a GCM encryptor with the given password. -
Method Summary
Modifier and TypeMethodDescriptionstatic BytesEncryptorPbkdf2Sha512Creates a CBC encryptor with the given password.byte[]decrypt(byte[] bytes) Decrypt the given encrypted message.byte[]encrypt(byte[] bytes) Encrypt the given message.static BytesEncryptorPbkdf2Sha512Creates a GCM encryptor with the given password.Returns the JCE transformation string (e.g.booleanisEncrypted(byte[] bytes) Check whether the given message is already encrypted or not.
-
Field Details
-
KEY_ALGORITHM
- See Also:
-
DEFAULT_ITERATIONS
public static final int DEFAULT_ITERATIONS- See Also:
-
PREFIX
- See Also:
-
DEFAULT_GCM_IV_SIZE
public static final int DEFAULT_GCM_IV_SIZE- See Also:
-
DEFAULT_CBC_IV_SIZE
public static final int DEFAULT_CBC_IV_SIZE- See Also:
-
-
Constructor Details
-
BytesEncryptorPbkdf2Sha512
Creates a GCM encryptor with the given password. -
BytesEncryptorPbkdf2Sha512
Creates an encryptor for the given mode and password -
BytesEncryptorPbkdf2Sha512
Creates an encryptor for the given mode, password, and iteration count.- Parameters:
mode- the AES operation mode (GCM or CBC)password- the password used for PBKDF2 key derivationiterations- the PBKDF2 iteration count
-
BytesEncryptorPbkdf2Sha512
public BytesEncryptorPbkdf2Sha512(OperationMode mode, String password, int iterations, int saltSize) Creates an encryptor with a custom salt size.- Parameters:
mode- the AES operation mode (GCM or CBC)password- the password used for PBKDF2 key derivationiterations- the PBKDF2 iteration countsaltSize- the salt length in bytes
-
-
Method Details
-
gcm
Creates a GCM encryptor with the given password. -
cbc
Creates a CBC encryptor with the given password. -
getTransformation
Returns the JCE transformation string (e.g.AES/GCM/NoPadding) used by this encryptor. -
isEncrypted
public boolean isEncrypted(byte[] bytes) Description copied from interface:EncryptorCheck whether the given message is already encrypted or not.- Specified by:
isEncryptedin interfaceEncryptor<byte[],byte[]> - Parameters:
bytes- the message to check- Returns:
- true if encrypted
-
encrypt
public byte[] encrypt(byte[] bytes) Description copied from interface:EncryptorEncrypt the given message. -
decrypt
public byte[] decrypt(byte[] bytes) Description copied from interface:EncryptorDecrypt the given encrypted message.
-