Exemple #1
0
        internal static string DecryptSection(string encryptedXml, ProtectedConfigurationProvider provider)
        {
            XmlDocument doc = new XmlDocument();

            ProtectedConfigurationProvider.LoadXml(doc, encryptedXml);
            XmlNode resultNode = provider.Decrypt(doc.DocumentElement);

            return(resultNode.OuterXml);
        }
Exemple #2
0
        internal static string EncryptSection(string clearXml, ProtectedConfigurationProvider provider)
        {
            XmlDocument xmlDocument = new XmlDocument();

            xmlDocument.PreserveWhitespace = true;
            ProtectedConfigurationProvider.LoadXml(xmlDocument, clearXml);
            string  sectionName = xmlDocument.DocumentElement.Name;
            XmlNode encNode     = provider.Encrypt(xmlDocument.DocumentElement);

            return(encNode.OuterXml);
        }
Exemple #3
0
        public override XmlNode Encrypt(XmlNode node)
        {
            string text    = node.OuterXml;
            string encText = EncryptText(text);
            string pre     = @"<EncryptedData><CipherData><CipherValue>";
            string post    = @"</CipherValue></CipherData></EncryptedData>";
            string xmlText = pre + encText + post;

            XmlDocument xmlDocument = new XmlDocument();

            xmlDocument.PreserveWhitespace = true;
            ProtectedConfigurationProvider.LoadXml(xmlDocument, xmlText);
            return(xmlDocument.DocumentElement);
        }
        public override XmlNode Decrypt(XmlNode encryptedNode)
        {
            XmlDocument              xmlDocument = new XmlDocument();
            EncryptedXml             exml        = null;
            RSACryptoServiceProvider rsa         = GetCryptoServiceProvider(false, true);

            xmlDocument.PreserveWhitespace = true;
            ProtectedConfigurationProvider.LoadXml(xmlDocument, encryptedNode.OuterXml);
            exml = new FipsAwareEncryptedXml(xmlDocument);
            exml.AddKeyNameMapping(_KeyName, rsa);
            exml.DecryptDocument();
            rsa.Clear();
            return(xmlDocument.DocumentElement);
        }
Exemple #5
0
        public override XmlNode Decrypt(XmlNode encryptedNode)
        {
            if (encryptedNode.NodeType != XmlNodeType.Element ||
                encryptedNode.Name != "EncryptedData")
            {
                throw new ConfigurationErrorsException(SR.GetString(SR.DPAPI_bad_data));
            }

            XmlNode cipherNode = TraverseToChild(encryptedNode, "CipherData", false);

            if (cipherNode == null)
            {
                throw new ConfigurationErrorsException(SR.GetString(SR.DPAPI_bad_data));
            }

            XmlNode cipherValue = TraverseToChild(cipherNode, "CipherValue", true);

            if (cipherValue == null)
            {
                throw new ConfigurationErrorsException(SR.GetString(SR.DPAPI_bad_data));
            }

            string encText = cipherValue.InnerText;

            if (encText == null)
            {
                throw new ConfigurationErrorsException(SR.GetString(SR.DPAPI_bad_data));
            }

            string      decText     = DecryptText(encText);
            XmlDocument xmlDocument = new XmlDocument();

            xmlDocument.PreserveWhitespace = true;
            ProtectedConfigurationProvider.LoadXml(xmlDocument, decText);
            return(xmlDocument.DocumentElement);
        }
        public override XmlNode Encrypt(XmlNode node)
        {
            XmlDocument  xmlDocument;
            EncryptedXml exml;

            byte[]                   rgbOutput;
            EncryptedData            ed;
            KeyInfoName              kin;
            EncryptedKey             ek;
            KeyInfoEncryptedKey      kek;
            XmlElement               inputElement;
            RSACryptoServiceProvider rsa = GetCryptoServiceProvider(false, false);

            // Encrypt the node with the new key
            xmlDocument = new XmlDocument();
            xmlDocument.PreserveWhitespace = true;
            ProtectedConfigurationProvider.LoadXml(xmlDocument, "<foo>" + node.OuterXml + "</foo>");
            exml         = new EncryptedXml(xmlDocument);
            inputElement = xmlDocument.DocumentElement;

            using (SymmetricAlgorithm symAlg = GetSymAlgorithmProvider()) {
                rgbOutput           = exml.EncryptData(inputElement, symAlg, true);
                ed                  = new EncryptedData();
                ed.Type             = EncryptedXml.XmlEncElementUrl;
                ed.EncryptionMethod = GetSymEncryptionMethod();
                ed.KeyInfo          = new KeyInfo();

                ek = new EncryptedKey();
                ek.EncryptionMethod       = new EncryptionMethod(EncryptedXml.XmlEncRSA15Url);
                ek.KeyInfo                = new KeyInfo();
                ek.CipherData             = new CipherData();
                ek.CipherData.CipherValue = EncryptedXml.EncryptKey(symAlg.Key, rsa, UseOAEP);
            }

            kin       = new KeyInfoName();
            kin.Value = _KeyName;
            ek.KeyInfo.AddClause(kin);
            kek = new KeyInfoEncryptedKey(ek);
            ed.KeyInfo.AddClause(kek);
            ed.CipherData             = new CipherData();
            ed.CipherData.CipherValue = rgbOutput;
            EncryptedXml.ReplaceElement(inputElement, ed, true);

            rsa.Clear();

            // Get node from the document
            foreach (XmlNode node2 in xmlDocument.ChildNodes)
            {
                if (node2.NodeType == XmlNodeType.Element)
                {
                    foreach (XmlNode node3 in node2.ChildNodes) // node2 is the "foo" node
                    {
                        if (node3.NodeType == XmlNodeType.Element)
                        {
                            return(node3); // node3 is the "EncryptedData" node
                        }
                    }
                }
            }
            return(null);
        }