Introduction
As you know XML plays a significant role in our product or project development and from an XML document we gather much of the information. Do you know that the data you are reading from a XML document is authentic? Are you sure that the XML document has come from a trusted source? There may be many questions about the authenticity or originality of the data from an XML file. It is a common practice that whenever we encounter an XML document we directly process the XML document by parsing the document, we hardly care about the data security. But there are situations where all the points and questions made in the preceding lines are relevant. Let us consider a real-world situation. Whenever we get a letter or mail from our Post Office, how do we know that the letter has come from our friends? It is based upon his/her typical statements or words or address details. It may be his/her character signature. Now-a-days it is also possible that the letter we have received has been modified by an unknown person by adding an another page. For all those reasons we normally verify the handwriting signature of our friend. This is all about the normal Post Office letter. What about an electronic message? How can we verify that the electronic message is authentic? In this case we need to adopt the technology for digital signature. In this article, I will present a brief glimpse at XML Digital Signatures that play a significant role for our data integrity. In this article I will show you how to attach a digital signature to an XML document and how to verify the XML document with the attached signature.
Technicalities
In the last couple of years XML Digital Signatures have gained momentum, particularly in the field of the financial sector. Before we jump into XML Digital Signatures, let us consider a typical scenario for our understanding. Think about a situation where an organization sends all the salary details of its employees to the Income Tax department in the form of a XML document. How does the Income Tax department verify the XML document? It means the IT department must verify the sensitive information of all the employees of the organization. The IT department must ensure that the XML document has come from a trusted source and the document has not yet been modified while coming to the IT department. It means the document has not been tampered with in between. First of all we need to understand the concept of digital signatures. A Digital Signature is an electronic signature that validates the authenticity of the document sent by a trusted person. It also provides assurance that the original content of the document has not been modified during transmission. A digital signature can be used with any message, whether it is encrypted or not, so that the receiver can be sure of the identity of the sender and that the message has not been altered by an unknown person. According to Wikipedia, a digital signature is a mathematical scheme for demonstrating the authenticity of a digital message or document. A valid digital signature gives a recipient reason to believe that the message was created by a known sender, such that the sender cannot deny having sent the message (authentication and non-repudiation) and that the message was not altered in transit (integrity). Digital signatures are commonly used for software distribution, financial transactions, and in other cases where it is important to detect forgery or tampering.
XML Digital Signatures in Java
Let us see a complete XML document with a digital signature.
- <?xml version="1.0" encoding="UTF-8" standalone="no"?><SalaryDeposit>
- <Organisation>
- <Name>DDLab Inc</Name>
- <AccountNo>SBC-12345789</AccountNo>
- </Organisation>
- <Employees>
- <Emp>
- <Name>John Abraham</Name>
- <AccountNo>SB-001</AccountNo>
- <Amount>1234</Amount>
- </Emp>
- <Emp>
- <Name>Bipasha Basu</Name>
- <AccountNo>SB-002</AccountNo>
- <Amount>2334</Amount>
- </Emp>
- <Emp>
- <Name>Vidya Balan</Name>
- <AccountNo>SB-003</AccountNo>
- <Amount>3465</Amount>
- </Emp>
- <Emp>
- <Name>Debadatta Mishra</Name>
- <AccountNo>SB-007</AccountNo>
- <Amount>5789</Amount>
- </Emp>
- <Emp>
- <Name>Priti Zinta</Name>
- <AccountNo>SB-009</AccountNo>
- <Amount>1234</Amount>
- </Emp>
- </Employees>
- <Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
- <SignedInfo>
- <CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/>
- <SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1"/>
- <Reference URI="">
- <Transforms>
- <Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/>
- </Transforms>
- <DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/>
- <DigestValue>bHS+6uf8KbJV4AGzoHNHLfnXvKM=</DigestValue>
- </Reference>
- </SignedInfo>
- <SignatureValue>aUEMrCT5dzeOfSNaznzoT0If8WZ8KQcMNXDqtoeseonVk3NqOk9ctcxrf3QVX3wP6810DDRPdI6l
e8ccG64Ge0HjkO+aYC5+c2L/qKBzwtSbl/olJEuFU2DVxBQO+K29TTUJfxpVzC9Zf2pvT+1NRj0f - 2/ofHujYZ01D6+YqI8c=
- </SignatureValue>
- <KeyInfo>
- <KeyValue>
- <RSAKeyValue>
- <Modulus>jfAd5uV38L36+lDZJrqfH9oLN86VJezXYfAeU+lrFoHlKAXVJLAi9hKvBHQRer4tPfdez6iSBKsl
6IHkPnVRAKt0xU99uxi5QpymsWAX3qnBqHlw9Z70PwyZ+Xysfw4Q2tK2HtSgUOhMuaUcIf9sbHvf - gbvcRPgxDZZqfIzDmDU=</Modulus>
- <Exponent>AQAB</Exponent>
- </RSAKeyValue>
- </KeyValue>
- </KeyInfo>
- </Signature>
- </SalaryDeposit>
- <Signature xmlns="">
- <SignedInfo>
- <CanonicalizationMethod
- Algorithm="" />
- <SignatureMethod Algorithm="" />
- <Reference URI="">
- <Transforms>
- <Transform Algorithm="" />
- </Transforms>
- <DigestMethod Algorithm="" />
- <DigestValue></DigestValue>
- </Reference>
- </SignedInfo>
- <SignatureValue></SignatureValue>
- <KeyInfo>
- <KeyValue>
- <RSAKeyValue>
- <Modulus></Modulus>
- <Exponent></Exponent>
- </RSAKeyValue>
- </KeyValue>
- </KeyInfo>
- </Signature>
- <Signature>
- <SignedInfo></SignedInfo>
- <SignatureValue></SignatureValue>
- <KeyInfo></KeyInfo>
- </Signature>
- <SignedInfo>
- <CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/>
- <SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1"/>
- <Reference URI="">
- <Transforms>
- <Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/>
- </Transforms>
- <DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/>
- <DigestValue>bHS+6uf8KbJV4AGzoHNHLfnXvKM=</DigestValue>
- </Reference>
- </SignedInfo>
Now let us look into the XML tag <KeyInfo> structure below.
- <KeyInfo>
- <KeyValue>
- <RSAKeyValue>
- <Modulus></Modulus>
- <Exponent></Exponent>
- </RSAKeyValue>
- </KeyValue>
- </KeyInfo>
In order to create an XML Digital Signature, use the following procedure:
- Generate a pair of Keys called Private Key and Pubic Key.
- Obtain the original XML document.
- Sign the original XML document using both Private and Public key by Java API and generate another document that has a XML Digital Signature.
Let us see a brief Java code snippet for generating a XML Digital Signature.
- public void generateXMLDigitalSignature(String originalXmlFilePath,
- String destnSignedXmlFilePath, String privateKeyFilePath, String publicKeyFilePath) {
- //Get the XML Document object
- Document doc = getXmlDocument(originalXmlFilePath);
- //Create XML Signature Factory
- XMLSignatureFactory xmlSigFactory = XMLSignatureFactory.getInstance("DOM");
- PrivateKey privateKey = new KryptoUtil().getStoredPrivateKey(privateKeyFilePath);
- DOMSignContext domSignCtx = new DOMSignContext(privateKey, doc.getDocumentElement());
- Reference ref = null;
- SignedInfo signedInfo = null;
- try {
- ref = xmlSigFactory.newReference("", xmlSigFactory.newDigestMethod(DigestMethod.SHA1, null),
Collections.singletonList(xmlSigFactory.newTransform(Transform.ENVELOPED, - (TransformParameterSpec) null)), null, null);
- signedInfo = xmlSigFactory.newSignedInfo(
xmlSigFactory.newCanonicalizationMethod(CanonicalizationMethod.INCLUSIVE, - (C14NMethodParameterSpec) null),
xmlSigFactory.newSignatureMethod(SignatureMethod.RSA_SHA1, null), - Collections.singletonList(ref));
- } catch (NoSuchAlgorithmException ex) {
- ex.printStackTrace();
- } catch (InvalidAlgorithmParameterException ex) {
- ex.printStackTrace();
- }
- //Pass the Public Key File Path
- KeyInfo keyInfo = getKeyInfo(xmlSigFactory, publicKeyFilePath);
- //Create a new XML Signature
- XMLSignature xmlSignature = xmlSigFactory.newXMLSignature(signedInfo, keyInfo);
- try {
- //Sign the document
- xmlSignature.sign(domSignCtx);
- } catch (MarshalException ex) {
- ex.printStackTrace();
- } catch (XMLSignatureException ex) {
- ex.printStackTrace();
- }
- //Store the digitally signed document inta a location
- storeSignedDoc(doc, destnSignedXmlFilePath);
- }
- Obtain both signed XML document and Public key.
- Verifying the digital signature of the <SignedInfo> element
- Calculate the digest of the <SignedInfo> element and compare the values.
Let us see the brief Java code snippet of XML Digital Signature verification.
- public static boolean isXmlDigitalSignatureValid(String signedXmlFilePath,
- String pubicKeyFilePath) throws Exception {
- boolean validFlag = false;
- Document doc = getXmlDocument(signedXmlFilePath);
- NodeList nl = doc.getElementsByTagNameNS(XMLSignature.XMLNS, "Signature");
- if (nl.getLength() == 0) {
- throw new Exception("No XML Digital Signature Found, document is discarded");
- }
- PublicKey publicKey = new KryptoUtil().getStoredPublicKey(pubicKeyFilePath);
- DOMValidateContext valContext = new DOMValidateContext(publicKey, nl.item(0));
- XMLSignatureFactory fac = XMLSignatureFactory.getInstance("DOM");
- XMLSignature signature = fac.unmarshalXMLSignature(valContext);
- validFlag = signature.validate(valContext);
- return validFlag;
- }
Configuration
You can download the complete project on XML Digital Signature in Java from this site.
You can configure this project in your favorite Java IDE and run the standalone program available inside the test source folder. This project already contains both Private Key and Public Key. If you want to generate it then run the Java class "TestGenerateKeys" to generate a pair of keys. You can also provide the path of your own XML file to see how a XML Digital Signature is generated.
Conclusion
I hope you have enjoyed my article about the XML Digital Signature in Java. There are also many ways you can create a XML Digital Signature, I have provided one of the ways using the Java API. You can follow the various reference sites below. Download the complete project and go through the source code to understand the concept and its usage. Based upon the complexity and design, you can decide whether to use this concept. For any kind of issues and errors you can contact me at [email protected] .
Resource and Reference

Tin ĐangPosted Oct 13, 2021, 8:54 AM
This lecture helps me a lot. Thank you very much.
Edward CruzPosted Feb 10, 2020, 7:18 PM
Mechanism type DOM not available for android studio
baalu dPosted Mar 3, 2018, 7:14 AM
Hi Mishra.. Will u Please help us by giving method defination which are included in the above code
Nouha tarkhaniPosted Jul 5, 2017, 5:39 AM
Where's the method that calculate the digets value i'm working on dynamic project
samsul hudaPosted Dec 15, 2013, 12:48 AM
Good Job sir, I need your help. If we want to implement digSig In java with RSA algorithm we can use java.security class, How If use any algorithm, like schnorr?
i.starcevicPosted Oct 10, 2013, 6:05 PM
This dont work with Android, so can you give me some instructions about that for android if you know something..... Please, I need that...
Debadatta MishraPosted Oct 3, 2013, 11:27 AM
It is very simple Sandeep, just download the jdbc driver for MS SQL Server ie jtds.jar file. You can find a simple java connection from using MS Sql Server from internet. First you try, still if you have any problem, ping me, I will send you code. Download JTDS jar file from surceforge.net
Sandeep SharmaPosted Oct 1, 2013, 7:34 AM
Good informative article.....Sir, I need your help. Can you explain how to establish java connection with SQL Server?