Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Encrypting string

Status
Not open for further replies.

orion2347

IS-IT--Management
Mar 4, 2002
10
SG
Hi. I'm having this problem regarding hashing the string passed into the function. It keeps giving me the exception "Invalid length for Base-64 char array". How do I calculate the length of the byte array required?

The exception occurs at the line:
"bteInputByteArray = Convert.FromBase64String(strToHash)"

The following is the function's code:

Public Shared Function MD5Hash(ByVal strToHash As String) As String
Dim md5HashSP As New MD5CryptoServiceProvider()
Dim bteInputByteArray(strToHash.Length) As Byte
Dim bteOutputByteArray() As Byte

Try
'Exception occurs here.
bteInputByteArray = Convert.FromBase64String(strToHash)
bteOutputByteArray = md5HashSP.ComputeHash(bteInputByteArray)

Dim encUTF8 As Encoding = Encoding.UTF8

Return encUTF8.GetString(bteOutputByteArray)

Catch e As Exception
Return String.Empty
End Try

End Function


Thanks in advance! :D
 
Is strToHash already in Base64 format? If the Convert.FromBase64String sees punctuation or other invalid characters it'll choke.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top