本文介绍了加密和解密连接字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我加密我的app连接字符串,但是当我启动解密连接字符串并连接到我的数据库的应用程序时,连接字符串以加密格式维护 我尝试了什么: 我正在训练解密我在app.config中输入的加密连接字符串,所以在第一次我加密它比替换真正的一个由加密的链接,它看起来i encrypt my app connectionstring but when i launch the app that decrypt the connection string and connect to my database the connection string maintain in the encrypted formatWhat I have tried:i'm training to decrypt my encrypted connection string putted in app.config , so in first i crypt it thani replaced the real one by the crypted chaine and it look<?xml version="1.0" encoding="utf-8"?><configuration> <configSections> </configSections> <connectionStrings> <add name="Benificiare.Properties.Settings.LogementConnectionString" connectionString="6uDB/32KxMOEPPO0maQDJ63Adjp7okmRdGd9s67mV5+8v6wiRK8UKWscnJSbQzFZTQCLiHQnZPGC8S6lI5Uw28qgjKLL14bkL8sNaDkwRUZ+bB6GMFelH9OVZpG1p+4T/I1LosmrkHylQKHotPFZg3xq3EwDJOY3rjbgE5mu6ow=" providerName="System.Data.SqlClient"/> </connectionStrings><startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1"/></startup></configuration> 在我的代码中我做反向in my code i do the inverseConfiguration configuration = null; string connection = null; configuration = ConfigurationManager.OpenExeConfiguration(@".\Myapp.exe"); connection = ConfigurationManager.ConnectionStrings["Benificiare.Properties.Settings.LogementConnectionString"].ConnectionString; string ENC = DecryptString(connection, "KEY"); //decrypte the connection string configuration.ConnectionStrings.ConnectionStrings["Benificiare.Properties.Settings.LogementConnectionString"].ConnectionString = ENC; 当我尝试连接的时候我会有消息确认连接字符串没有改变。when i try to connect i'll have message indecate that the connectionstring is not changed.推荐答案connection = ConfigurationManager.ConnectionStrings["Myapp.Properties.Settings.LogementConnectionString"].ConnectionString;寻找名为 Myapp.Properties.Settings.LogementConnectionString 的连接。 但是这一行:seeks for a connection named Myapp.Properties.Settings.LogementConnectionString.But this line:<add name="Benificiare.Properties.Settings.LogementConnectionString" ...在名称 Benificiare.Properties.Settings.LogementConnectionString 。 您在XML文件中错误地命名了连接,或者您错误地将其命名为当您尝试从C#代码中获取它时。我很惊讶当您尝试访问不存在的连接字符串时,此代码在运行时不会产生异常。 这里最好的选择是开始使用调试器^ 。调试是开发人员工作的一个非常有趣的部分,我热烈鼓励你试一试,因为你甚至可能觉得它很有趣。 在您希望开始调试的行上放置一个断点^ ,并启动调试会话(参见上面的链接)。 我们不能为您做到这一点,我们也没有所有相关信息。但是,自己学习可以给你带来很大的满足感。declares the connection under the name Benificiare.Properties.Settings.LogementConnectionString.Either you misnamed the connection in the XML file, or you misnamed it when you try to fetch it from your C# code. I'm surprised this code does not produce an exception at runtime when you try to access an unexistent connection string.Your best option here is to start using your debugger^. Debugging is a very funny part of a developer's job, I warmly encourage you to give it a try as you even may find it entertaining.Put a breakpoint^ on the line you wish to start debugging from, and launch the debug session (see above link for that).We cannot do that for you, nor do we have all relevant informations. Learning to do it yourself could bring you quite a satisfaction, though.ConfigurationManager.OpenExeConfiguration(@".\Benificiare.exe").ConnectionStrings.ConnectionStrings["Benificiare.Properties.Settings.LogementConnectionString"].ConnectionString; 然后我解密它then i decrypt itstring ENC = DecryptString(connection, "MyPassPhrase"); 然后我要更改我的连接字符串then ihave to change my connectionstringConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None).ConnectionStrings.CurrentConfiguration.ConnectionStrings.ConnectionStrings["Benificiare.Properties.Settings.LogementConnectionString"].ConnectionString = ENC; 我的错误是运行时间的pb(文件受到保护),所以我使用 OpenExeConfiguration(ConfigurationUserLevel.None)修复它相反 OpenExeConfiguration(@。\ Myapp.exe)<?xml version="1.0" encoding="utf-8" ?><configuration> <connectionStrings> <clear /> <add name="MyConnection" providerName="System.Data.ProviderName" connectionString="Tie two pieces of string together" /> </connectionStrings> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" /> </startup></configuration> 以下应用程序显示了一种管理加密和解密的方法。它主要基于此文档。The following app shows a way of managing encryption and decryption. It's based largely on this documentation.using System;//may need to add reference to System.Configurationusing System.Configuration;namespace Scratch{ class Program { static void Main(string[] args) { //path to the exe file EnsureConnectionIsEncrypted(@".\Scratch.exe"); //To do:use a text reader to check .\Scratch.exe.config //now has an encrypted connection //Note: The App.config file in solution explorer is not changed //To retrieve the decrypted string, simply do this:- var connectionString = ConfigurationManager.ConnectionStrings["MyConnection"].ConnectionString; Console.WriteLine(connectionString); Console.ReadLine(); } //The encryption must be enacted on the client machine, //as decryption can only be done by the machine //that performed the encryption static void EnsureConnectionIsEncrypted(string exeFileName) { Configuration config = ConfigurationManager. OpenExeConfiguration(exeFileName); ConnectionStringsSection section = config.GetSection("connectionStrings") as ConnectionStringsSection; if (!section.SectionInformation.IsProtected) { section.SectionInformation.ProtectSection( "DataProtectionConfigurationProvider"); config.Save(); } } }} 加密后, exe。输出目录中的config 文件看起来像这样,实际的 CipherValue 已被截断。After encryption, the exe.config file in the output directory will look something like this, the actual CipherValue has been truncated.<?xml version="1.0" encoding="utf-8" ?><configuration> <connectionStrings configProtectionProvider="DataProtectionConfigurationProvider"> <EncryptedData> <CipherData> <CipherValue>AQAAANCMnd8BFdERjHoAwE/Cl........................</CipherValue> </CipherData> </EncryptedData> </connectionStrings> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" /> </startup></configuration> 这篇关于加密和解密连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
07-31 20:55