2012-05-15 27 views
6

VB.NETでWinFormsアプリケーションのSQLサーバー接続文字列を保存する一般的な方法は何かを知る必要があります。WinFormsアプリケーションに接続文字列を安全に格納する方法は?

私はネットを検索しましたし、私は次の各質問への回答が見つかりました:

  • 私はApp.configファイルは、ASP.NET リファレンスでそれを行うにはどのよう
  • 値の読み取りにはどうすればよい:this SO question。私は、接続文字列(暗号化されていないため、安全ではない)

を保存しない私は(それが良いかどうかsettings.settings)しっかりapp.configにVB.NETでの接続文字列を格納する方法についての完全な答えをしたいと思いますどのように

  • app.configは適切な場所ですか?これらの値を暗号化できますか?

  • 答えて

    9

    単純に、.NET Frameworkが

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

    関連情報を参照することをあなたがすることができます:

    これはのmachine.configファイルに行く:

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

    これはアプリケーションコードです:

    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

    おかげthis link

    +0

    私はそれが、ありがとうと思います。 – MarioDS

    +1

    この質問にはかなり多くの意見が寄せられていますので、リンクが壊れても情報が失われないようにあなたの答えを編集しました。 – MarioDS

    +1

    @pylover _これは質問に答えますが、適切な解決策を提供しません。提供されたMSリンクから。 **注記:**: '接続文字列は暗号化されたコンピュータでのみ解読できます.' [記事のMSリンクが解決策を提供する可能性があります](https://docs.microsoft.com/en-us/dotnet/framework/data/adonet/protecting-connection-information) – wpcoder

    5

    私の仕事では、app.configに完全な接続文字列を格納しますが、AES256で暗号化します。それはかなりうまく動作し、かなりの量のセキュリティを追加します。私たちは接続文字列を暗号化および復号化できる小さなツールを書いてapp.configファイルを編集するのはかなり簡単です。暗号化キーはアプリケーションにハードコードされているだけなので、誰でもアセンブリを逆コンパイルすることができれば、それを理解することができますが、必要に応じてバーを上げます。ここでは、接続文字列の暗号化と復号化に使用するクラスがあります:

    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 
    

    実は、私たちは、秘密鍵をハードコードConnectionStringEncrpyterクラスの内部を包みました。

    +0

    ため、@wpcoder?私は暗号化クラスをパスワードのためだけに用意していますが、接続文字列全体(オーバーヘッドワイズ)ではありません。 – MarioDS

    +0

    @MarioDeSchaepmeester私はいくつかのコード例を追加しました。私は理解しています - 暗号化は苦しいことがあります。 base64にエンコードするので、テキストファイルにうまく格納されます。 –

    +0

    助けてくれてありがとう、しかし、私はピラバーが私に私が探していた答えを提供したと思う。 – MarioDS

    関連する問題