Ejemplo n.º 1
0
        /// <devdoc>
        /// Load the xml file storage.
        /// </devdoc>
        private XmlDocument LoadXmlFile(string fileName)
        {
            XmlDocument doc = new XmlDocument();

            using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read))
            {
                try
                {
                    using (ConfigurationProtector protector = runtimeConfigurationView.GetConfigurationProtector(CurrentSectionName))
                    {
                        byte[] fileBytes = new byte[fs.Length];
                        fs.Read(fileBytes, 0, fileBytes.Length);
                        if (fileBytes.Length > 0)
                        {
                            fileBytes = protector.Decrypt(fileBytes);
                            doc.Load(new MemoryStream(fileBytes));
                        }
                    }
                }
                catch (XmlException e)
                {
                    throw new ConfigurationException(SR.ExceptionInvalidXmlStorageFile(fileName), e);
                }
            }


            return(doc);
        }
Ejemplo n.º 2
0
        public void ConfigurationProtectorTestEncryptedButNoProvider()
        {
            RijndaelManaged myRijndael = new RijndaelManaged();

            myRijndael.GenerateKey();
            KeyAlgorithmPair pair = new KeyAlgorithmPair(myRijndael.Key, myRijndael.GetType().AssemblyQualifiedName);

            SaveKeyPair(pair, xmlStringWithDpapi);
            using (ConfigurationContext context = CreateContext(xmlStringNoStorageProvider))
            {
                using (ConfigurationProtector protector = new ConfigurationProtector())
                {
                    protector.Load(context, sectionName);
                }
            }
        }
Ejemplo n.º 3
0
 private void SaveData(XmlDocument xmlDoc)
 {
     using (ConfigurationProtector protector = this.runtimeConfigurationView.GetConfigurationProtector(CurrentSectionName))
     {
         if (!protector.Encrypted)
         {
             xmlDoc.Save(this.applicationDocumentPath);
         }
         else
         {
             using (FileStream fs = new FileStream(this.applicationDocumentPath, FileMode.Truncate, FileAccess.Write))
             {
                 byte[] xmlBytes = GetEncoding(xmlDoc).GetBytes(xmlDoc.OuterXml);
                 xmlBytes = protector.Encrypt(xmlBytes);
                 fs.Write(xmlBytes, 0, xmlBytes.Length);
                 fs.Flush();
             }
         }
     }
 }
Ejemplo n.º 4
0
        public void ConfigurationProtectorTestWithDpapi()
        {
            string          mySecret   = "mary had a little lamb";
            RijndaelManaged myRijndael = new RijndaelManaged();

            myRijndael.GenerateKey();
            KeyAlgorithmPair pair = new KeyAlgorithmPair(myRijndael.Key, myRijndael.GetType().AssemblyQualifiedName);

            SaveKeyPair(pair, xmlStringWithDpapi);
            ConfigurationContext context = CreateContext(xmlStringWithDpapi);

            using (ConfigurationProtector protector = new ConfigurationProtector())
            {
                protector.Load(context, sectionName);
                byte[] inBytes        = UnicodeEncoding.Unicode.GetBytes(mySecret);
                byte[] encryptedBytes = protector.Encrypt(inBytes);
                byte[] decryptedBytes = protector.Decrypt(encryptedBytes);
                Assert.AreEqual(mySecret, UnicodeEncoding.Unicode.GetString(decryptedBytes));
            }
        }
 public void ConfigurationProtectorTestWithoutDpapi()
 {
     string mySecret = "mary had a little lamb";
     RijndaelManaged myRijndael = new RijndaelManaged();
     myRijndael.GenerateKey();
     KeyAlgorithmPair pair = new KeyAlgorithmPair(myRijndael.Key, myRijndael.GetType().AssemblyQualifiedName);
     SaveKeyPair(pair, xmlString);
     ConfigurationContext context = CreateContext(xmlString);
     using (ConfigurationProtector protector = new ConfigurationProtector())
     {
         protector.Load(context, sectionName);
         byte[] inBytes = UnicodeEncoding.Unicode.GetBytes(mySecret);
         byte[] encryptedBytes = protector.Encrypt(inBytes);
         Assert.IsFalse(CryptographyUtility.CompareBytes(inBytes, encryptedBytes));
         byte[] decryptedBytes = protector.Decrypt(encryptedBytes);
         Assert.AreEqual(mySecret, UnicodeEncoding.Unicode.GetString(decryptedBytes));
     }
 }
 public void ConfigurationProtectorTestEncryptedButNoProvider()
 {
     RijndaelManaged myRijndael = new RijndaelManaged();
     myRijndael.GenerateKey();
     KeyAlgorithmPair pair = new KeyAlgorithmPair(myRijndael.Key, myRijndael.GetType().AssemblyQualifiedName);
     SaveKeyPair(pair, xmlStringWithDpapi);
     using (ConfigurationContext context = CreateContext(xmlStringNoStorageProvider))
     {
         using (ConfigurationProtector protector = new ConfigurationProtector())
         {
             protector.Load(context, sectionName);
         }
     }
 }