|
using System;
using System.IO;
using System.Security;
using System.Security.Cryptography;
using System.Security.Cryptography.Xml;
using System.Xml;
namespace CodeProject.XmlDSigLic
{
/// <summary>
/// Console application to verify a signed XML file.
/// </summary>
class Verify
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static int Main(string[] args)
{
// Verify that an XML document path is provided.
if (args.Length != 1 || !File.Exists(args[0]))
{
Console.Error.WriteLine("You must provide the path to an XML " +
"document to verify.");
return 1;
}
// Get the XML content from the embedded XML public key.
Stream s = null;
string xmlkey = string.Empty;
try
{
s = typeof(Verify).Assembly.GetManifestResourceStream(
"CodeProject.XmlDSigLic.PubKey.xml");
// Read-in the XML content.
StreamReader reader = new StreamReader(s);
xmlkey = reader.ReadToEnd();
reader.Close();
}
catch (Exception e)
{
Console.Error.WriteLine("Error: could not import public key: {0}",
e.Message);
return 1;
}
// Create an RSA crypto service provider from the embedded
// XML document resource (the public key).
RSACryptoServiceProvider csp = new RSACryptoServiceProvider();
csp.FromXmlString(xmlkey);
// Load the signed XML license file.
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(args[0]);
// Create the signed XML object.
SignedXml sxml = new SignedXml(xmldoc);
try
{
// Get the XML Signature node and load it into the signed XML object.
XmlNode dsig = xmldoc.GetElementsByTagName("Signature",
SignedXml.XmlDsigNamespaceUrl)[0];
sxml.LoadXml((XmlElement)dsig);
}
catch
{
Console.Error.WriteLine("Error: no signature found.");
return 1;
}
// Verify the signature.
if (sxml.CheckSignature(csp))
Console.WriteLine("SUCCESS: Signature valid.");
else
Console.WriteLine("FAILED: Signature invalid.");
return 0;
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
Principal Software Engineer currently working on Azure SDKs at Microsoft. My opinions are my own. I work on a number of OSS projects for work and personally in numerous languages including C++, C#, JavaScript, Go, Rust, et. al. See a problem, fix a problem (or at least create an issue)!
Avid outdoor adventurer 🏔️❄️👞🚴♂️, husband, father.