Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在任何win form应用程序中加密AppConfig文件的一部分?_C#_Encryption - Fatal编程技术网

C# 如何在任何win form应用程序中加密AppConfig文件的一部分?

C# 如何在任何win form应用程序中加密AppConfig文件的一部分?,c#,encryption,C#,Encryption,我正在开发一个小型winform应用程序。这里我有一些配置设置,例如用户名和密码之类的东西 现在我的要求是我要加密这个特定的细节。有人能告诉我如何在.NET(C#)中实现这一点吗。关于代码项目描述了如何加密和解密字符串。它是您调用的一个类,但是提供了源代码,以便您可以看到它是如何工作的 在Sharper教程中,实际上介绍了加密连接字符串的情况 不幸的是,两者都太长,无法在此处引用。您可以使用RsaProtectedConfigurationProvider您可以使用DPAPI provider对

我正在开发一个小型winform应用程序。这里我有一些配置设置,例如用户名和密码之类的东西

现在我的要求是我要加密这个特定的细节。有人能告诉我如何在.NET(C#)中实现这一点吗。

关于代码项目描述了如何加密和解密字符串。它是您调用的一个类,但是提供了源代码,以便您可以看到它是如何工作的

在Sharper教程中,实际上介绍了加密连接字符串的情况


不幸的是,两者都太长,无法在此处引用。

您可以使用RsaProtectedConfigurationProvider您可以使用
DPAPI provider对app.config的
部分进行加密。将您的用户名/密码对放入appSettings部分。您的应用程序中不需要更改任何其他内容。您仍然像往常一样读取appsettings字符串。使用下面的代码加密/解密部分配置文件

//call: ProtectSection("appSettings","DataProtectionConfigurationProvider"); 
private void ProtectSection(string sectionName, string provider) 
{ 
    Configuration config = 
        WebConfigurationManager. 
            OpenWebConfiguration(Request.ApplicationPath); 

    ConfigurationSection section = config.GetSection(sectionName); 

    if (section != null && !section.SectionInformation.IsProtected) 
    { 
        section.SectionInformation.ProtectSection(provider); 
        config.Save(); 
    } 
} 

//call: UnProtectSection("appSettings"); 
private void UnProtectSection(string sectionName) 
{ 
    Configuration config = 
        WebConfigurationManager. 
            OpenWebConfiguration(Request.ApplicationPath); 

    ConfigurationSection section = config.GetSection(sectionName); 

    if (section != null && section.SectionInformation.IsProtected) 
    { 
        section.SectionInformation.UnprotectSection(); 
        config.Save(); 
    } 
} 
有,但我从来没有尝试过这是否也可以应用到App.config。这样做的好处是,密钥存储在操作系统控制下的密钥存储中


除此之外,我不知道有任何其他内置解决方案,我们通常在读取加密值后自行解密。这需要将密钥存储在某个地方(通常包含在代码中),这远远不是最优的。因此,如果可能,应该使用Windows集成安全性(例如SQL Server身份验证已被弃用)或任何其他高级基础结构,如Kerberos(如果可用)。

这对我来说是一种代码味道。这是用于数据库访问吗?适用于任何.NET应用程序-请看我已经在另一个答案中阅读了它,很高兴知道,无论如何谢谢!配置需要使用哪种方法?