Skip to main content

By clicking Submit, you agree to the developerWorks terms of use.

The first time you sign into developerWorks, a profile is created for you. Select information in your profile (name, country/region, and company) is displayed to the public and will accompany any content you post. You may update your IBM account at any time.

All information submitted is secure.

  • Close [x]

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerworks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

By clicking Submit, you agree to the developerWorks terms of use.

All information submitted is secure.

  • Close [x]

Exploring XML Encryption, Part 1

Demonstrating the secure exchange of structured data

Return to article.


Listing 12. Authors the EncryptedData element


 /*
 	DW/BS
	20020204
	EncryptedData.java
	Listing 12
	Authors the EncryptedData element which is the parent of
	all XML Encryption structures except EncryptedData element.
 */

import org.w3c.dom.*;

public class EncryptedData{
	
	// Element to hold complete XML Structure.
	Element encData = null;
	Document doc = null;

	// Element will be appended in Document only once.
	private boolean elementAppendedToDoc = false;

	// Default Constructor
	public EncryptedData(Document document) {
		doc = document;
		encData = doc.createElement("EncryptedData");
		
		// Add the namespace attribute
		encData.setAttribute("xmlns","http://www.w3.org/2001/04/xmlenc#");
	}// End EncryptedData()

	// Specify the ID of this EncryptedData as there
	// can be many EncryptedData elements in the same XML file.
	public void setId(String id){
		encData.setAttribute("Id", id);
	}// End setId()
	
	// There can be three types:
	// 201 for ELEMENT
	// 202 for CONTENT
	// 303 for DOCUMENT
	public void setType(int type){
		encData.setAttribute("Type", AlgoNames.getAlgoNSValue(type));
	}// end setType()
	

	// If an Arbitrary type is to be Encrypted, specify its mimeType.
	public void setArbitraryType(String mimeType){
		String typeValue = "http://www.isi.edu/in-notes/iana/assignments/media-types/" + mimeType;
		encData.setAttribute("Type", typeValue);		
	}// End setArbitraryType()
	
	// Add any of these XML Documents KeyInfo | CipherData | Transforms | EncryptionMethod.
	public void addChild(Document document){
		NodeList nList = document.getChildNodes();

		for (int i=0 ; i<nList.getLength(); i++){
			Node tempNode = nList.item(i);
			// If it is a Root Element  
			if (tempNode.getNodeType() == Node.ELEMENT_NODE) {
				Node importedNode = (Node)doc.importNode(tempNode, 
					true /* import all childern*/);
				encData.appendChild((Element)importedNode);
				return;
			}// end if (tempNode.getNodeType() == Node.ELEMENT_NODE) 
		}// end for (int i=0 ; i<nList.getLength(); i++)
	}// End addChild()	
	
	// Return the complete XML structure as a Document Object.
	public Document getEncData(){
		if (elementAppendedToDoc == false) {
			doc.appendChild(encData);
			elementAppendedToDoc = true;
		}
		return doc;
	}// end getEncData()
}// End class EncryptedData

Return to article.