473,320 Members | 2,094 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

md5 hash not working?

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=="
Nov 20 '05 #1
5 3830
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
--

------------------------------
If work were a good thing, the boss would take it all from you

------------------------------

"HamuNaptra" <ha********@pandora.be.NOSPAM> wrote in message
news:hy******************@phobos.telenet-ops.be...
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=="

Nov 20 '05 #2
So where is the problem?
What whould you espected to get?


maybe try and read my post??

for short:
eg. "test" = "098f6bcd4621d373cade4e832627b4f6" when i call the function i get this:

"yAWeLsdBn1kOedfxt3S/5g=="

Nov 20 '05 #3
"HamuNaptra" <ha********@pandora.be.NOSPAM> wrote
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[i].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
Nov 20 '05 #4
On Tue, 04 Nov 2003 13:09:33 GMT, HamuNaptra wrote:
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
--
Chris

To send me an E-mail, remove the underscores and lunchmeat from my E-Mail
address.
Nov 20 '05 #5

End Function


tnx
Nov 20 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Murali | last post by:
I have a requirement where I have to use two unsigned ints as a key in a STL hash map. A couple of ways to do this is 1. create a struct with two unsigned ints and use that as key (write my own...
22
by: VK | last post by:
A while ago I proposed to update info in the group FAQ section, but I dropped the discussion using the approach "No matter what color the cat is as long as it still hounts the mice". Over the last...
2
by: Bryan Olson | last post by:
The current Python standard library provides two cryptographic hash functions: MD5 and SHA-1 . The authors of MD5 originally stated: It is conjectured that it is computationally infeasible to...
2
by: Ravi | last post by:
Hi, I am working on a winform app. I need to use an object which can store some information(key/value pairs) and also can be acessed by multiple threads(read/write). From what I heard Hash table...
10
by: Qiangning Hong | last post by:
I'm writing a spider. I have millions of urls in a table (mysql) to check if a url has already been fetched. To check fast, I am considering to add a "hash" column in the table, make it a unique...
12
by: shaanxxx | last post by:
I wanted to write hash function float or double. Any suggestion would be appreciated.
3
by: keithl | last post by:
Hi (again !) I posted a question yesterday just for info on hashrefs which I have read and it makes sense. Putting this into proactive though has now toally confused me ! I have an XML file...
139
by: ravi | last post by:
Hi can anybody tell me that which ds will be best suited to implement a hash table in C/C++ thanx. in advanced
7
numberwhun
by: numberwhun | last post by:
Hello all! I am working on trying to figure out how to print out a hash of hashes. How the hash in the code is defined below is very similar to how the hash is defined in the script I am...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.