Da willst du etwas machen, was MS so nicht vorgesehen hat.
Du kannst in den app.config zwar den Application Teil per section.SectionInformation.ProtectSection verschlüsseln ( einmal verschlüsselt ist das transparent ), aber nicht den User Teil.
Da der aber im privaten roamingverzeichnis des Users ist, geht MS davon aus das er auch nicht verschlüsselt werden muss.
Was du also machen musst ist:
1. Umstellen der Eigenschaften auf Bereich=Benutzer und setzen von Roaming=true in den Eigenschaften.
2. Erstelle ein Settings.Extended.vb, da die Settings eine Partielle Klasse ist, lässt sie sich erweitern.
3. Bennene deine Settings in Crypted... um.
3. Erstelle dir Properties (Username,Password) die beim lesen die orginalstrings entschlüsseln und beim schreiben Verschlüsseln.
Fertig.
Imports System.Security.Cryptography
Imports System.IO
Imports System.Text
Namespace My
Partial Friend NotInheritable Class MySettings
Protected key As String = "&/?@*>:>"
Public Property UserName() As String
Get
Return decryptString(Me.CryptedUsername)
End Get
Set(ByVal value As String)
Me.CryptedUsername = encryptString(value)
End Set
End Property
Public Property Password() As String
Get
Return decryptString(Me.CryptedPassword)
End Get
Set(ByVal value As String)
Me.CryptedPassword = encryptString(value)
End Set
End Property
Public Sub New()
'constructor
End Sub
Public Function encryptString(ByVal strtext As String) As String
Return Encrypt(strtext, key)
End Function
Public Function decryptString(ByVal strtext As String) As String
Return Decrypt(strtext, key)
End Function
'The function used to encrypt the text
Private Function Encrypt(ByVal strText As String, ByVal strEncrKey _
As String) As String
Dim byKey() As Byte = {}
Dim IV() As Byte = {&H12, &H34, &H56, &H78, &H90, &HAB, &HCD, &HEF}
Try
byKey = System.Text.Encoding.UTF8.GetBytes(Left(strEncrKey, 8))
Dim des As New DESCryptoServiceProvider()
Dim inputByteArray() As Byte = Encoding.UTF8.GetBytes(strText)
Dim ms As New MemoryStream()
Dim cs As New CryptoStream(ms, des.CreateEncryptor(byKey, IV), _
CryptoStreamMode.Write)
cs.Write(inputByteArray, 0, inputByteArray.Length)
cs.FlushFinalBlock()
Return Convert.ToBase64String(ms.ToArray())
Catch ex As Exception
Return ex.Message
End Try
End Function
'The function used to decrypt the text
Private Function Decrypt(ByVal strText As String, ByVal sDecrKey _
As String) As String
Dim byKey() As Byte = {}
Dim IV() As Byte = {&H12, &H34, &H56, &H78, &H90, &HAB, &HCD, &HEF}
Dim inputByteArray(strText.Length) As Byte
Try
byKey = System.Text.Encoding.UTF8.GetBytes(Left(sDecrKey, 8))
Dim des As New DESCryptoServiceProvider()
inputByteArray = Convert.FromBase64String(strText)
Dim ms As New MemoryStream()
Dim cs As New CryptoStream(ms, des.CreateDecryptor(byKey, IV), _
CryptoStreamMode.Write)
cs.Write(inputByteArray, 0, inputByteArray.Length)
cs.FlushFinalBlock()
Dim encoding As System.Text.Encoding = System.Text.Encoding.UTF8
Return encoding.GetString(ms.ToArray())
Catch ex As Exception
Return ex.Message
End Try
End Function
End Class
End NamespaceDie Klassen sind von http://www.wizbay.com/post/2010/09/10/Encrypt-and-Decrypt-string-in-VBNet.aspx
Den Applicationteil kannst du mit http://www.emmet-gray.com/Articles/EncryptConnectionStrings.htm machen. |