コード例 #1
0
        public string DoEncryptOrDecrypt(bool doEncrypt, string xmlString, string protectionProviderName, string protectionProviderType, string[] paramKeys, string[] paramValues)
        {
            Type t = Type.GetType(protectionProviderType, true);

            if (!typeof(ProtectedConfigurationProvider).IsAssignableFrom(t))
            {
                throw new Exception(SR.GetString(SR.WrongType_of_Protected_provider));
            }

            ProtectedConfigurationProvider provider    = (ProtectedConfigurationProvider)Activator.CreateInstance(t);
            NameValueCollection            cloneParams = new NameValueCollection(paramKeys.Length);
            XmlNode node;

            for (int iter = 0; iter < paramKeys.Length; iter++)
            {
                cloneParams.Add(paramKeys[iter], paramValues[iter]);
            }

            provider.Initialize(protectionProviderName, cloneParams);
            XmlDocument xmlDocument = new XmlDocument();

            xmlDocument.PreserveWhitespace = true;
            xmlDocument.LoadXml(xmlString);
            if (doEncrypt)
            {
                node = provider.Encrypt(xmlDocument.DocumentElement);
            }
            else
            {
                node = provider.Decrypt(xmlDocument.DocumentElement);
            }

            return(node.OuterXml);
        }
コード例 #2
0
        public string DoEncryptOrDecrypt(bool doEncrypt, string xmlString, string protectionProviderName, string protectionProviderType, string[] paramKeys, string[] paramValues)
        {
            XmlNode node;
            Type    c = Type.GetType(protectionProviderType, true);

            if (!typeof(ProtectedConfigurationProvider).IsAssignableFrom(c))
            {
                throw new Exception(System.Web.SR.GetString("WrongType_of_Protected_provider"));
            }
            ProtectedConfigurationProvider provider = (ProtectedConfigurationProvider)Activator.CreateInstance(c);
            NameValueCollection            config   = new NameValueCollection(paramKeys.Length);

            for (int i = 0; i < paramKeys.Length; i++)
            {
                config.Add(paramKeys[i], paramValues[i]);
            }
            provider.Initialize(protectionProviderName, config);
            XmlDocument document = new XmlDocument {
                PreserveWhitespace = true
            };

            document.LoadXml(xmlString);
            if (doEncrypt)
            {
                node = provider.Encrypt(document.DocumentElement);
            }
            else
            {
                node = provider.Decrypt(document.DocumentElement);
            }
            return(node.OuterXml);
        }
コード例 #3
0
 private void InitializeProvider(string thumbprint)
 {
     // the provider needs to be initialized with the certificate thumbprint
     _provider.Initialize("CustomProvider", new NameValueCollection
     {
         { "thumbprint", thumbprint },
     });
 }
コード例 #4
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ConfigProtection"/> class.
        /// </summary>
        public ConfigProtection()
        {
            var parameters = new NameValueCollection();

            parameters["keyContainerName"]    = "CustomContainer";
            parameters["useMachineContainer"] = "true";
            parameters["useOAEP"]             = "false";

            _protectionProvider = new RsaProtectedConfigurationProvider();
            _protectionProvider.Initialize("CustomProvider", parameters);
        }