2012-05-15 57 views
6

Tôi cần biết cách lưu trữ chuỗi kết nối máy chủ SQL là gì cho một ứng dụng WinForms trong VB.NET.Làm thế nào để lưu trữ an toàn chuỗi kết nối trong ứng dụng WinForms?

Tôi đã tìm kiếm trên net và tôi thấy câu trả lời cho mỗi câu hỏi sau:

  • Làm thế nào để đọc app.config giá trị
  • Làm thế nào để làm điều đó trong ASP.NET tham khảo: this SO question.
  • Làm thế nào để lưu trữ một chuỗi kết nối (không được mã hóa do đó không an toàn)

Tôi muốn một câu trả lời đầy đủ về cách lưu trữ một chuỗi kết nối trong VB.NET trong app.config (hoặc settings.settings nếu nó tốt hơn) một cách an toàn.

Có phải app.config đúng nơi không? Tôi có thể mã hóa các giá trị này không?

Trả lời

9

đơn giản, khung lưới cho phép bạn làm điều đó, xem

http://msdn.microsoft.com/en-us/library/89211k9b(v=vs.80).aspx

thông tin liên quan:

này đi vào Machine.config file:

<configProtectedData defaultProvider="RsaProtectedConfigurationProvider"> 
    <providers> 
    <add name="RsaProtectedConfigurationProvider" 
     type="System.Configuration.RsaProtectedConfigurationProvider, ... /> 
    <add name="DataProtectionConfigurationProvider" 
     type="System.Configuration.DpapiProtectedConfigurationProvider, ... /> 
    </providers> 
</configProtectedData> 

Và đây là mã ứng dụng:

Shared Sub ToggleConfigEncryption(ByVal exeConfigName As String) 
    ' Takes the executable file name without the 
    ' .config extension. 
    Try 
     ' Open the configuration file and retrieve 
     ' the connectionStrings section. 
     Dim config As Configuration = ConfigurationManager. _ 
      OpenExeConfiguration(exeConfigName) 

     Dim section As ConnectionStringsSection = DirectCast(_ 
      config.GetSection("connectionStrings"), _ 
      ConnectionStringsSection) 

     If section.SectionInformation.IsProtected Then 
      ' Remove encryption. 
      section.SectionInformation.UnprotectSection() 
     Else 
      ' Encrypt the section. 
      section.SectionInformation.ProtectSection(_ 
       "DataProtectionConfigurationProvider") 'this is an entry in machine.config 
     End If 

     ' Save the current configuration. 
     config.Save() 

     Console.WriteLine("Protected={0}", _ 
     section.SectionInformation.IsProtected) 

    Catch ex As Exception 
     Console.WriteLine(ex.Message) 
    End Try 
End Sub 

UPDATE 1

Cảm ơn @wpcoder, cho this link

+0

Tôi nên nghĩ rằng nó sẽ làm, cảm ơn. – MarioDS

+1

Vì câu hỏi này đang nhận được khá nhiều lượt xem, tôi đã chỉnh sửa câu trả lời của bạn để đảm bảo thông tin không bị mất nếu liên kết bị ngắt. – MarioDS

+1

@pylover _Điều này trả lời câu hỏi nhưng không cung cấp giải pháp thích hợp. Từ liên kết MS được cung cấp; ** lưu ý **: 'Chuỗi kết nối chỉ có thể được giải mã trên máy tính mà nó được mã hóa'. [Liên kết MS cập nhật cho bài viết có thể cung cấp giải pháp] (https://docs.microsoft.com/en-us/dotnet/framework/data/adonet/protecting-connection-information) – wpcoder

5

Tại công việc của mình, chúng tôi lưu trữ chuỗi kết nối đầy đủ trong app.config, nhưng chúng tôi mã hóa chúng bằng AES256. Nó hoạt động khá tốt và bổ sung một số tiền bảo mật hợp lý. Chúng tôi đã viết một công cụ nhỏ cho phép bạn mã hóa và giải mã các chuỗi kết nối để chỉnh sửa các tệp app.config khá dễ dàng. Chúng tôi chỉ có mã hóa mã hóa cứng trong ứng dụng, vì vậy nếu bất cứ ai quan tâm đến dịch ngược các hội đồng, có thể tìm ra nó, nhưng nó làm tăng thanh đủ cao cho nhu cầu của chúng tôi. Đây là lớp chúng tôi sử dụng để mã hóa và giải mã các chuỗi kết nối:

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 

Thực ra, chúng tôi đã bao bọc bên trong lớp ConnectionStringEncrpyter mã hóa khóa bí mật.

+0

Đó là âm thanh tuyệt vời, nhưng bạn có thể cho tôi biết làm thế nào để mã hóa nó? Tôi có một lớp mã hóa chỉ sẵn sàng cho mật khẩu, không phải cho toàn bộ chuỗi kết nối (trên không khôn ngoan) – MarioDS

+0

@MarioDeSchaepmeester Tôi đã thêm một số mã ví dụ. Tôi hiểu - mã hóa có thể là một nỗi đau. Chúng tôi mã hóa nó thành base64 để nó lưu trữ độc đáo trong tệp văn bản. –

+0

Cảm ơn sự giúp đỡ, nhưng tôi nghĩ pylover đã cung cấp cho tôi câu trả lời tôi đang tìm kiếm. – MarioDS

Các vấn đề liên quan