You can use this code to encrypt and decrypt a stored value.
Imports System.Security .Cryptography
Imports System.Text
Module mod_Globals
Public EncryptionKey As String =
"justsomewordst obeusedasacrypt ionkey"
Public Function EncryptString12 8Bit(ByVal vstrTextToBeEnc rypted As
String, ByVal vstrEncryptionK ey As String) As String
Dim bytValue() As Byte
Dim bytKey() As Byte
Dim bytEncoded() As Byte
Dim bytIV() As Byte = {121, 241, 10, 1, 132, 74, 11, 39, 255,
91, 45, 78, 14, 211, 22, 62}
Dim intLength As Integer
Dim intRemaining As Integer
Dim objMemoryStream As New MemoryStream
Dim objCryptoStream As CryptoStream
Dim objRijndaelMana ged As RijndaelManaged
vstrTextToBeEnc rypted =
StripNullCharac ters(vstrTextTo BeEncrypted)
bytValue =
Encoding.ASCII. GetBytes(vstrTe xtToBeEncrypted .ToCharArray)
intLength = Len(vstrEncrypt ionKey)
If intLength >= 32 Then
vstrEncryptionK ey = Strings.Left(vs trEncryptionKey , 32)
Else
intLength = Len(vstrEncrypt ionKey)
intRemaining = 32 - intLength
vstrEncryptionK ey = vstrEncryptionK ey &
Strings.StrDup( intRemaining, "X")
End If
bytKey = Encoding.ASCII. GetBytes(vstrEn cryptionKey.ToC harArray)
objRijndaelMana ged = New RijndaelManaged
Try
objCryptoStream = New CryptoStream(ob jMemoryStream,
objRijndaelMana ged.CreateEncry ptor(bytKey, bytIV),
CryptoStreamMod e.Write)
objCryptoStream .Write(bytValue , 0, bytValue.Length )
objCryptoStream .FlushFinalBloc k()
bytEncoded = objMemoryStream .ToArray
objMemoryStream .Close()
objCryptoStream .Close()
Catch
End Try
Return Convert.ToBase6 4String(bytEnco ded)
End Function
Public Function DecryptString12 8Bit(ByVal vstrStringToBeD ecrypted
As String, ByVal vstrDecryptionK ey As String) As String
Dim bytDataToBeDecr ypted() As Byte
Dim bytTemp() As Byte
Dim bytIV() As Byte = {121, 241, 10, 1, 132, 74, 11, 39, 255,
91, 45, 78, 14, 211, 22, 62}
Dim objRijndaelMana ged As New RijndaelManaged
Dim objMemoryStream As MemoryStream
Dim objCryptoStream As CryptoStream
Dim bytDecryptionKe y() As Byte
Dim intLength As Integer
Dim intRemaining As Integer
Dim intCtr As Integer
Dim strReturnString As String = String.Empty
Dim achrCharacterAr ray() As Char
Dim intIndex As Integer
bytDataToBeDecr ypted =
Convert.FromBas e64String(vstrS tringToBeDecryp ted)
intLength = Len(vstrDecrypt ionKey)
If intLength >= 32 Then
vstrDecryptionK ey = Strings.Left(vs trDecryptionKey , 32)
Else
intLength = Len(vstrDecrypt ionKey)
intRemaining = 32 - intLength
vstrDecryptionK ey = vstrDecryptionK ey &
Strings.StrDup( intRemaining, "X")
End If
bytDecryptionKe y =
Encoding.ASCII. GetBytes(vstrDe cryptionKey.ToC harArray)
ReDim bytTemp(bytData ToBeDecrypted.L ength)
objMemoryStream = New MemoryStream(by tDataToBeDecryp ted)
Try
objCryptoStream = New CryptoStream(ob jMemoryStream,
objRijndaelMana ged.CreateDecry ptor(bytDecrypt ionKey, bytIV),
CryptoStreamMod e.Read)
objCryptoStream .Read(bytTemp, 0, bytTemp.Length)
objCryptoStream .FlushFinalBloc k()
objMemoryStream .Close()
objCryptoStream .Close()
Catch
End Try
Return StripNullCharac ters(Encoding.A SCII.GetString( bytTemp))
End Function
Public Function StripNullCharac ters(ByVal vstrStringWithN ulls As
String) As String
Dim intPosition As Integer
Dim strStringWithOu tNulls As String
intPosition = 1
strStringWithOu tNulls = vstrStringWithN ulls
Do While intPosition 0
intPosition = InStr(intPositi on, vstrStringWithN ulls,
vbNullChar)
If intPosition 0 Then
strStringWithOu tNulls = Left$(strString WithOutNulls,
intPosition - 1) & _
Right$(strStrin gWithOutNulls,
Len(strStringWi thOutNulls) - intPosition)
End If
If intPosition strStringWithOu tNulls.Length Then
Exit Do
End If
Loop
Return strStringWithOu tNulls
End Function
End Module
Then to call this code do the following:
'Get Password
Dim strPassword as string = DecryptString12 8Bit(My.Setting s.Password,
EncryptionKey)
'Save Password
My.Settings.Pas sword = EncryptString12 8Bit(txt_Passwo rd1.Text.Trim,
EncryptionKey)
Hope this helps!
I didn't write this and I can't remember who did, otherwise I would
reference them.
Israel
Tom wrote:
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
--