Newbie: Password encryption

  • Thread starter Thread starter Adriano
  • Start date Start date
A

Adriano

hello,

i'm using app.config for storing passwords,
how can I encrypt so users can't read it, or is there any other ways for
using configuration file in such a way???

Regards,

Adriano
 
i'm using app.config for storing passwords,
how can I encrypt so users can't read it, or is there any other ways for
using configuration file in such a way???

Hash the passwords with the function:
FormsAuthentication.HashPasswordForStoringInConfigFile
 
hello Lucas,
thanks for reply,

can you send me an example please,

Regards,
Adriano
 
You could use this class :

Imports System.Security.Cryptography
Imports System.Text
Imports System.IO



Public Class MyEncryptDecrypt


' Encrypt the text
Public Shared Function EncryptText(ByVal strText As String) As String
Return Encrypt(strText, "&%#@?,:*")
End Function

'Decrypt the text
Public Shared Function DecryptText(ByVal strText As String) As String
Return Decrypt(strText, "&%#@?,:*")
End Function

'The function used to encrypt the text
Private Shared 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 Shared 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



Kind regards,

Gene
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top