Version 5

    Disclaimer:  This article does not talk about the implementation of any cryptographic modules. It talks about the usage of a simple library util that uses the Apache XML Security library ( to provide XML Encryption support ( for any XML Document.


    Introduction:  When you transmit XML Documents from one end (sender) to the other end (recipient), you would like to encrypt some elements in the document such that any sniffers along the way do not see sensitive information. Things include credit card numbers, bank account information etc.   As part of the JBoss Identity project, there is an utility class called as XMLEncryptionUtil that can be used to encrypt/decrypt documents.


    The sender needs to have the Public Key of the recipient.  The sender creates a SecretKey to encrypt.  Then this SecretKey is encrypted using the Public Key of the recipient and the encrypted key is sent as part of the encryption payload in the document.  At the recieving end, the receiver uses his Private Key to decrypt the SecretKey and then decrypts the document.  As you can see, the SecretKey is safe during transport.




    Let us the usage of this util class.


    // Some imports
    import javax.crypto.KeyGenerator;
    import javax.crypto.SecretKey;
    import javax.xml.namespace.QName;
    import org.jboss.identity.federation.core.saml.v2.util.DocumentUtil;
    import org.jboss.identity.federation.api.util.XMLEncryptionUtil;
    //Define some arbitrary xml
    String myXML = "<somexml><a><b></b></a></somexml>";
    KeyPair kp = this.getKeyPair("RSA");
    SecretKey sk = this.getSecretKey();
    PublicKey publicKey = kp.getPublic();
    Document doc = DocumentUtil.getDocument(myXML);
    QName qname = new QName("urn:test", "encryptedA", "someprefix");
    QName elementAQname = new QName("a");
    XMLEncryptionUtil.encryptElement(elementAQname, doc,
                publicKey, sk, 256, qname, true);
    //Let us verify the document:
    //The original document that has been passed has been updated
    NodeList nl = doc.getElementsByTagNameNS(XMLEncryptionUtil.XMLENC_NS,
    assertTrue(nl != null && nl.getLength() == 1); 
    Node wrappedNode = doc.getDocumentElement().getFirstChild();
    assertEquals(wrappedNode.getNamespaceURI(), qname.getNamespaceURI());
    assertEquals(wrappedNode.getPrefix(), qname.getPrefix());
    //Let us decrypt the document
    PrivateKey privateKey = kp.getPrivate();
    Element decryptedElement = XMLEncryptionUtil.decryptElementInDocument(
                              doc, privateKey);
    assertEquals("a", decryptedElement.getLocalName());


    In this example, we encrypt the xml element "a"  and wrap the encrypted element using "someprefix:encryptedA" in the updated document.  The important thing to note is that the document during encryption gets updated with the encrypted element as a replacement to the original element.


    During decryption, we pass the document and the private key.  Then the document gets updated with the original element as a replacement to the encrypted element.


    XML Encrypted Document in transit:


    <?xml version="1.0" encoding="UTF-8"?>
      <someprefix:encryptedA xmlns:someprefix="urn:test">
     <ds:KeyInfo xmlns:ds="">
      <xenc:EncryptedKey xmlns:xenc="">
      <xenc:CipherValue xmlns:xenc="">



    • Please do not do any "pretty print" of the encrypted document because decryption will fail as the document could be messed up with white spaces. 
    • Additionally, the SecretKey can be decrypted only with the PrivateKey of the holder whose PublicKey was used in encrypting the key.
    • If you are running the application in a JBoss Application Server or any other container environment, you may probably get the dependencies (Apache XML Security etc) from the container.



    1. Download the JBoss Identity distribution from :
    2. Download the Apache XML Security 1.4.2 library (or better) from the Apache website or the JBoss maven repository here:
    3. Ensure that the Endorsed Jars exist (Xerces, XML APIs, Xalan etc)


    Future Roadmap:

    Even though I have shown one typical usage of xml encryption, there are opportunities to improve the XMLEncryptionUtil class further.



    1. XMLEncryptionUtil source:
    2. XMLEncryptionUtilTestCase:


    Related Information:

    JBoss Web Services with WS-Security support does have support for XML Encryption.