JWEAlgorithm alg = readOnlyJWEHeader.getAlgorithm();
if (! alg.equals(JWEAlgorithm.DIR)) {
throw new JOSEException("Unsupported JWE algorithm, must be \"dir\"");
}
// Check key length matches matches encryption method
EncryptionMethod enc = readOnlyJWEHeader.getEncryptionMethod();
if (enc.cekBitLength() != getKey().getEncoded().length * 8) {
throw new JOSEException("The Content Encryption Key (CEK) length must be " + enc.cekBitLength() + " bits for " + enc + " encryption");
}
final Base64URL encryptedKey = null; // The second JWE part
// Apply compression if instructed
byte[] plainText = DeflateHelper.applyCompression(readOnlyJWEHeader, bytes);
// Compose the AAD
byte[] aad = StringUtils.toByteArray(readOnlyJWEHeader.toBase64URL().toString());
// Encrypt the plain text according to the JWE enc
byte[] iv;
AuthenticatedCipherText authCipherText;
SecureRandom randomGen = getSecureRandom();
if (enc.equals(EncryptionMethod.A128CBC_HS256) || enc.equals(EncryptionMethod.A192CBC_HS384) || enc.equals(EncryptionMethod.A256CBC_HS512)) {
iv = AESCBC.generateIV(randomGen);
authCipherText = AESCBC.encryptAuthenticated(getKey(), 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(getKey(), iv, plainText, aad, contentEncryptionProvider);
} else {
throw new JOSEException("Unsupported encryption method, must be A128CBC_HS256, A192CBC_HS384, A256CBC_HS512, A128GCM, A192GCM or A128GCM");
}
return new JWECryptoParts(encryptedKey,
Base64URL.encode(iv),
Base64URL.encode(authCipherText.getCipherText()),