public static Document encryptAES( Key symmetricKey, Key keyEncryptionKey, Document document, Element elementToEncrypt, boolean encryptContentsOnly) throws Exception { org.apache.xml.security.Init.init(); // TODO: make singleton and do this only one time. // initialize cipher // XMLCipher keyCipher = XMLCipher.getInstance(XMLCipher.RSA_v1dot5); XMLCipher keyCipher = XMLCipher.getInstance(XMLCipher.RSA_OAEP); keyCipher.init(XMLCipher.WRAP_MODE, keyEncryptionKey); // encrypt symmetric key System.out.println("sym key: " + symmetricKey); EncryptedKey encryptedKey = keyCipher.encryptKey(document, symmetricKey); // xml XMLCipher xmlCipher = XMLCipher.getInstance(XMLCipher.AES_128); xmlCipher.init(XMLCipher.ENCRYPT_MODE, symmetricKey); // add key info to encrypted data element EncryptedData encryptedDataElement = xmlCipher.getEncryptedData(); KeyInfo keyInfo = new KeyInfo(document); keyInfo.add(encryptedKey); encryptedDataElement.setKeyInfo(keyInfo); // do the actual encryption document = xmlCipher.doFinal(document, elementToEncrypt, encryptContentsOnly); return document; }