sdlangers Posted November 18, 2003 Posted November 18, 2003 Hi, I'm trying to use the MD5CryptoServiceProvider class to create the hash code for a string. However, it seems i can only return it as a base64 code, and i need the string as hex. How can i convert it? Thanks. Here is my code: Private Function computeMD5(ByVal plainText As String) As String Dim ue As New UnicodeEncoding Dim bytes() As Byte = ue.GetBytes(plainText) Dim md5 As New Security.Cryptography.MD5CryptoServiceProvider Dim hashBytes As Byte() hashBytes = md5.ComputeHash(bytes) ' Convert result into a base64-encoded string. Dim hashString As String hashString = Convert.ToBase64String(hashBytes) ' Return the result Return hashString End Function Quote
Administrators PlausiblyDamp Posted November 18, 2003 Administrators Posted November 18, 2003 So, so close - took me ages to hit on the correct way in MSDN Private Function computeMD5(ByVal plainText As String) As String Dim ue As New UnicodeEncoding Dim bytes() As Byte = ue.GetBytes(plainText) Dim md5 As New Security.Cryptography.MD5CryptoServiceProvider Dim hashBytes As Byte() hashBytes = md5.ComputeHash(bytes) ' Convert result into a base64-encoded string. Dim hashString As String hashString = BitConverter.ToString(hashBytes ).ToLower().Replace("-", "") 'This line will do the trick ' Return the result Return hashString End Function Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
sdlangers Posted November 18, 2003 Author Posted November 18, 2003 Hi, Thanks for your reply. This doesnt seem to be what i need either. Problem is, i'm using it to pass to an interface which checks my md5 encrypted string. this means it has to be the exact implementation as theirs. Theirs is as per the standard MD5 if you look at this site - http://bfl.rctek.com/tools/?tool=hasher it calculates the string correctly. using the above code - we get a different string Thanks for your help. Quote
*Gurus* Derek Stone Posted November 19, 2003 *Gurus* Posted November 19, 2003 Dim byteValue As Byte = 255 Dim stringValue As String = System.Convert.ToString(byteValue, 16) Quote Posting Guidelines
sdlangers Posted November 19, 2003 Author Posted November 19, 2003 Thanks! Private Function computeMD5(ByVal plainText As String) As String Dim ue As New UnicodeEncoding Dim bytes() As Byte = ue.GetBytes(plainText) Dim md5 As New Security.Cryptography.MD5CryptoServiceProvider Dim hashBytes As Byte() hashBytes = md5.ComputeHash(bytes) ' Convert result into a base64-encoded string. Dim hashString As String Dim i As Integer For i = 0 To UBound(hashBytes) hashString += Convert.ToString(hashBytes(i), 16) Next ' Return the result Return hashString End Function That seems to convert it alright.. but now its back to the same problem.. the string i get is different. anyone know what is going on? If you look at the above link and type in a simple string say "test", the encrypted string is: 098f6bcd4621d373cade4e832627b4f6 but the above function returns: c859e2ec7419f59e79d7f1b774bfe6 ???? thanks for your help! Quote
sdlangers Posted November 19, 2003 Author Posted November 19, 2003 Hi All, OK - i solved it - you need to read in the plainText as UTF8 encoding.. The following gives the correct result: Private Function computeMD5(ByVal plainText As String) As String Dim ue As New UTF8Encoding ' THIS WAS THE PROBLEM - needs to be UTF8 Dim bytes() As Byte = ue.GetBytes(plainText) Dim md5 As New Security.Cryptography.MD5CryptoServiceProvider Dim hashBytes() As Byte hashBytes = md5.ComputeHash(bytes) Dim hashString As String Dim i As Integer For i = 0 To UBound(hashBytes) hashString += Convert.ToString(hashBytes(i), 16) Next ' Return the result Return hashString End Function hope this helps someone else. Quote
MadHatter Posted December 4, 2003 Posted December 4, 2003 A while ago I was trying to do the same thing (only in another language). I was hashing credit card numbers, and don't think I would have found it had it not been for the million+ records I was parsing and hashing. one thing you need to add to your hash function is inside your for loop (sorry I don't know VB so I'm taking an honest stab at it :p) Private Function computeMD5(ByVal plainText As String) As String Dim ue As New UTF8Encoding Dim bytes() As Byte = ue.GetBytes(plainText) Dim md5 As New Security.Cryptography.MD5CryptoServiceProvider Dim hashBytes() As Byte hashBytes = md5.ComputeHash(bytes) Dim hashString As String Dim i As Integer For i = 0 To UBound(hashBytes) ' ---- here ---- Dim tempString As String tempString = Convert.ToString(hashBytes(i), 16) If (tempString.Length.Equals(0)) Then hashString += "00" ElseIf (tempString.Length.Equals(1)) Then hashString += "0" + tempString Else hashString += tempString End If ' ---- to here ---- Next Return hashString End Function I found that if the byte sometimes is null (or nothing) and won't be converted to zero's, so you have to do it manually. this fixes the problem.. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.