Create the Signature of XML file |
| Megha P posted at 12-Oct-08 09:43 |
/* 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: <parent> <?xml version="1.0" encoding="utf-8" ?>
/* All the nodes and childs */
<signature> <key value="6gljdh&*flj$(DDK@fgjfjLFOFJFDD"/> </signature>
</parent>
Advantages:
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.
Please contact me by replying to this thread if you find any difficulties or if you have any questions about this.
*/
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 { private static string encryptionKey = "oM4jBpAw39Qoo3aSGyLiYnFqi5wYSpL2";
[STAThread] static void Main(string[] args) { if (args.Length != 2 || args[0].ToLower() != "-c") { showHelp(); return; } if( !File.Exists(args[1]) ) { System.Console.WriteLine(args[1] + " file is not exits."); return; } try { //Read XML file except node as a plain text string line=string.Empty, data = string.Empty; StreamReader sr = new StreamReader(args[1]); while ((line = sr.ReadLine().Trim()) != "") data += line.Trim(); data += ""; sr.Close(); sr.Dispose();
// Open sudoers.xml file in XML mode XmlDocument xmlDoc = new XmlDocument(); xmlDoc.PreserveWhitespace = true; xmlDoc.Load(args[1]);
//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);
//write checksum to sudoers.xml file xmlDoc.GetElementsByTagName("key").Item(0).Attributes["value"].Value = keyToPlaceInXML; xmlDoc.Save(args[1]); Console.WriteLine("Key generated successfully."); } catch (Exception ex) { Console.WriteLine(ex.Message); } } static void showHelp() { System.Console.WriteLine("usage: ConfigWinKeyGen [Option][sudoers.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" + "ConfigKeyGen -c C:\\local\\config.xml\n" + "ConfigWinKeyGen -h\n" + "ConfigWinKeyGen /?\n"); } } } Regards, Megha
|
|