2017-09-28 42 views
0

我在我的c#代碼中有連接字符串,所以我可以使用存儲過程從數據庫中獲取數據。在c#代碼中加密連接字符串

連接字符串是這樣的:

connection.ConnectionString = "Data Source=192.168.4.33;Initial Catalog=database;Persist Security Info=True;User ID=test;[email protected]"; 

我怎樣才能加密連接,然後當我想用它解密?

+0

您可以使用RijndaelManaged的加密/解密連接字符串 – mbadeveloper

+0

而且你沒有找到谷歌的答案嗎?這是非常有據可查的。或者是因爲Xamarin的問題? – smoore4

+0

在谷歌我發現的一切都是關於加密web.config上的連接字符串。 – Phill

回答

0

您可以使用此:

try 
{ 
    // Open the configuration file and retrieve 
    // the connectionStrings section. 
    Configuration config = ConfigurationManager. 
     OpenExeConfiguration(exeConfigName); 

    ConnectionStringsSection section = 
     config.GetSection("connectionStrings") 
     as ConnectionStringsSection; 

    if (section.SectionInformation.IsProtected) 
    { 
     // Remove encryption. 
     section.SectionInformation.UnprotectSection(); 
    } 
    else 
    { 
     // Encrypt the section. 
     section.SectionInformation.ProtectSection(
      "DataProtectionConfigurationProvider"); 
    } 
    // Save the current configuration. 
    config.Save(); 

    Console.WriteLine("Protected={0}", 
     section.SectionInformation.IsProtected); 
} 
catch (Exception ex) 
{ 
    Console.WriteLine(ex.Message); 
} 

,或者你企業庫數據訪問應用程序塊來執行使用RSAProtectedConfigurationProvider或DPAPIProtectedConfigurationProvider加密。 鏈接: http://msdn.microsoft.com/en-us/library/89211k9b(VS.80).aspx

+0

這是如果我使用web.config文件,對不對? – Phill