/// <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); }
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)); } }