有什么方法可以从我的C#代码中实现对连接字符串的加密和解密。
像我们一样aspnet_regiis -pe“connectionStrings”
aspnet_regiis -pd“connectionStrings”
由于
答案 0 :(得分:0)
static public void ProtectSection()
{
// Get the current configuration file.
System.Configuration.Configuration config =
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
// Get the section.
UrlsSection section = (UrlsSection)config.GetSection("connectionStrings");
// Protect (encrypt)the section.
section.SectionInformation.ProtectSection("RsaProtectedConfigurationProvider");
// Save the encrypted section.
section.SectionInformation.ForceSave = true;
config.Save(ConfigurationSaveMode.Full);
// Display decrypted configuration
// section. Note, the system
// uses the Rsa provider to decrypt
// the section transparently.
string sectionXml =
section.SectionInformation.GetRawXml();
Console.WriteLine("Decrypted section:");
Console.WriteLine(sectionXml);
}