@Override
public ContentEncryptionKeys manageForEncrypt(Key managementKey, ContentEncryptionKeyDescriptor cekDesc, Headers headers, byte[] cekOverride) throws JoseException
{
byte[] cek = (cekOverride == null) ? ByteUtil.randomBytes(cekDesc.getContentEncryptionKeyByteLength()) : cekOverride;
Base64Url base64Url = new Base64Url();
String encodedIv = headers.getStringHeaderValue(HeaderParameterNames.INITIALIZATION_VECTOR);
byte[] iv;
if (encodedIv == null)
{
iv = ByteUtil.randomBytes(IV_BYTE_LENGTH);
encodedIv = base64Url.base64UrlEncode(iv);
headers.setStringHeaderValue(HeaderParameterNames.INITIALIZATION_VECTOR, encodedIv);
}
else
{
iv = base64Url.base64UrlDecode(encodedIv);
}
SimpleAeadCipher.CipherOutput encrypted = simpleAeadCipher.encrypt(managementKey, iv, cek, null);
byte[] encryptedKey = encrypted.getCiphertext();
byte[] tag = encrypted.getTag();
String encodedTag = base64Url.base64UrlEncode(tag);
headers.setStringHeaderValue(HeaderParameterNames.AUTHENTICATION_TAG, encodedTag);
return new ContentEncryptionKeys(cek, encryptedKey);
}