/// <summary>
        /// Decrypts the specified XML element.
        /// </summary>
        /// <param name="encryptedElement">An encrypted XML element.</param>
        /// <returns>The decrypted form of <paramref name="encryptedElement"/>.</returns>
        /// <remarks>
        public XElement Decrypt(XElement encryptedElement)
        {
            if (encryptedElement == null)
            {
                throw new ArgumentNullException(nameof(encryptedElement));
            }

            // <EncryptedData Type="http://www.w3.org/2001/04/xmlenc#Element" xmlns="http://www.w3.org/2001/04/xmlenc#">
            //   ...
            // </EncryptedData>

            // EncryptedXml works with XmlDocument, not XLinq. When we perform the conversion
            // we'll wrap the incoming element in a dummy <root /> element since encrypted XML
            // doesn't handle encrypting the root element all that well.
            var xmlDocument = new XmlDocument();
            xmlDocument.Load(new XElement("root", encryptedElement).CreateReader());
            var elementToDecrypt = (XmlElement)xmlDocument.DocumentElement.FirstChild;

            // Perform the decryption and update the document in-place.
            var encryptedXml = new EncryptedXml(xmlDocument);
            _decryptor.PerformPreDecryptionSetup(encryptedXml);
            encryptedXml.DecryptDocument();

            // Strip the <root /> element back off and convert the XmlDocument to an XElement.
            return XElement.Load(xmlDocument.DocumentElement.FirstChild.CreateNavigator().ReadSubtree());
        }
 /// <summary>
 /// Decrypt data with X509 certificate
 /// </summary>
 /// <param name="encryptedNode"></param>
 /// <returns></returns>
 public override System.Xml.XmlNode Decrypt(System.Xml.XmlNode encryptedNode)
 {
     XmlDocument doc = encryptedNode.OwnerDocument;
     EncryptedXml eXml = new EncryptedXml(doc);
     eXml.DecryptDocument();
     return doc.DocumentElement;
 }
Пример #3
0
 /// <summary>
 /// 解密Xml。
 /// </summary>
 /// <param name="doc"></param>
 public static void DecryptorXml(XmlDocument doc)
 {
     if (doc != null)
     {
         EncryptedXml encXml = new EncryptedXml(doc);
         encXml.AddKeyNameMapping("session", keyAlgorithm);
         encXml.DecryptDocument();
     }
 }
        public override XmlNode Decrypt(XmlNode encryptedNode)
        {
            // Load config section to encrypt into xmlDocument instance
            XmlDocument doc = encryptedNode.OwnerDocument;
            EncryptedXml eXml = new EncryptedXml(doc);

            // Add a key-name mapping. This method can only decrypt documents that present the specified key name.
            eXml.AddKeyNameMapping(this.keyName, this.rsaKey);

            eXml.DecryptDocument();
            return doc.DocumentElement;
        }
 public override XmlNode Decrypt(XmlNode encryptedNode)
 {
     XmlDocument document = new XmlDocument();
     EncryptedXml xml = null;
     RSACryptoServiceProvider cryptoServiceProvider = this.GetCryptoServiceProvider(false, true);
     document.PreserveWhitespace = true;
     document.LoadXml(encryptedNode.OuterXml);
     xml = new EncryptedXml(document);
     xml.AddKeyNameMapping(this._KeyName, cryptoServiceProvider);
     xml.DecryptDocument();
     cryptoServiceProvider.Clear();
     return document.DocumentElement;
 }
		public override XmlNode Decrypt (XmlNode encrypted_node)
		{
			XmlDocument doc = new ConfigurationXmlDocument ();
			
			doc.Load (new StringReader (encrypted_node.OuterXml));

			EncryptedXml ex = new EncryptedXml (doc);

			ex.AddKeyNameMapping ("Rsa Key", GetProvider ());

			ex.DecryptDocument ();
			
			return doc.DocumentElement;
		}
        public override XmlNode Decrypt(XmlNode encryptedNode)
        {
            XmlDocument                 xmlDocument = new XmlDocument();
            EncryptedXml                exml        = null;
            RSACryptoServiceProvider    rsa         = GetCryptoServiceProvider(false, true);

            xmlDocument.PreserveWhitespace = true;
            xmlDocument.LoadXml(encryptedNode.OuterXml);
            exml = new EncryptedXml(xmlDocument);
            exml.AddKeyNameMapping(_KeyName, rsa);
            exml.DecryptDocument();
            rsa.Clear();
            return xmlDocument.DocumentElement;
        }
Пример #8
0
        /// <summary>
        /// Permite desencriptar con una llave privada un documento XML encriptado con una clave pública
        /// </summary>
        /// <param name="xmlIn"></param>
        /// <param name="privateKey"></param>
        /// <returns></returns>
        public static XmlDocument DecryptXml(XmlDocument xmlIn, RSA privateKey)
        {
            const string keyName = "Llave";
            if ((xmlIn == null))
            {
                throw new ArgumentNullException("xmlIn");
            }
            if ((privateKey == null))
            {
                throw new ArgumentNullException("privateKey");
            }

            var exml = new EncryptedXml(xmlIn);
            exml.AddKeyNameMapping(keyName, privateKey);
            exml.DecryptDocument();
            return xmlIn;
        }
Пример #9
0
        public static void Decrypt(XmlDocument Doc, RSA Alg, string KeyName)
        {
            // Check the arguments.
            if (Doc == null)
                throw new ArgumentNullException("Doc");
            if (Alg == null)
                throw new ArgumentNullException("Alg");
            if (KeyName == null)
                throw new ArgumentNullException("KeyName");
            // Create a new EncryptedXml object.
            EncryptedXml exml = new EncryptedXml(Doc);

            // Add a key-name mapping.
            // This method can only decrypt documents
            // that present the specified key name.
            exml.AddKeyNameMapping(KeyName, Alg);

            // Decrypt the element.
            exml.DecryptDocument();
        }
Пример #10
0
        public static void DecryptXmlDocument(
            Stream sourceXmlFile,
            Stream destinationXmlFile,
            X509Certificate2 certificate)
        {
            // Apro il documento
            XmlDocument doc = new XmlDocument();
            doc.Load(sourceXmlFile);

            // Decifro il nodo di tipo elemento
            EncryptedXml enc = new EncryptedXml(doc);
            enc.DecryptDocument();

            // Salvo il risultato
            doc.Save(destinationXmlFile);
        }
Пример #11
0
        public ActionResult Index(XmlModel model)
        {
            if (model.Action == "encrypt")
            {
                var recipientCertificate = LoadCertificate(model.RecipientThumbprint);
                var signingCertificate = LoadCertificate(model.SenderThumbprint);
                var xmlDocument = new XmlDocument();
                xmlDocument.LoadXml(model.PlainText);

                var elementToEncrypt = xmlDocument.GetElementsByTagName("message")[0] as XmlElement;
                var encryptedXml = new EncryptedXml();

                // Encrypt the element.
                var encryptedElement = encryptedXml.Encrypt(elementToEncrypt, recipientCertificate);
                EncryptedXml.ReplaceElement(elementToEncrypt, encryptedElement, false);

                // Sign the document
                var signedXml = new SignedXml(xmlDocument) { SigningKey = signingCertificate.PrivateKey };
                var reference = new Reference { Uri = string.Empty };

                var transform = new XmlDsigC14NTransform();
                reference.AddTransform(transform);

                var envelope = new XmlDsigEnvelopedSignatureTransform();
                reference.AddTransform(envelope);
                signedXml.AddReference(reference);

                var keyInfo = new KeyInfo();
                keyInfo.AddClause(new KeyInfoX509Data(signingCertificate));
                signedXml.KeyInfo = keyInfo;
                signedXml.ComputeSignature();

                var xmlDigitalSignature = signedXml.GetXml();
                xmlDocument.DocumentElement.AppendChild(xmlDocument.ImportNode(xmlDigitalSignature, true));

                model.PlainText = "";
                model.Envelope = XmlToString(xmlDocument);
            }
            else if (model.Action == "decrypt")
            {
                var xmlDocument = new XmlDocument();
                xmlDocument.LoadXml(model.Envelope);

                // Validate the signature
                var signedXml = new SignedXml(xmlDocument);
                var nodeList = xmlDocument.GetElementsByTagName("Signature");

                if (nodeList.Count <= 0)
                {
                    throw new Exception("No signature found.");
                }

                signedXml.LoadXml((XmlElement)nodeList[0]);
                AsymmetricAlgorithm signingKey;

                if (!signedXml.CheckSignatureReturningKey(out signingKey))
                {
                    throw new Exception("Invalid Signature");
                }
                else
                {
                    IEnumerable<X509Certificate2> keyInfoCertificates =
                        signedXml.KeyInfo.OfType<KeyInfoX509Data>()
                            .SelectMany(x => x.Certificates.Cast<X509Certificate2>());
                    var signingCertificate = keyInfoCertificates.FirstOrDefault(x => x.PublicKey.Key == signingKey);
                    if (signingCertificate == null)
                    {
                        throw new Exception("Signing certificate not found in KeyInfo.");
                    }
                    model.SenderSubject = signingCertificate.Subject;
                }

                var encryptedXml = new EncryptedXml(xmlDocument);
                encryptedXml.DecryptDocument();

                model.Envelope = "";
                model.PlainText = XmlToString(xmlDocument);
            }

            ModelState.Clear();
            model.RecipientThumbprint = RecipientThumbprint;
            model.SenderThumbprint = SenderThumbprint;
            return View(model);
        }
Пример #12
0
 public static void Decrypt(XmlDocument Doc,AsymmetricAlgorithm privateKey)
 {
     EncryptedXml exml = new EncryptedXml(Doc);
     exml.AddKeyNameMapping("encKey", privateKey);
     exml.DecryptDocument();
 }
Пример #13
0
 //解密
 private void btn2Open_Click(object sender, EventArgs e)
 {
     try
     {
         RSACryptoServiceProvider rsaKey = new RSACryptoServiceProvider();
         rsaKey.FromXmlString(privateK);
         XmlDocument xmlDoc = new XmlDocument();
         xmlDoc.PreserveWhitespace = true;
         xmlDoc.Load(this.label2.Text);
         EncryptedXml exml = new EncryptedXml(xmlDoc);
         exml.AddKeyNameMapping(keyName, rsaKey);
         exml.DecryptDocument();
         xmlDoc.Save(this.label2.Text);
         succes();
     }
     catch (Exception)
     {
         fail();
     }
 }
Пример #14
0
 public XmlDocument DecryptEncriptedXmlfile(string txtEncryptedXml)
 {
     XmlDocument xmlEncDoc = new XmlDocument();
     xmlEncDoc.LoadXml(txtEncryptedXml);
     var rsa = new RSACryptoServiceProvider();
     EncryptedXml encXml = new EncryptedXml(xmlEncDoc);
     encXml.AddKeyNameMapping("asyncKey", rsa);
     encXml.DecryptDocument();
     return xmlEncDoc;
 }
Пример #15
0
        public static void Decrypt(string xmlEncrypted, SymmetricAlgorithm Alg, string KeyName)
        {
     

            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.PreserveWhitespace = true;
            xmlDoc.LoadXml(xmlEncrypted);


            // Create a new EncryptedXml object.
            EncryptedXml exml = new EncryptedXml(xmlDoc);

            // Add a key-name mapping.
            // This method can only decrypt documents
            // that present the specified key name.
            exml.AddKeyNameMapping(KeyName, Alg);

            // Decrypt the element.
            exml.DecryptDocument();

        }
Пример #16
0
        public static string Decrypt(string xmlEncrypted, string elementPath, SymmetricAlgorithm symmetricAlgorithm)
        {

            // Check the arguments.  
            if (string.IsNullOrEmpty(xmlEncrypted))
                throw new ArgumentNullException("xml");
            if (string.IsNullOrEmpty(elementPath))
                throw new ArgumentNullException("elementPath");
            if (symmetricAlgorithm == null)
                throw new ArgumentNullException("SymmetricAlgorithm ");


            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.PreserveWhitespace = true;
            xmlDoc.LoadXml(xmlEncrypted);


            // Find the EncryptedData element in the XmlDocument.
            
            XmlNodeList encryptedElementxmlDoc = xmlDoc.GetElementsByTagName("EncryptedData");

            // If the EncryptedData element was not found, throw an exception.
            if (encryptedElementxmlDoc == null)
            {
                throw new XmlException("The EncryptedData element was not found.");
            }
            EncryptedData edElement;
            EncryptedXml exml = new EncryptedXml(xmlDoc);
            // Add a key-name mapping.
            // This method can only decrypt documents
            // that present the specified key name.
            exml.AddKeyNameMapping("", symmetricAlgorithm);


            exml.DecryptDocument();
      
            //foreach (XmlNode node in encryptedElementxmlDoc)
            //{


            //    // Create an EncryptedData object and populate it.
            //    edElement = new EncryptedData();
            //    edElement.LoadXml(node as XmlElement);
            //    exml = new EncryptedXml();

            //    exml.DecryptDocument();

            //    //// Decrypt the element using the symmetric key.
            //    //byte[] rgbOutput = exml.DecryptData(edElement, symmetricAlgorithm);

            //    //// Replace the encryptedData element with the plaintext XML element.
            //    //exml.ReplaceData(node as XmlElement, rgbOutput);
            //}



            symmetricAlgorithm.Clear();
            xmlEncrypted = xmlDoc.InnerXml;
            xmlDoc = null;
            return xmlEncrypted;
        }