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