From Gcube Wiki
Jump to: navigation, search


This library offers an easy way to encrypt and decrypt XML documents and String objects.

Design and implementation notes

The library uses a symmetric key based on the AES standard algorithm for cryptography. It does expect that such a key is available on the local classpath. Optionally, the key can be programmatically passed to the methods exposed by the Encrypters.

In addition, the resulting encrypted data are encoded in the BASE 64 schema in order to represent them in the ASCII string format.

The library builds on top of the Apache XML Security for Java library and the XML Encryption standard.

It exposes two main classes:

  • StringEncrypter for encrypting/decrypting String objects
  • XMLDocumentEncrypter for encrypting/decrypting XML Documents or Elements

Sample Usage

String Encryption

This and the following sample are taken from the exploitation the resource library does of the encryption library for protecting the AccessData content of the RuntimeResource class.

The following snippet shows how to encrypt a string:

import org.gcube.common.utils.encryption.StringEncrypter;
resource.setAccessData(StringEncrypter.getEncrypter().encrypt("my sensible data"));

After its serialization, the resource appears as follows:

<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
<Resource version="0.4.x">
		<Category>test category</Category>
		<Name>resource name</Name>
		<Description>a description</Description>
			<Name>Test platform</Name>
			<GHN UniqueID="123456789"/>
				<Endpoint EntryName="ap"></Endpoint>
			<AccessData>dtvKM4JImPLQvboHwBvKEur1tbvdnKXYB82AICLq5/c=</AccessData> <!-- here's the encrypted data -->

String Decryption

The following snippet shows how to decrypt a string:

import org.gcube.common.utils.encryption.StringEncrypter;
AccessPoint ap = new AccessPoint();
System.out.println("Access data's content: " + ap.getAccessData());

This will print the following line:

Access data's content: my sensible data

XML Document Encryption

The following snippet shows how to encrypt the content of the root element of an XML document:

import org.gcube.common.utils.encryption.XMLDocumentEncrypter;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
Document doc = createSampleDocument();
XMLDocumentEncrypter edoc = new XMLDocumentEncrypter(doc);

Here it is the XML document before the encryption:

<?xml version="1.0" encoding="UTF-8"?>
  <myns:RootElement xmlns:myns="">
     <myns:foo>Some simple text</myns:foo>

...and here it is the same document after the encryption:

<?xml version="1.0" encoding="UTF-8"?>
 <myns:RootElement xmlns:myns="">
   <xenc:EncryptedData xmlns:xenc="" Type="">  
     <xenc:EncryptionMethod Algorithm=""/>

XML Document Decryption

The following code shows how to decrypt the root Element of the Document encrypted in the previous example:

import org.gcube.common.utils.encryption.XMLDocumentEncrypter;
Document doc = XMLDocumentUtils.deserialize(encDoc);
System.out.println("The document after the decryption:");

This will print out:

<?xml version="1.0" encoding="UTF-8"?>
  <myns:RootElement xmlns:myns="">
    <myns:foo>Some simple text</myns:foo>