使用C#代码加密/解密连接字符串

3

有没有办法从我的C#代码中实现连接字符串的加密和解密。

就像我们执行

aspnet_regiis -pe "connectionStrings"

aspnet_regiis -pd "connectionStrings"

谢谢


2
哪个 C# 代码 在哪里?最终,这归结于你试图保护什么攻击向量。您需要清楚地声明攻击向量,以了解加密是否实际上会增加任何安全性,而不仅仅是造成不便。例如,对 aspnet_regiis 的攻击向量是它防止文件被拷贝到应用程序外被检查。它无法保护数据免受可以访问正在运行的应用程序本身的任何东西的影响。 - Marc Gravell
原因是如果连接字符串中有任何密码,它不会以明文形式显示。 - BreakHead
@Christian K,如果答案对我没有用,我该如何接受它? - BreakHead
@BreakHead,你可能不应该这样做。然而 - 我并没有真的去查看你所有的未解决问题 - 请记住有时候正确的答案是表明问题本身不好/无法回答的答案 - 不管对提问者是否令人满意。 - Christian.K
重复内容:http://stackoverflow.com/search?q=[c#]encrypt+web.config - oleksii
你目前是如何处理设计并在应用程序中使用连接字符串的?它们是在web.config还是代码后面?它们是否根据操作环境动态确定?你是否有一个单一的类来设置值?你是否使用了硬编码的数据集?我可以继续问下去,但正如@Marc-Gravell所说,我们需要更多信息来帮助你。 - McArthey
1个回答

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接