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