admin管理员组文章数量:1639833
有时设计客户端程序,需要发布给其他人使用,如app.config文件中存在一些敏感数据,存在一定的安全隐患,解决办法之一就是加密这些配置文件中的敏感数据,可以在用户安装程序时自动加密配置文件内容。
方法之一 就是使用 的两个提供程序,好处就是使用配置文件中配置时, 自动解密,代码不需要调整,如同未使用加密配置。
DPAPIProtectedConfigurationProvider 使用 Windows 数据保护 API (DPAPI)对数据进行加密和解密。 加密后的文件只能在加密的机器上解密,拷贝到其他机器上不能解密
RsaProtectedConfigurationProvider 使用 RSA 加密算法对数据进行加密和解密。可以实现加密后的文件拷贝的其他机器上进行解密
可以根据需求选择其一进行加密,针对DPAPIProtectedConfigurationProvider 的使用,过程如下:
1 新建windows 应用程序 demoapp
2 添加配置文件app.config,内容如下:
<?xml version="1.0"?>
<configuration>
<configSections>
</configSections>
<connectionStrings>
<add name="constr" connectionString="Data Source=100.223.22.21;Initial Catalog=testdb;Persist Security Info=True;User ID=sa;Password=xyzpdd54655" providerName="System.Data.SqlClient"/>
</connectionStrings>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup></configuration>
加密内容是connectionstrings配置节
3.添加安装类(installer)文件,demoapp_installer.cs 内容如下:
[RunInstaller(true)]
public partial class demoapp_installer: System.Configuration.Install.Installer
{
public guang_caiji_installer()
{
InitializeComponent();
}
public override void Install(System.Collections.IDictionary stateSaver)
{
base.Install(stateSaver);
ProtectSection("connectionStrings", Context.Parameters["assemblypath"]);
}
private void ProtectSection(string connectionStrings, string exeFilePath)
{
Configuration configuration = null;
// Open the configuration file and retrieve the connectionStrings section.
configuration = ConfigurationManager.OpenExeConfiguration(exeFilePath);
ConnectionStringsSection configSection = configuration.GetSection("connectionStrings") as ConnectionStringsSection;
if ((!(configSection.ElementInformation.IsLocked)) && (!(configSection.SectionInformation.IsLocked)))
{
if (!configSection.SectionInformation.IsProtected)
{
//this line will encrypt the file
configSection.SectionInformation.ProtectSection("DataProtectionConfigurationProvider");
}
//re-save the configuration file section
configSection.SectionInformation.ForceSave = true;
// Save the current configuration
configuration.Save();
//configFile.FilePath
}
}
4.添加安装项目 setup project demoapp_setup
View—File System—Application Folder —Add— Project output,选择primary output
5.添加自定义动作 custome action
install 里面添加
6.编译发布安装包,安装后可以看到配置文件connectionstrings节已经加密
版权声明:本文标题:app.config文件加密安装包制作 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://www.elefans.com/dianzi/1729295571a1194676.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论