0% found this document useful (0 votes)
15 views3 pages

Digital Signature

XML Digital Signature
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views3 pages

Digital Signature

XML Digital Signature
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Digital signature

using System;
using System.Runtime.Versioning;
using System.Security.Cryptography;
using System.Security.Cryptography.Xml; // Ensure this package is installed via NuGet
using System.Xml;

//[SupportedOSPlatform("Windows")] // Only use this attribute if it's necessary


public class SignXML
{
public static void Main(String[] args)
{
try
{
// Create a new CspParameters object to specify
// a key container.
CspParameters cspParams = new CspParameters
{
KeyContainerName = "XML_DSIG_RSA_KEY"
};

// Create a new RSA signing key and save it in the container.


RSACryptoServiceProvider rsaKey = new RSACryptoServiceProvider(cspParams);

// Create a new XML document.


XmlDocument xmlDoc = new XmlDocument
{
// Load an XML file into the XmlDocument object.
PreserveWhitespace = true
};

// Load the XML file (ensure that the XML file path is correct).
//xmlDoc.Load("test1.xml");
xmlDoc.Load(@"D:\xmllabwork\test1.xml");
// Sign the XML document.
SignXml(xmlDoc, rsaKey);

Console.WriteLine("XML file signed successfully.");

// Save the signed XML document.


xmlDoc.Save("signed_test.xml");
}
catch (Exception e)
{
Console.WriteLine($"An error occurred: {e.Message}");
}
}

// Sign the XML file.


public static void SignXml(XmlDocument xmlDoc, RSA rsaKey)
{
// Check arguments.
if (xmlDoc == null)
throw new ArgumentNullException(nameof(xmlDoc));
if (rsaKey == null)
throw new ArgumentNullException(nameof(rsaKey));
// Create a SignedXml object.
SignedXml signedXml = new SignedXml(xmlDoc)
{
// Add the key to the SignedXml document.
SigningKey = rsaKey
};

// Create a reference to be signed.


Reference reference = new Reference
{
Uri = "" // Empty string for signing the entire document
};

// Add an enveloped transformation to the reference.


XmlDsigEnvelopedSignatureTransform env = new XmlDsigEnvelopedSignatureTransform();
reference.AddTransform(env);

// Add the reference to the SignedXml object.


signedXml.AddReference(reference);

// Compute the signature.


signedXml.ComputeSignature();

// Get the XML representation of the signature and save it to an XmlElement object.
XmlElement xmlDigitalSignature = signedXml.GetXml();

// Append the element to the XML document.


xmlDoc.DocumentElement?.AppendChild(xmlDoc.ImportNode(xmlDigitalSignature, true));
}
}

<?xml version="1.0" encoding="UTF-8" ?>

<root>
<creditcard>

<number>19834209</number>

<expiry>02/02/2002</expiry>

</creditcard>

</root>

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy