当前位置:编程学习 > 网站相关 >>

写段小程序,为自己的配置加个密

配置文件,这个再平常不过了。不管你用的服务器,还是开发时用到的各种开发工具,还有我们自己开发的网站/软件,配置文件都是很重要的。今天说的就是.net环境下的配置问题。
先来段程序,以connectionString为例:
web.config中初始的配置:
<connectionStrings>
<add name="TestConnStr" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=Readers;Persist Security Info=True;User ID=test;Password=123456" providerName="System.Data.SqlClient" />
</connectionStrings>
加密代码:
         Configuration config = WebConfigurationManager.OpenWebConfiguration("~");
ConnectionStringsSection section = config.ConnectionStrings;
if (!section.SectionInformation.IsProtected && !section.SectionInformation.IsLocked)
{
section.SectionInformation.ProtectSection("RsaProtectedConfigurationProvider");
config.Save(ConfigurationSaveMode.Full, false);
}
看一下加密的结果:
<connectionStrings configProtectionProvider="RsaProtectedConfigurationProvider">
<EncryptedData Type="http://www.w3.org/2001/04/xmlenc#Element"
xmlns="http://www.w3.org/2001/04/xmlenc#">
<EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#tripledes-cbc" />
<KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#">
<EncryptedKey xmlns="http://www.w3.org/2001/04/xmlenc#">
<EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#rsa-1_5" />
<KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#">
<KeyName>Rsa Key</KeyName>
</KeyInfo>
<CipherData>
<CipherValue>Y1QD6G00t8+X3Igfth3ElX3eA/zVxVOEnQjiFLH6l7c3LQWIRe9hZV4J77QfN+7T9pzcMgaIDm8Ck5C5R9FsDaV5AzfdfT3SluDib9exZhOoZUY3IQq+S8sxf0SjzFZLw4lgfG/DPkvo6KkMAYgl/J4lsXeAg16Q0XViiOTqcI0=</CipherValue>
</CipherData>
</EncryptedKey>
</KeyInfo>
<CipherData>
<CipherValue>hoS1FoN13VLcsVPFNBV4B7Qese90qp9kudsl/svyPu9NO3wLmmhcNqzl/Dkho2EgiM4E1pkG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FhDhuut/IyOonhb7mbwRCyt+bPqrfXpAVvFtMJ8dnLIwSoKVVM2P0KW3RnVhy3H9clCedVukhAYlkGuIdiO1HB1D6P52kSkNFUDmgAWTR5JYeRcmB3TLG8T9pa7LoRTbcCkzWHHSOZHgU97uf4dHmbd7GJIRJMMwEzNAWaVob9K390x+gZ1fhg4c2XfiVW5M8dfPH2iElgBhswdQgHYuyHorLdjpRA4fclvpNylpSPj6XxH0oKhj6hYpbyYQawGp2vRp2VHVJIKRWppmJ0mAa3W5HnCZgZ/K2k1r6TLeg5DzLnpDkijmPJVNDm5n88+IDLzqLUtmFcwdN1clwjaIhhHmI0iXh/irVta59s24YPF
补充:综合编程 , 安全编程 ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,