@Override
public JWECryptoParts encrypt(final JWEHeader header, final byte[] bytes)
throws JOSEException {
final JWEAlgorithm alg = header.getAlgorithm();
final EncryptionMethod enc = header.getEncryptionMethod();
// Generate and encrypt the CEK according to the enc method
final SecureRandom randomGen = getSecureRandom();
final SecretKey cek = AES.generateKey(enc.cekBitLength(), randomGen);
byte[] keyIV;
final AuthenticatedCipherText authCiphCEK;
AlgFamily algFamily;
Base64URL encryptedKey; // The second JWE part
if (alg.equals(JWEAlgorithm.A128KW)) {
if(kek.getEncoded().length != 16){
throw new JOSEException("The Key Encryption Key (KEK) length must be 128 bits for A128KW encryption");
}
algFamily = AlgFamily.AESKW;
} else if (alg.equals(JWEAlgorithm.A192KW)) {
if(kek.getEncoded().length != 24){
throw new JOSEException("The Key Encryption Key (KEK) length must be 192 bits for A192KW encryption");
}
algFamily = AlgFamily.AESKW;
} else if (alg.equals(JWEAlgorithm.A256KW)) {
if (kek.getEncoded().length != 32) {
throw new JOSEException("The Key Encryption Key (KEK) length must be 256 bits for A256KW encryption");
}
algFamily = AlgFamily.AESKW;
} else if (alg.equals(JWEAlgorithm.A128GCMKW)) {
if(kek.getEncoded().length != 16){
throw new JOSEException("The Key Encryption Key (KEK) length must be 128 bits for A128GCMKW encryption");
}
algFamily = AlgFamily.AESGCMKW;
} else if (alg.equals(JWEAlgorithm.A192GCMKW)) {
if(kek.getEncoded().length != 24){
throw new JOSEException("The Key Encryption Key (KEK) length must be 192 bits for A192GCMKW encryption");
}
algFamily = AlgFamily.AESGCMKW;
} else if (alg.equals(JWEAlgorithm.A256GCMKW)) {
if(kek.getEncoded().length != 32){
throw new JOSEException("The Key Encryption Key (KEK) length must be 256 bits for A256GCMKW encryption");
}
algFamily = AlgFamily.AESGCMKW;
} else {
throw new JOSEException("Unsupported JWE algorithm, must be A128KW, A192KW, A256KW, A128GCMKW, A192GCMKW orA256GCMKW");
}
// We need to work on the header
JWEHeader modifiableHeader;
switch (algFamily) {
case AESKW:
encryptedKey = Base64URL.encode(AESKW.encryptCEK(cek, kek));
modifiableHeader = header; // simply copy ref
break;
case AESGCMKW:
keyIV = AESGCM.generateIV(randomGen);
authCiphCEK = AESGCMKW.encryptCEK(cek, keyIV, kek, keyEncryptionProvider);
encryptedKey = Base64URL.encode(authCiphCEK.getCipherText());
// Add iv and tag to the header
modifiableHeader = new JWEHeader.Builder(header).
iv(Base64URL.encode(keyIV)).
authTag(Base64URL.encode(authCiphCEK.getAuthenticationTag())).
build();
break;
default:
// This should never happen
throw new JOSEException("Unsupported JWE algorithm, must be A128KW, A192KW, A256KW, A128GCMKW, A192GCMKW orA256GCMKW");
}
// Apply compression if instructed
byte[] plainText = DeflateHelper.applyCompression(modifiableHeader, bytes);
// Compose the AAD
byte[] aad = StringUtils.toByteArray(modifiableHeader.toBase64URL().toString());
// Encrypt the plain text according to the JWE enc
byte[] iv;
AuthenticatedCipherText authCipherText;
if (enc.equals(EncryptionMethod.A128CBC_HS256) ||
enc.equals(EncryptionMethod.A192CBC_HS384) ||
enc.equals(EncryptionMethod.A256CBC_HS512) ) {
iv = AESCBC.generateIV(randomGen);
authCipherText = AESCBC.encryptAuthenticated(
cek, iv, plainText, aad,
contentEncryptionProvider, macProvider);
} else if (enc.equals(EncryptionMethod.A128GCM) ||
enc.equals(EncryptionMethod.A192GCM) ||
enc.equals(EncryptionMethod.A256GCM) ) {
iv = AESGCM.generateIV(randomGen);
authCipherText = AESGCM.encrypt(
cek, iv, plainText, aad,
contentEncryptionProvider);
} else if (enc.equals(EncryptionMethod.A128CBC_HS256_DEPRECATED) ||
enc.equals(EncryptionMethod.A256CBC_HS512_DEPRECATED) ) {
iv = AESCBC.generateIV(randomGen);
authCipherText = AESCBC.encryptWithConcatKDF(
modifiableHeader, cek, encryptedKey, iv, plainText,