Package com.nimbusds.jose.crypto
Class AESEncrypter
- java.lang.Object
-
- com.nimbusds.jose.crypto.impl.AESCryptoProvider
-
- com.nimbusds.jose.crypto.AESEncrypter
-
- All Implemented Interfaces:
JCAAware<JWEJCAContext>,JOSEProvider,JWEEncrypter,JWEProvider
@ThreadSafe public class AESEncrypter extends AESCryptoProvider implements JWEEncrypter
AES and AES GCM key wrap encrypter ofJWE objects. Expects an AES key.Encrypts the plain text with a generated AES key (the Content Encryption Key) according to the specified JOSE encryption method, then wraps the CEK with the specified AES key and returns it alongside the IV, cipher text and authentication tag. See RFC 7518, sections 4.4 and 4.7 for more information.
This class is thread-safe.
Supports the following key management algorithms:
JWEAlgorithm.A128KWJWEAlgorithm.A192KWJWEAlgorithm.A256KWJWEAlgorithm.A128GCMKWJWEAlgorithm.A192GCMKWJWEAlgorithm.A256GCMKW
Supports the following content encryption algorithms:
- Version:
- 2023-09-10
- Author:
- Melisa Halsband, Vladimir Dzhuvinov, Dimitar A. Stoikov, Egor Puzanov
-
-
Field Summary
-
Fields inherited from class com.nimbusds.jose.crypto.impl.AESCryptoProvider
COMPATIBLE_ALGORITHMS, SUPPORTED_ALGORITHMS, SUPPORTED_ENCRYPTION_METHODS
-
-
Constructor Summary
Constructors Constructor Description AESEncrypter(byte[] keyBytes)Creates a new AES encrypter.AESEncrypter(OctetSequenceKey octJWK)Creates a new AES encrypter.AESEncrypter(SecretKey kek)Creates a new AES encrypter.AESEncrypter(SecretKey kek, SecretKey contentEncryptionKey)Creates a new AES encrypter.
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description JWECryptoPartsencrypt(JWEHeader header, byte[] clearText)Deprecated.JWECryptoPartsencrypt(JWEHeader header, byte[] clearText, byte[] aad)Encrypts the specified clear text of aJWE object.protected SecretKeygetCEK(EncryptionMethod enc)Returns the content encryption key (CEK) to use.JWEJCAContextgetJCAContext()Returns the Java Cryptography Architecture (JCA) context.protected booleanisCEKProvided()Returnstrueif a content encryption key (CEK) was provided at construction time.Set<EncryptionMethod>supportedEncryptionMethods()Returns the names of the supported encryption methods by the JWE provier.Set<JWEAlgorithm>supportedJWEAlgorithms()Returns the names of the supported algorithms by the JWE provider instance.-
Methods inherited from class com.nimbusds.jose.crypto.impl.AESCryptoProvider
getKey
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface com.nimbusds.jose.jca.JCAAware
getJCAContext
-
Methods inherited from interface com.nimbusds.jose.JWEProvider
supportedEncryptionMethods, supportedJWEAlgorithms
-
-
-
-
Constructor Detail
-
AESEncrypter
public AESEncrypter(SecretKey kek, SecretKey contentEncryptionKey) throws KeyLengthException
Creates a new AES encrypter.- Parameters:
kek- The Key Encryption Key. Must be 128 bits (16 bytes), 192 bits (24 bytes) or 256 bits (32 bytes). Must not benull.contentEncryptionKey- The content encryption key (CEK) to use. If specified its algorithm must be "AES" or "ChaCha20" and its length must match the expected for the JWE encryption method ("enc"). Ifnulla CEK will be generated for each JWE.- Throws:
KeyLengthException- If the KEK length is invalid.
-
AESEncrypter
public AESEncrypter(SecretKey kek) throws KeyLengthException
Creates a new AES encrypter.- Parameters:
kek- The Key Encryption Key. Must be 128 bits (16 bytes), 192 bits (24 bytes) or 256 bits (32 bytes). Must not benull.- Throws:
KeyLengthException- If the KEK length is invalid.
-
AESEncrypter
public AESEncrypter(byte[] keyBytes) throws KeyLengthException
Creates a new AES encrypter.- Parameters:
keyBytes- The Key Encryption Key, as a byte array. Must be 128 bits (16 bytes), 192 bits (24 bytes) or 256 bits (32 bytes). Must not benull.- Throws:
KeyLengthException- If the KEK length is invalid.
-
AESEncrypter
public AESEncrypter(OctetSequenceKey octJWK) throws KeyLengthException
Creates a new AES encrypter.- Parameters:
octJWK- The Key Encryption Key, as a JWK. Must be 128 bits (16 bytes), 192 bits (24 bytes), 256 bits (32 bytes), 384 bits (48 bytes) or 512 bits (64 bytes) long. Must not benull.- Throws:
KeyLengthException- If the KEK length is invalid.
-
-
Method Detail
-
encrypt
@Deprecated public JWECryptoParts encrypt(JWEHeader header, byte[] clearText) throws JOSEException
Deprecated.Encrypts the specified clear text of aJWE object.- Parameters:
header- The JSON Web Encryption (JWE) header. Must specify a supported JWE algorithm and method. Must not benull.clearText- The clear text to encrypt. Must not benull.- Returns:
- The resulting JWE crypto parts.
- Throws:
JOSEException- If the JWE algorithm or method is not supported or if encryption failed for some other internal reason.
-
encrypt
public JWECryptoParts encrypt(JWEHeader header, byte[] clearText, byte[] aad) throws JOSEException
Description copied from interface:JWEEncrypterEncrypts the specified clear text of aJWE object.- Specified by:
encryptin interfaceJWEEncrypter- Parameters:
header- The JSON Web Encryption (JWE) header. Must specify a supported JWE algorithm and method. Must not benull.clearText- The clear text to encrypt. Must not benull.aad- The additional authenticated data. Must not benull.- Returns:
- The resulting JWE crypto parts.
- Throws:
JOSEException- If the JWE algorithm or method is not supported or if encryption failed for some other internal reason.
-
supportedJWEAlgorithms
public Set<JWEAlgorithm> supportedJWEAlgorithms()
Description copied from interface:JWEProviderReturns the names of the supported algorithms by the JWE provider instance. These correspond to thealgJWE header parameter.- Specified by:
supportedJWEAlgorithmsin interfaceJWEProvider- Returns:
- The supported JWE algorithms, empty set if none.
-
supportedEncryptionMethods
public Set<EncryptionMethod> supportedEncryptionMethods()
Description copied from interface:JWEProviderReturns the names of the supported encryption methods by the JWE provier. These correspond to theencJWE header parameter.- Specified by:
supportedEncryptionMethodsin interfaceJWEProvider- Returns:
- The supported encryption methods, empty set if none.
-
getJCAContext
public JWEJCAContext getJCAContext()
Description copied from interface:JCAAwareReturns the Java Cryptography Architecture (JCA) context. May be used to set a specific JCA security provider or secure random generator.- Specified by:
getJCAContextin interfaceJCAAware<JWEJCAContext>- Returns:
- The JCA context. Not
null.
-
isCEKProvided
protected boolean isCEKProvided()
Returnstrueif a content encryption key (CEK) was provided at construction time.- Returns:
trueif a CEK was provided at construction time,falseif CEKs will be internally generated.
-
getCEK
protected SecretKey getCEK(EncryptionMethod enc) throws JOSEException
Returns the content encryption key (CEK) to use. Unless a CEK was provided at construction time this will be a new internally generated CEK.- Parameters:
enc- The encryption method. Must not benull.- Returns:
- The content encryption key (CEK).
- Throws:
JOSEException- If an internal exception is encountered.
-
-