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.
Nguồn
2012-05-15 18:48:41
Tôi nên nghĩ rằng nó sẽ làm, cảm ơn. – MarioDS
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
@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