md5 hash not working?

H

HamuNaptra

Hi im trying to get a hash from a string and conert it back to a string

eg. "test" = "098f6bcd4621d373cade4e832627b4f6"

my function:


Private Function GenerateHash(ByVal SourceText As String) As String

Dim Ue As New UnicodeEncoding()
Dim ByteSourceText() As Byte = Ue.GetBytes(SourceText)
Dim Md5 As New MD5CryptoServiceProvider()
Dim ByteHash() As Byte = Md5.ComputeHash(ByteSourceText)

Return Encoding.ASCII.GetString(ByteHash)

End Function

when i call the function i get this:

"yAWeLsdBn1kOedfxt3S/5g=="
 
C

Crirus

Hi
So where is the problem?
What whould you espected to get?
MD5 will encrypt irreversible your string ..
Possible use is password hiding pourpose...to store a obfuscated value not a
readable password.

Ceers,
Crirus
 
P

Pieter Philippaerts

HamuNaptra said:
Hi im trying to get a hash from a string and conert it back to a string
eg. "test" = "098f6bcd4621d373cade4e832627b4f6"
my function:

Your code is wrong. First, you're using a unicode encoding to convert "test"
to a byte string; you should use an ASCII encoding or you will never get the
hash result you mention above.
Second, the hash above is in hexadecimal format and the result of your
function appears to be in Base64 format. Here's a small method [in C#] to
convert your bytes to a hexadecimal string:

private static string BytesToHex(byte[] hash) {
StringBuilder sb = new StringBuilder(hash.Length * 2);
for(int i = 0; i < hash.Length; i++) {
sb.Append(hash.ToString("X2"));
}
return sb.ToString();
}

It shouldn't be too hard to port to VB.NET.

Regards,
Pieter Philippaerts
Managed SSL/TLS: http://www.mentalis.org/go.php?sl
 
C

Chris Dunaway

Hi im trying to get a hash from a string and conert it back to a string

eg. "test" = "098f6bcd4621d373cade4e832627b4f6"

my function:


Private Function GenerateHash(ByVal SourceText As String) As String

Dim Ue As New UnicodeEncoding()
Dim ByteSourceText() As Byte = Ue.GetBytes(SourceText)
Dim Md5 As New MD5CryptoServiceProvider()
Dim ByteHash() As Byte = Md5.ComputeHash(ByteSourceText)

Return Encoding.ASCII.GetString(ByteHash)

End Function

when i call the function i get this:

"yAWeLsdBn1kOedfxt3S/5g=="

I modified your function as follows and got the result you expected:

Private Function GenerateHash(ByVal SourceText As String) As String

'Dim Ue As New UnicodeEncoding
Dim ByteSourceText() As Byte = Encoding.Default.GetBytes(SourceText)

'Dim ByteSourceText() As Byte = Ue.GetBytes(SourceText)
Dim Md5 As New MD5CryptoServiceProvider

Dim ByteHash() As Byte = Md5.ComputeHash(ByteSourceText)

'Convert the Byte array to a hex string
Dim sb As New StringBuilder(32)
For x As Integer = 0 To ByteHash.Length - 1
If ByteHash(x) < 16 Then
sb.Append("0")
End If
sb.Append(Convert.ToString(ByteHash(x), 16))
Next

Return sb.ToString

End Function
 

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

Top