log.error("Encryption key for key encryption was null");
throw new EncryptionException("Encryption key was null");
}
log.debug("Encrypting encryption key with algorithm: {}", encryptionAlgorithmURI);
XMLCipher xmlCipher;
try {
if (getJCAProviderName() != null) {
xmlCipher = XMLCipher.getProviderInstance(encryptionAlgorithmURI, getJCAProviderName());
} else {
xmlCipher = XMLCipher.getInstance(encryptionAlgorithmURI);
}
xmlCipher.init(XMLCipher.WRAP_MODE, encryptionKey);
} catch (XMLEncryptionException e) {
log.error("Error initializing cipher instance on key encryption", e);
throw new EncryptionException("Error initializing cipher instance on key encryption", e);
}
org.apache.xml.security.encryption.EncryptedKey apacheEncryptedKey;
try {
apacheEncryptedKey = xmlCipher.encryptKey(containingDocument, targetKey);
postProcessApacheEncryptedKey(apacheEncryptedKey, targetKey, encryptionKey,
encryptionAlgorithmURI, containingDocument);
} catch (XMLEncryptionException e) {
log.error("Error encrypting element on key encryption", e);
throw new EncryptionException("Error encrypting element on key encryption", e);
}
EncryptedKey encryptedKey;
try {
Element encKeyElement = xmlCipher.martial(containingDocument, apacheEncryptedKey);
encryptedKey = (EncryptedKey) encryptedKeyUnmarshaller.unmarshall(encKeyElement);
} catch (UnmarshallingException e) {
log.error("Error unmarshalling EncryptedKey element", e);
throw new EncryptionException("Error unmarshalling EncryptedKey element");
}