In my work, we save the full connection strings in app.config, but we encrypt them using AES256. It works very well and greatly improves security. We wrote a small tool that allows you to encrypt and decrypt connection strings, so editing the app.config files is quite simple. We just have an encryption key hardcoded in the application, so if someone cares about decompiling assemblies, he can understand this, but he raises the bar high enough for our needs. Here is the class we use to encrypt and decrypt connection strings:
Public Class Aes256Base64Encrypter Public Function Decrypt(ByVal encryptedText As String, ByVal secretKey As String) As String Dim plainText As String = Nothing Using inputStream As MemoryStream = New MemoryStream(System.Convert.FromBase64String(encryptedText)) Dim algorithm As RijndaelManaged = getAlgorithm(secretKey) Using cryptoStream As CryptoStream = New CryptoStream(inputStream, algorithm.CreateDecryptor(), CryptoStreamMode.Read) Dim outputBuffer(0 To CType(inputStream.Length - 1, Integer)) As Byte Dim readBytes As Integer = cryptoStream.Read(outputBuffer, 0, CType(inputStream.Length, Integer)) plainText = Unicode.GetString(outputBuffer, 0, readBytes) End Using End Using Return plainText End Function Public Function Encrypt(ByVal plainText As String, ByVal secretKey As String) As String Dim encryptedPassword As String = Nothing Using outputStream As MemoryStream = New MemoryStream() Dim algorithm As RijndaelManaged = getAlgorithm(secretKey) Using cryptoStream As CryptoStream = New CryptoStream(outputStream, algorithm.CreateEncryptor(), CryptoStreamMode.Write) Dim inputBuffer() As Byte = Unicode.GetBytes(plainText) cryptoStream.Write(inputBuffer, 0, inputBuffer.Length) cryptoStream.FlushFinalBlock() encryptedPassword = System.Convert.ToBase64String(outputStream.ToArray()) End Using End Using Return encryptedPassword End Function Private Function getAlgorithm(ByVal secretKey As String) As RijndaelManaged Const salt As String = "put a salt key here" Const keySize As Integer = 256 Dim keyBuilder As Rfc2898DeriveBytes = New Rfc2898DeriveBytes(secretKey, Unicode.GetBytes(salt)) Dim algorithm As RijndaelManaged = New RijndaelManaged() algorithm.KeySize = keySize algorithm.IV = keyBuilder.GetBytes(CType(algorithm.BlockSize / 8, Integer)) algorithm.Key = keyBuilder.GetBytes(CType(algorithm.KeySize / 8, Integer)) algorithm.Padding = PaddingMode.PKCS7 Return algorithm End Function End Class
Actually, we wrapped it inside the ConnectionStringEncrpyter class, which encodes the private key.
source share