What is the best way to encrypt connstring in app.config?
- use
cryptography for encryption and decryption, or - use
%windir%\Microsoft.NET\Framework\v2.0.50727\aspnet_regiis -pef "connectionStrings" "C:\documents and settings\bob\projects\myproject" , as recommended in Protect App.Config or Encrypt .
Care:
1) If I use Crytography, everything works fine. Except that the code below will always be called every time you run using (leDataContext db = new leDataContext()) , which makes me feel like it will slow down the system.
public partial class leDataContext { public leDataContext() : base("")
2) If I use method 2, this sounds good, as it will automatically perform encryption. However, should these encrypted <CipherValue>rUmEA8h02uMZ5M4uEVtL+5M/UvPuyJ4UJz5d/P...</CipherValue> in my app.conf when I publish them using ClickOnce?
Precisely because method 2 can only be executed on the client machine. So I have to execute method 2 on the client computer and then copy this encrypted code to a file and every time I want to publish using clickOnce, manually copy it back to App.config before publishing so that the client updates the connstring right?
Cryptography code:
internal static string Encrypt(string sender, string key) { string text1; if (sender == null) sender = ""; byte[] buffer4 = new byte[0]; byte[] buffer1 = buffer4; byte[] buffer2 = new byte[] { 110, 120, 130, 140, 150, 160, 170, 180 }; try { buffer1 = Encoding.UTF8.GetBytes(key.Substring(0, 8)); DESCryptoServiceProvider provider1 = new DESCryptoServiceProvider(); byte[] buffer3 = Encoding.UTF8.GetBytes(sender); MemoryStream stream1 = new MemoryStream(); CryptoStream stream2 = new CryptoStream(stream1, provider1.CreateEncryptor(buffer1, buffer2), CryptoStreamMode.Write); stream2.Write(buffer3, 0, buffer3.Length); stream2.FlushFinalBlock(); text1 = Convert.ToBase64String(stream1.ToArray()); } catch (Exception ex) { text1 = string.Empty; } return text1; }
Could you please consult?
c # encryption
VeecoTech
source share