Example #1
0
        public static XmlDocument AssinarXML(this XmlDocument xmlDocument, string tagAssinatura)
        {
            var certificado = Config.Certificado;

            var reference = new System.Security.Cryptography.Xml.Reference
            {
                Uri = ""
            };

            var signedXml = new System.Security.Cryptography.Xml.SignedXml(xmlDocument)
            {
                SigningKey = certificado.PrivateKey
            };

            reference.AddTransform(new System.Security.Cryptography.Xml.XmlDsigEnvelopedSignatureTransform());

            reference.AddTransform(new System.Security.Cryptography.Xml.XmlDsigC14NTransform());

            signedXml.AddReference(reference);

            var keyInfo = new System.Security.Cryptography.Xml.KeyInfo();

            keyInfo.AddClause(new System.Security.Cryptography.Xml.KeyInfoX509Data(certificado));

            signedXml.KeyInfo = keyInfo;

            signedXml.ComputeSignature();

            var xmlDigitalSignature = signedXml.GetXml();

            xmlDocument.GetElementsByTagName(tagAssinatura)[0].AppendChild(xmlDocument.ImportNode(xmlDigitalSignature, true));

            return(xmlDocument);
        }
Example #2
0
        // Sign an XML file.
        // This document cannot be verified unless the verifying
        // code has the key with which it was signed.
        public static void SignXml(System.Xml.XmlDocument Doc, RSA Key)
        {
            // Check arguments.
            if (Doc == null)
            {
                throw new ArgumentException("Doc");
            }
            if (Key == null)
            {
                throw new ArgumentException("Key");
            }

            // Create a SignedXml object.
            var signedXml = new System.Security.Cryptography.Xml.SignedXml(Doc);

            // Add the key to the SignedXml document.
            signedXml.SigningKey = Key;

            // Create a reference to be signed.
            var reference = new System.Security.Cryptography.Xml.Reference();

            reference.Uri = "";

            // Add an enveloped transformation to the reference.
            var env = new System.Security.Cryptography.Xml.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.
            Doc.DocumentElement.AppendChild(Doc.ImportNode(xmlDigitalSignature, true));
        }
Example #3
0
            /// <summary>
            /// Create a signature xml element for the specified xml document and private key
            /// </summary>
            /// <param name="xmlToSign"></param>
            /// <param name="keyPubPri">Private+public key</param>
            /// <returns></returns>
            public static System.Xml.XmlElement CreateSignature(System.Xml.XmlDocument xmlToSign, string keyPubPri)
            {
                System.Security.Cryptography.RSACryptoServiceProvider rsa = new System.Security.Cryptography.RSACryptoServiceProvider();
                rsa.FromXmlString(keyPubPri);

                System.Security.Cryptography.Xml.SignedXml sx = new System.Security.Cryptography.Xml.SignedXml(xmlToSign);
                sx.SigningKey = rsa;

                // Create a reference to be signed
                System.Security.Cryptography.Xml.Reference reference = new System.Security.Cryptography.Xml.Reference("");

                // Set the canonicalization method for the document.
                sx.SignedInfo.CanonicalizationMethod = System.Security.Cryptography.Xml.SignedXml.XmlDsigCanonicalizationUrl; // No comments.

                // Add an enveloped transformation to the reference.
                System.Security.Cryptography.Xml.XmlDsigEnvelopedSignatureTransform env = new System.Security.Cryptography.Xml.XmlDsigEnvelopedSignatureTransform(false);
                reference.AddTransform(env);

                sx.AddReference(reference);

                sx.ComputeSignature();

                return(sx.GetXml());
            }
Example #4
0
            /// <summary>
            /// Create a signature xml element for the specified xml document and private key
            /// </summary>
            /// <param name="xmlToSign"></param>
            /// <param name="keyPubPri">Private+public key</param>
            /// <returns></returns>
            public static System.Xml.XmlElement CreateSignature(System.Xml.XmlDocument xmlToSign, string keyPubPri)
            {
                System.Security.Cryptography.RSACryptoServiceProvider rsa = new System.Security.Cryptography.RSACryptoServiceProvider();
                rsa.FromXmlString(keyPubPri);

                System.Security.Cryptography.Xml.SignedXml sx = new System.Security.Cryptography.Xml.SignedXml(xmlToSign);
                sx.SigningKey = rsa;

                // Create a reference to be signed
                System.Security.Cryptography.Xml.Reference reference = new System.Security.Cryptography.Xml.Reference("");

                // Set the canonicalization method for the document.
                sx.SignedInfo.CanonicalizationMethod = System.Security.Cryptography.Xml.SignedXml.XmlDsigCanonicalizationUrl; // No comments.

                // Add an enveloped transformation to the reference.
                System.Security.Cryptography.Xml.XmlDsigEnvelopedSignatureTransform env = new System.Security.Cryptography.Xml.XmlDsigEnvelopedSignatureTransform(false);
                reference.AddTransform(env);

                sx.AddReference(reference);

                sx.ComputeSignature();

                return sx.GetXml();
            }