Пример #1
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();
     }
 }
Пример #2
0
        public static void Encrypt(XmlDocument Doc, string ElementToEncrypt, AsymmetricAlgorithm publicKey)
        {
            XmlElement elementToEncrypt = Doc.GetElementsByTagName(ElementToEncrypt)[0] as XmlElement;

            EncryptedXml eXml = new EncryptedXml();
            eXml.AddKeyNameMapping("encKey", publicKey);

            EncryptedData edElement = eXml.Encrypt(elementToEncrypt, "encKey");
            EncryptedXml.ReplaceElement(elementToEncrypt, edElement, false);
        }
Пример #3
0
 /// <summary>
 /// 加密Xml。
 /// </summary>
 /// <param name="doc"></param>
 public static void EncryptorXml(XmlDocument doc)
 {
     if (doc != null)
     {
         XmlElement encElement = doc.DocumentElement;
         EncryptedXml encXml = new EncryptedXml(doc);
         encXml.AddKeyNameMapping("session", keyAlgorithm);
         EncryptedData encData = encXml.Encrypt(encElement, "session");
         EncryptedXml.ReplaceElement(encElement, encData, false);
     }
 }
        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 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;
        }
		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 void LoadKeyValuePairsFromValidEncryptedXml()
        {
            var xml = @"
                <settings>
                    <Data.Setting>
                        <DefaultConnection>
                            <Connection.String>Test.Connection.String</Connection.String>
                            <Provider>SqlClient</Provider>
                        </DefaultConnection>
                        <Inventory>
                            <ConnectionString>AnotherTestConnectionString</ConnectionString>
                            <Provider>MySql</Provider>
                        </Inventory>
                    </Data.Setting>
                </settings>";

            // This AES key will be used to encrypt the 'Inventory' element
            var aes = Aes.Create();
            aes.KeySize = 128;
            aes.GenerateKey();

            // Perform the encryption
            var xmlDocument = new XmlDocument();
            xmlDocument.LoadXml(xml);
            var encryptedXml = new EncryptedXml(xmlDocument);
            encryptedXml.AddKeyNameMapping("myKey", aes);
            var elementToEncrypt = (XmlElement)xmlDocument.SelectSingleNode("//Inventory");
            EncryptedXml.ReplaceElement(elementToEncrypt, encryptedXml.Encrypt(elementToEncrypt, "myKey"), content: false);

            // Quick sanity check: the document should no longer contain an 'Inventory' element
            Assert.Null(xmlDocument.SelectSingleNode("//Inventory"));

            // Arrange
            var xmlConfigSrc = new XmlConfigurationSource(ArbitraryFilePath, new EncryptedXmlDocumentDecryptor(doc =>
            {
                var innerEncryptedXml = new EncryptedXml(doc);
                innerEncryptedXml.AddKeyNameMapping("myKey", aes);
                return innerEncryptedXml;
            }));

            // Act
            xmlConfigSrc.Load(StringToStream(xmlDocument.OuterXml));

            // Assert
            Assert.Equal("Test.Connection.String", xmlConfigSrc.Get("DATA.SETTING:DEFAULTCONNECTION:CONNECTION.STRING"));
            Assert.Equal("SqlClient", xmlConfigSrc.Get("DATA.SETTING:DefaultConnection:Provider"));
            Assert.Equal("AnotherTestConnectionString", xmlConfigSrc.Get("data.setting:inventory:connectionstring"));
            Assert.Equal("MySql", xmlConfigSrc.Get("Data.setting:Inventory:Provider"));
        }
Пример #9
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;
        }
Пример #10
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();
        }
Пример #11
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;
        }
Пример #12
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();
     }
 }
Пример #13
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;
 }
Пример #14
0
 public XmlDocument CreateEncriptedXmlfile(string txtXml)
 {
     XmlDocument xmlDoc = new XmlDocument();
     try
     {
         xmlDoc.LoadXml(txtXml);
         var rsa = new RSACryptoServiceProvider();
         XmlElement xmlElemt;
         xmlElemt = xmlDoc.DocumentElement;
         EncryptedXml xmlEnc = new EncryptedXml(xmlDoc);
         xmlEnc.AddKeyNameMapping("asyncKey", rsa);
         EncryptedData encXml = xmlEnc.Encrypt(xmlElemt, "asyncKey");
         EncryptedXml.ReplaceElement(xmlElemt, encXml, false);
     }
     catch (XmlException ex)
     {
         throw ex;
     }
     return  xmlDoc ;
 }
Пример #15
0
        /// <summary>
        /// ����XmlԪ��
        /// </summary>
        /// <param name="doc">XmlDocument</param>
        /// <param name="elmentName">Ҫ���ܵ�Ԫ������</param>
        /// <param name="isContent">����Ԫ������ΪTrue,��������Ԫ��ΪFalse</param>
        public static void EncryptorXmlElement(XmlDocument doc, string elmentName, bool isContent)
        {
            if (doc == null)
                throw new ArgumentNullException("doc");

            EncryptedXml encXml = new EncryptedXml(doc);
            encXml.AddKeyNameMapping("session", keyAlgorithm);

            XmlElement encElement = null;
            EncryptedData encData = null;
            if (string.IsNullOrEmpty(elmentName))
            {
                encElement = doc.DocumentElement;
                encData = encXml.Encrypt(encElement, "session");
                EncryptedXml.ReplaceElement(encElement, encData, isContent);
            }
            else
            {
                XmlNodeList nodeList = doc.GetElementsByTagName(elmentName);
                if (nodeList != null && nodeList.Count > 0)
                {
                    for (int i = 0; i < nodeList.Count; i++)
                    {
                        encElement = nodeList[i] as XmlElement;
                        if (encElement != null)
                        {
                            encData = encXml.Encrypt(encElement, "session");
                            EncryptedXml.ReplaceElement(encElement, encData, isContent);
                        }
                    }
                }
            }
        }
Пример #16
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();

        }
		public override XmlNode Encrypt (XmlNode node)
		{
			XmlDocument doc = new ConfigurationXmlDocument ();
			
			doc.Load (new StringReader (node.OuterXml));

			EncryptedXml ex = new EncryptedXml (doc);

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

			EncryptedData d = ex.Encrypt (doc.DocumentElement, "Rsa Key");

			return d.GetXml();
		}
Пример #18
0
 public static void Decrypt(XmlDocument Doc,AsymmetricAlgorithm privateKey)
 {
     EncryptedXml exml = new EncryptedXml(Doc);
     exml.AddKeyNameMapping("encKey", privateKey);
     exml.DecryptDocument();
 }