Create the Signature of XML file

This program can be use to create the unique signature key at the signature node in XML file. Basically this concept I have implemented to stop the interception of the XML file data. If some one change the file also they can not use this file in your application because of unique signature. I have use the SHA1 algorithm for encryption.

How this program works?

1. You can see the help for arguments required for this program.
2. To generate the signature this program reads the complete XML
file as a flat text file upto the signature node and append the
private key which is hard coded here to those content and then
use SHA1 algorith to generate the HASH key. This hash will be
a value of signature node.

Example of XML file:
<?xml version="1.0" encoding="utf-8" ?>

/* All the nodes and childs */

<key value="6gljdh&*flj$(DDK@fgjfjLFOFJFDD"/>



You can prevent the interception of XML file using this unique key so even though
intruder changed the XML file he cannot generate the unique key untill and unless he has rights
to run this application and he knows the location where you kept the destination XML file.

Complete C# Code:
using System;
using System.Text;
using System.Security;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Security.Cryptography.Xml;
using System.Xml;
using System.IO;

namespace HashPassword
    class TestApplication
        //Following is the private key
        private static string encryptionKey = "oM4jBpAw39Qoo3aSGyLiYnFqi5wYSpL2";

        static void Main(string[] args)
            if (args.Length != 2 || args[0].ToLower() != "-c")
            if( !File.Exists(args[1]) )
                System.Console.WriteLine(args[1] + " file is not exits.");
                //Read XML file except <signature> node as a plain text
                string line=string.Empty, data = string.Empty;
                StreamReader sr = new StreamReader(args[1]);
                while ((line = sr.ReadLine().Trim()) != "<signature>")              
                    data += line.Trim();                                 
                data += "</parent>";

                // Open sudoers.xml file in XML mode
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.PreserveWhitespace = true;

                //calculate the checkSum based on whole file data and secret encryption key
                byte[] checksumData = System.Text.Encoding.UTF8.GetBytes(data + encryptionKey);
                byte[] hash = SHA1.Create().ComputeHash(checksumData);
                string keyToPlaceInXML = Convert.ToBase64String(hash);                        
                xmlDoc.GetElementsByTagName("key").Item(0).Attributes["value"].Value = keyToPlaceInXML;
                Console.WriteLine("Key generated successfully.");             
            catch (Exception ex)
        static void showHelp()
            System.Console.WriteLine("usage: KeyGen [Option][xml file path]\n" +   
                                     "OPTIONS\n" +
                                     "-c     to specify the sudoers.xml file path\n" +
                                     "-h     displays this menu\n" +
                                     "/?     displays this menu\n" +           
                                     "EXAMPLES\n" +
                                      "KeyGen -c C:\\myxmlfile.xml\n" +
                                      "KeyGen -h\n" +
                                      "KeyGen /?\n");
By Perry    Popularity  (1919 Views)