Ejemplo n.º 1
0
        private System.Security.Cryptography.Xml.Signature SignXml(XmlDocument xmlDoc, RSA Key)
        {
            if (xmlDoc == null)
            {
                throw new ArgumentException(nameof(xmlDoc));
            }
            if (Key == null)
            {
                throw new ArgumentException(nameof(Key));
            }
            SignedXml signedXml = new SignedXml(xmlDoc);

            signedXml.SigningKey = (AsymmetricAlgorithm)Key;
            Reference reference = new Reference();

            reference.Uri = "";
            XmlDsigEnvelopedSignatureTransform signatureTransform = new XmlDsigEnvelopedSignatureTransform();

            reference.AddTransform((Transform)signatureTransform);
            signedXml.AddReference(reference);
            signedXml.ComputeSignature();
            XmlElement xml = signedXml.GetXml();

            System.Security.Cryptography.Xml.Signature signature = signedXml.Signature;
            xmlDoc.DocumentElement.AppendChild(xmlDoc.ImportNode((XmlNode)xml, true));
            return(signature);
        }
Ejemplo n.º 2
0
		public SignedXml () 
		{
			m_signature = new Signature ();
			m_signature.SignedInfo = new SignedInfo ();
			hashes = new Hashtable (2); // 98% SHA1 for now
		}
Ejemplo n.º 3
0
        public static void SignXmlFile(string fileName, ref string signedContent, RSA key)
        {
            // Check the arguments.
            if (fileName == null)
            {
                throw new ArgumentNullException("fileName");
            }
            if (signedContent == null)
            {
                throw new ArgumentNullException("signedFileName");
            }
            if (key == null)
            {
                throw new ArgumentNullException("key");
            }

            // Create a new XML document.
            XmlDocument doc = new XmlDocument();

            // Format the document to ignore white spaces.
            doc.PreserveWhitespace = false;
            // Load the passed XML file using it's name.
            //doc.Load(new XmlTextReader(FileName));
            doc.LoadXml(fileName);
            // Create a SignedXml object.
            SignedXml signedXml = new SignedXml(doc);

            // Add the key to the SignedXml document.
            signedXml.SigningKey = key;
            // Get the signature object from the SignedXml object.
            System.Security.Cryptography.Xml.Signature XMLSignature = signedXml.Signature;
            // Create a reference to be signed.  Pass ""
            // to specify that all of the current XML
            // document should be signed.
            System.Security.Cryptography.Xml.Reference reference = new System.Security.Cryptography.Xml.Reference("");
            // Add an enveloped transformation to the reference.
            XmlDsigEnvelopedSignatureTransform env = new XmlDsigEnvelopedSignatureTransform();

            reference.AddTransform(env);
            // Add the Reference object to the Signature object.
            XMLSignature.SignedInfo.AddReference(reference);
            // Add an RSAKeyValue KeyInfo (optional; helps recipient find key to validate).
            System.Security.Cryptography.Xml.KeyInfo keyInfo = new System.Security.Cryptography.Xml.KeyInfo();
            keyInfo.AddClause(new RSAKeyValue((RSA)key));
            // Add the KeyInfo object to the Reference object.
            XMLSignature.KeyInfo = keyInfo;
            // 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.
            doc.DocumentElement.AppendChild(doc.ImportNode(xmlDigitalSignature, true));

            if (doc.FirstChild is XmlDeclaration)
            {
                doc.RemoveChild(doc.FirstChild);
            }

            signedContent = doc.InnerXml;
        }
Ejemplo n.º 4
0
 public SignedXml()
 {
     m_signature            = new Signature();
     m_signature.SignedInfo = new SignedInfo();
     hashes = new Hashtable(2);              // 98% SHA1 for now
 }