Exploring XML Encryption, Part 1

Demonstrating the secure exchange of structured data

Return to article.

Listing 11. XmlEncryption.java
 /*
 	DW/BS
	20020204
	XmlEncryption.java
	Listing 11
	A wrapper class that can generate complete XML encrypted file.
	It uses all the other classes. 
	Users of our XML Encryption Engine will only need to interact with 
	this class.
 */

import java.io.*;
import org.w3c.dom.*;
import javax.xml.parsers.*;
import org.apache.crimson.tree.XmlDocument;

public class XmlEncryption {
	
	// Source and Result file names.
	private String fileSource = null;
	private String fileResult = null;
	
	// Name of Algorithm which will be used to encrypt data.
	private String algoName = null;

	// Name of Secret key which was previously agreed upon 
	// and saved with the given name.
	private String keyName = null; 
	
	// Id attribute of Main structure 
	private String encId = null;

	// It will be used to get New Document Objects.
	private DocumentBuilder docBuilder = null;

	// Default Constructor
	public XmlEncryption() {
		// Create DocumentBuilder object from DocumentBuilderFactory.
		try {
			docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
		} catch (ParserConfigurationException e){ docBuilder = null; }
	}
	
	// Get the new Document object for DocumentBuilder
	private Document getNewDocument() {
		if (docBuilder != null)
			return docBuilder.newDocument();
		else
			return null;	
	}
	
	// Generate Complete XML Encrypted File.
	public void encryptCompleteXmlFile(){
		// Take an Object of EncryptedData Class. 
		// It represents EncryptedData Element.
		EncryptedData encDataObj = this.getEncryptedDataDoc(this.encId, "DOCUMENT");

		// Get XML Structure for EncryptionMehtod element.
		Document encMethodDoc = this.getEncryptionMethodDoc(this.algoName);	

		// Get XML Structure for KeyInfo element.		
		Document encKeyInfoDoc = this.getKeyInfoDoc(this.keyName);

		// Read the given file data which will be encrypted.
		String plainData = this.readFile(fileSource);

		// Use of JCA/JCE to get encrypted data.
		String cipherData = this.getEncryptedData(plainData);

		// Get XML Structure for CipherData element.				
		Document cipherDataDoc = this.getCipherDataDoc(cipherData);
		

		// Join these XML Structures.
		encDataObj.addChild(encMethodDoc);
		encDataObj.addChild(encKeyInfoDoc);
		encDataObj.addChild(cipherDataDoc);
		
		// Now Save this Document as an XML File
		this.saveEncryptedFile(this.fileResult, encDataObj.getEncData());		
	}// End encryptCompleteXmlFile()
	
	
	//********** All Set/Get methods to related fields.
	
	public void setFileSource(String file) {
		this.fileSource = file;
	}// End setFileSource()
	
	public String getFileSource() {
		return this.fileSource;
	}// End getFileSource()

	public void setFileResult(String file) {
		this.fileResult = file;
	}// End setFileResult() 
	
	public String getFileResult() {
		return this.fileResult;
	}// End getFileResult() 
	
	public void setAlgoName(String name) {
		this.algoName = name;
	}// End setAlgoName() 

	public String getAlgoName() {
		return this.algoName;
	}// End getAlgoName() 
	
	public void setKeyName (String key) {
		this.keyName = key; 
	}// End setKeyName()
	
	public String getKeyName() {
		return this.keyName; 
	}// End getKeyName()

	public void setEncId (String id) {
		this.encId = id;
	}// End setEncId()

	public String getEncId() {
		return this.encId;
	}// End setEncId()

	//**************

	// Reads the given file and returns it as string.		
	public String readFile(String fileName){
		String xml = "";
		try {
			FileInputStream in = new FileInputStream(fileName);
			byte [] data = new byte[in.available()];
			in.read(data);
			xml = new String(data);
		} catch (IOException e) { }
		return xml;
	}// End readFile()
	
	// Saves the given document as an XML (Text) file with given name.
	public void saveEncryptedFile (String fileName, Document doc) {
		XmlDocument xmlDoc = (XmlDocument)doc;
		try { 
			OutputStream out = new FileOutputStream(fileName);
			xmlDoc.write(out);
			out.close();
		} catch (IOException e) { }

	}// End saveEncryptedFile()
	
	// Returns the EncryptedData Object.
	public EncryptedData getEncryptedDataDoc(String Id, String encType) {
		EncryptedData ed = new EncryptedData(this.getNewDocument());
		ed.setId(Id);
		if (encType.equals("DOCUMENT"))
			ed.setType(AlgoNames.DOCUMENT);
	
		return ed;
	}// End getEncryptedDataDoc()

	// Returns the EncryptionMehtod Structure.
	public Document getEncryptionMethodDoc (String algoName) {
		EncryptionMethod em = new EncryptionMethod(this.getNewDocument());
		if (algoName.equals("TripleDes-cbc"))
			em.setAlgorithm(AlgoNames.TRIPLE_DES);
		return em.getEncMethod();
	}// End getEncryptionMethodDoc()

	// Returns the KeyInfo Structure.
	public Document getKeyInfoDoc (String keyName) {
		GenericKeyInfo ki = new GenericKeyInfo(this.getNewDocument(),"ds", AlgoNames.XML_DSIG);
		ki.setKeyName(keyName);
		return ki.getKeyInfo();	
	}// End getKeyInfoDoc()
	
	// Returns the CipherData Structure.
	public Document getCipherDataDoc (String data) {
		CipherData cd = new CipherData(this.getNewDocument());
		cd.setValue(data);
		return cd.getCipherData();
	}// getCipherDataDoc()
	
	// In the future, all JCA/JCE related classes will be used here.
	// It will take plain text and return its encrypted form. 
	// All necessary Infromation about keys and algos will be 
	// taken from the fields representing them. 
	// For the time being it is not doing any thing.
	public String getEncryptedData(String data) {
		return "This is Cipher Data";
	}// End getEncryptedData()
	
	// This main method is only included to demonstrate functionality.
	public static void main (String args[]) {
		XmlEncryption xmlEnc = new XmlEncryption();
		xmlEnc.setFileSource("Order.xml");
		xmlEnc.setFileResult("EncryptedOrder.xml");
		xmlEnc.setAlgoName("TripleDes-cbc");
		xmlEnc.setKeyName("ImranAli"); 
		xmlEnc.setEncId("Test");
		xmlEnc.encryptCompleteXmlFile();
	}// End main()
}// End Class DemoApplication

Return to article.