XML Encryption Examples



This page contains the example found on the W3C XML encryption website. The original example can be found here.

Consider this basic XML encoded snippet for a transaction for a fictional individual, John Smith:

<?xml version='1.0'?>
<PaymentInfo xmlns='http://example.org/paymentv2'>
     <Name>John Smith</Name>
     <CreditCard Limit='5,000' Currency='USD'>
          <Number>4019 2445 0277 5567</Number>
          <Issuer>Example Bank</Issuer>
          <Expiration>04/02</Expiration>
     </CreditCard>
</PaymentInfo>



Suppose that the recipient of this XML file was a customer database tracking program, which would require the name of the customer, but not his credit card information. It would be highly desirable to simply be able to encrypt the credit card information as follows:

<?xml version='1.0'?>
<PaymentInfo xmlns='http://example.org/paymentv2'>
     <Name>John Smith</Name>
     <EncryptedData Type='http://www.w3.org/2001/04/xmlenc#Element' xmlns='http://www.w3.org/2001/04/xmlenc#'>
          <CipherData>
               <CipherValue>A23B45C56</CipherValue>
          </CipherData>
     </EncryptedData>
</PaymentInfo>


Anything intercepting this XML snippet would only know that John Smith made some sort of transaction, but would not know with what, and especially would not know his credit card number!

What if instead of encrypting the entire credit card tag, it was desirable only to encrypt one of the child tags (just the number for example)? This could be done in this fashion:


<?xml version='1.0'?>
<PaymentInfo xmlns='http://example.org/paymentv2'>
     <Name>John Smith</Name>
     <CreditCard Limit='5,000' Currency='USD'>
          <EncryptedData xmlns='http://www.w3.org/2001/04/xmlenc#' Type='http://www.w3.org/2001/04/xmlenc#Content'>
               <CipherData>
                    <CipherValue>A23B45C56</CipherValue>
               </CipherData>
          </EncryptedData>
     </CreditCard>
</PaymentInfo>


Anyone intercepting this piece of XML would know that John Smith paid with his credit card and he has a five thousand USD limit. This makes him a prime target for telemarketers! But we digress...
What if we only wanted to encrypt just the number, and not the number tag? This is possible, as follows:


<?xml version='1.0'?>
<PaymentInfo xmlns='http://example.org/paymentv2'>
     <Name>John Smith</Name>
     <CreditCard Limit='5,000' Currency='USD'>
          <Number>
               <EncryptedData xmlns='http://www.w3.org/2001/04/xmlenc#' Type='http://www.w3.org/2001/04/xmlenc#Content'>
                    <CipherData>
                         <CipherValue>A23B45C56</CipherValue>
                    </CipherData>
               </EncryptedData>
          </Number>
          <Issuer>Example Bank</Issuer>
          <Expiration>04/02</Expiration>
     </CreditCard>
</PaymentInfo>


Now anyone intercepting this would know that John Smith paid with his credit card with a five thousand USD limit, and that the encrypted credit card number follows.
This is an example perhaps of what one might not wish to do; anyone intercepting this knows exactly where and what sensitive data is contained in the XML. XML encryption is probably better used as used above, since encoding more than just the number itself provides a lot of information hiding and thus security. As another example, note that an entire XML document can be encoded as follows:


<?xml version='1.0'?>
<EncryptedData xmlns='http://www.w3.org/2001/04/xmlenc#' MimeType='text/xml'>
     <CipherData>
          <CipherValue>A23B45C56</CipherValue>
     </CipherData>
</EncryptedData>


It is also possible to encrypt parts of a document any number of times; this is known as superencryption:


<pay:PaymentInfo xmlns:pay='http://example.org/paymentv2'>
     <EncryptedData Id='ED2' xmlns='http://www.w3.org/2001/04/xmlenc#' Type='http://www.w3.org/2001/04/xmlenc#Element'>
          <CipherData>
               <CipherValue>newEncryptedData</CipherValue>
          </CipherData>
     </EncryptedData>
</pay:PaymentInfo>


Back to main