Monday, September 27, 2010

Password Encoding and Decoding in ASP.Net

Encoding

Public Function fnBase64Encode(ByVal strPwd As String) As String
Dim byteEncode(strPwd.Length) As Byte
Dim strEncoded As String

byteEncode = System.Text.Encoding.UTF8.GetBytes(strPwd)
strEncoded = Convert.ToBase64String(byteEncode)

Return strEncoded
End Function


Decoding

Public Function fnBase64Decode(ByVal strPwd As String) As String
Dim encoder As New System.Text.UTF8Encoding()
Dim utf8Decode As System.Text.Decoder
Dim byteDecode() As Byte
Dim intCharCnt As Integer
Dim strDecoded As String

utf8Decode = encoder.GetDecoder()
byteDecode = Convert.FromBase64String(strPwd)
intCharCnt = utf8Decode.GetCharCount(byteDecode, 0, byteDecode.Length)

Dim charDecoded(intCharCnt) As Char

utf8Decode.GetChars(byteDecode, 0, byteDecode.Length, charDecoded, 0)
strDecoded = New String(charDecoded)

Return strDecoded
End Function

No comments: