Re: Encrypt My.Settings setting?

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



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

--



.



Relevant Pages

  • Re: DES Encryption and Decryption using different keys gives Bad Data exception
    ... The DESCryptoServiceProvider is based on Symmetric encryption algorithm. ... To decrypt the data the user must possess the same key and IV ... Dim btKEK1() As Byte ... 'Create the encryption stream using the memory stream and the ...
    (microsoft.public.dotnet.security)
  • Re: Encryption/Decryption Changes File Size
    ... I was able to get rid of the extra bytes in the encryption by changing ... ' Write the byte array to the crypto stream and flush it. ... Dim bufferAs Byte ... ' Read a chunk of data from the MemoryStream ...
    (microsoft.public.dotnet.languages.vb)
  • Re: encrypting/ decrypting with RSA
    ... should not be able to distinguish encryption of two messages of his/her own ... otherwise semantic security is totally compromised*. ... > Function RSADecrypt(ByVal instring As String) As String ... > Dim RSA As RSACryptoServiceProvider = New ...
    (microsoft.public.dotnet.security)
  • Re: Encrypt / Sign ? Not really sure
    ... you may be better off with symmetric encryption which uses the same key for both encryption and decryption. ... I remember learning about Public-Private Key encryption at Uni I remember that once a private and public key are created, they act more or less like the ying and yang of each other. ... Private Shared mPrivateKey As String ... Dim RSA As RSACryptoServiceProvider = New RSACryptoServiceProvider ...
    (microsoft.public.dotnet.security)
  • Re: Asymetric Encryption - What am I doing wrong?
    ... > Encryption works fine, but decryption fails with 'Bad Key' every time. ... > Public Function EncryptText(ByVal sToEncrypt As String) As String ... > Dim RSA As New RSACryptoServiceProvider ...
    (microsoft.public.dotnet.security)