Re: Encrypt My.Settings setting?



I found this Class somehwere... can't remember where now... just include it
in your project.

Then somewhere in your main code just do something like the following...

dim EncClass as new Encryption
dim txtPlainTextPassword as string = "ThisIsMyNewPasswordSoThere!"
dim txtEncryptedPassword as string =
EncClass.EncryptData(txtPlainTextPassword)
dim txtDecryptedPassword as string =
EncClass.DecryptData(txtEncryptedPassword)

debug.print(txtPlainTextPassword)
debug.print(txtEncryptedPassword)
debug.print(txtDecryptedPassword)

Cheers, Sy

PS. Here's the Encryption Class... As I said I liked to give credit where I
found this...

Imports System.Security.Cryptography

Public NotInheritable Class Encryption

Private TripleDes As New TripleDESCryptoServiceProvider

Private svKey As String = "justsomewordstobeusedasacryptionkey"

Sub New(ByVal key As String)

' Initialize the crypto provider.

TripleDes.Key = TruncateHash(key, TripleDes.KeySize \ 8)

TripleDes.IV = TruncateHash("", TripleDes.BlockSize \ 8)

End Sub

Sub New()

TripleDes.Key = TruncateHash(svKey, TripleDes.KeySize \ 8)

TripleDes.IV = TruncateHash("", TripleDes.BlockSize \ 8)

End Sub

Private Function TruncateHash( _

ByVal key As String, _

ByVal length As Integer) _

As Byte()

Dim sha1 As New SHA1CryptoServiceProvider

' Hash the key.

Dim keyBytes() As Byte = _

System.Text.Encoding.Unicode.GetBytes(key)

Dim hash() As Byte = sha1.ComputeHash(keyBytes)

' Truncate or pad the hash.

ReDim Preserve hash(length - 1)

Return hash

End Function

Public Function EncryptData( _

ByVal plaintext As String) _

As String

' Convert the plaintext string to a byte array.

Dim plaintextBytes() As Byte = _

System.Text.Encoding.Unicode.GetBytes(plaintext)

' Create the stream.

Dim ms As New System.IO.MemoryStream

' Create the encoder to write to the stream.

Dim encStream As New CryptoStream(ms, _

TripleDes.CreateEncryptor(), _

System.Security.Cryptography.CryptoStreamMode.Write)

' Use the crypto stream to write the byte array to the stream.

encStream.Write(plaintextBytes, 0, plaintextBytes.Length)

encStream.FlushFinalBlock()

' Convert the encrypted stream to a printable string.

Return Convert.ToBase64String(ms.ToArray)

End Function

Public Function DecryptData( _

ByVal encryptedtext As String) _

As String

' Convert the encrypted text string to a byte array.

Dim encryptedBytes() As Byte = Convert.FromBase64String(encryptedtext)

' Create the stream.

Dim ms As New System.IO.MemoryStream

' Create the decoder to write to the stream.

Dim decStream As New CryptoStream(ms, _

TripleDes.CreateDecryptor(), _

System.Security.Cryptography.CryptoStreamMode.Write)

' Use the crypto stream to write the byte array to the stream.

decStream.Write(encryptedBytes, 0, encryptedBytes.Length)

decStream.FlushFinalBlock()

' Convert the plaintext stream to a string.

Return System.Text.Encoding.Unicode.GetString(ms.ToArray)

End Function

End Class





"Tom" <tom@xxxxxxxxxx> wrote in message
news:eFBHMgcqGHA.2232@xxxxxxxxxxxxxxxxxxxxxxx
Is it possible to encrypt a value in the my.settings area in VB.NET
2005? I.E. Can I add a settings value (via My Project / Settings) and
have it encrypt that value so that if anyone looks at the resulting
app.config file the value is encrypted? If so, (1) How do you specify
the value to be encrypted? And (2) How do you access it now from VB?
Can you still go through My.Settings??

Tom

--



.


Loading