473,320 Members | 1,804 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 - Converting Result From Byte[] Back To String

I wrote the following to do an MD5 hash. However, I have a problem (I
think) with the conversion from the Byte[] MD5 hash back to string.

Watching this through the debugger it appears as if the MD5 is
computing the right Byte[] for the hash when compared to other MD5 hash
generators online. However, when I attempt to convert it back to tring
using the line
String outputData = textConverter.GetString( result ) ;

I essentially get garbage. Any thoughts on what I am doing wrong?
Does the MD5 hash not generate a UTF8 byte array?

Amy.

UTF8Encoding textConverter = new UTF8Encoding();
String inputData = "" ;

Console.Write( "Enter a string: " ) ;
inputData = Console.ReadLine() ;

MD5 md5 = new MD5CryptoServiceProvider();

byte[] result = md5.ComputeHash( textConverter.GetBytes( inputData ) );
String outputData = textConverter.GetString( result ) ;

Console.WriteLine( "MD5 Hash: " + outputData ) ;
Console.ReadLine() ;

Nov 17 '05 #1
7 16968
string outputData = Convert.ToBase64String(result);

byte[] backResult = Convert.FromBase64String(outputData);
Nov 17 '05 #2
Would you mind expanding on this? All you did was convert the byte
array it to a base64 string and than back into a byte[]?

Amy

Nov 17 '05 #3
<dl*****@gmail.com> wrote:
I wrote the following to do an MD5 hash. However, I have a problem (I
think) with the conversion from the Byte[] MD5 hash back to string.

Watching this through the debugger it appears as if the MD5 is
computing the right Byte[] for the hash when compared to other MD5 hash
generators online. However, when I attempt to convert it back to tring
using the line
String outputData = textConverter.GetString( result ) ;

I essentially get garbage. Any thoughts on what I am doing wrong?
Does the MD5 hash not generate a UTF8 byte array?


No. It generates the MD5 bytes. Why would it decide to encode the bytes
in UTF-8? If it were converting the data to text, it would just return
it as a string.

The result of hashing is arbitrary binary data, and it should be
treated as such. Base64 encoding it is a fine way to get it into a text
form without the risk of any loss of data.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 17 '05 #4
Jon,

Thank you for your reply, but I seem to be missing something. So far
what I understand is that when I convert it to a MD5 hash it generates
a byte array of the MD5 hash. I should be able to convert that byte
array into a string to view it

for example: MD5 hashing the string "C#" is
"d7efa19fbe7d3972fd5adb6024223d74".

Now it was suggested to convert the MD5 byte array into a base64 string
which I can do, but that is a base64 string of the hash and not really
what I am looking for as I am looking for the MD5 hash in a string
format (like above and not in a base64 format)?

Playing around I found that
BitConverter.ToString( result ) ;
works in doing what I need done - but why doesn't
outputData = textConverter.GetString( result ) ;?

Thank
Amy

Nov 17 '05 #5
yes all can be done is converting to base64 string. what did you expected?
Nov 17 '05 #6
<dl*****@gmail.com> wrote:
Thank you for your reply, but I seem to be missing something. So far
what I understand is that when I convert it to a MD5 hash it generates
a byte array of the MD5 hash.
Well, the MD5 hash *is* a byte array. It's binary data - it's not a
string.
I should be able to convert that byte
array into a string to view it

for example: MD5 hashing the string "C#" is
"d7efa19fbe7d3972fd5adb6024223d74".
No, it's the binary data which that string is the hex-encoded version
of. Just as you could load up a JPEG into a hex editor and post the
hex-encoded data, that doesn't mean the data is inherently text.
Now it was suggested to convert the MD5 byte array into a base64 string
which I can do, but that is a base64 string of the hash and not really
what I am looking for as I am looking for the MD5 hash in a string
format (like above and not in a base64 format)?
Base64 *is* a string format. It happens to be in base64 instead of
base16 (which is what you posted above), that's all.
Playing around I found that
BitConverter.ToString( result ) ;
works in doing what I need done - but why doesn't
outputData = textConverter.GetString( result ) ;?


Because that's treating the arbitrary binary data as if it were the
results of applying the UTF-8 encoding to text.

I suspect you have some fundamental misunderstandings about what
encodings are about. See
http://www.pobox.com/~skeet/csharp/unicode.html for a bit of an
introduction.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 17 '05 #7
dl*****@gmail.com wrote:
Thank you for your reply, but I seem to be missing something. So far
what I understand is that when I convert it to a MD5 hash it generates
a byte array of the MD5 hash. I should be able to convert that byte
array into a string to view it

for example: MD5 hashing the string "C#" is
"d7efa19fbe7d3972fd5adb6024223d74".
No, actually, the MD5 hashing is the 128 bit binary data which, when
converted to a hexidecimal display, is the string you indicate. Basically
the hash (any hash) just generates some kind of binary data through a set of
mathematical operations on the underlying data that's being examined. For
example, it doesn't actually look at the text "C#" but at the binary
representation of that string, so a different encoding of C# (for example,
in UTF-16) will yield a different MD5 hash result.
Now it was suggested to convert the MD5 byte array into a base64
string which I can do, but that is a base64 string of the hash and
not really what I am looking for as I am looking for the MD5 hash in
a string format (like above and not in a base64 format)?
Actually, the base64 transformation turns a set of binary data into the
string representation you indicate above, so, in fact, it's exactly what you
want.
Playing around I found that
BitConverter.ToString( result ) ;
works in doing what I need done - but why doesn't
outputData = textConverter.GetString( result ) ;?


Because Text Converter attempts to transform a set of binary data into a
textual representation assuming a specific character code set. For example,
in ASCII, the 'A' character is (off the top of my head) 65 in decimal, and
41 in hexidecimal. So, conversely, the "39" in your string above, would
convert back to (again, off the top of my head) the ? symbol, at least with
the ASCII set (or a bunch of other sets which also map similarly). So,
thus, you get garbage in your translation.

To put it a different way, what you want is the hexidecimal representation
of your binary (byte) data, not the text representation of your binary data.
Thus, BitConverter and Base64 do what you want, but Text Converter does not.

Hope that helps.

--
Reginald Blue
"I have always wished that my computer would be as easy to use as my
telephone. My wish has come true. I no longer know how to use my
telephone."
- Bjarne Stroustrup (originator of C++) [quoted at the 2003
International Conference on Intelligent User Interfaces]
Nov 17 '05 #8

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

Similar topics

47
by: VK | last post by:
Or why I just did myArray = "Computers" but myArray.length is showing 0. What a hey? There is a new trend to treat arrays and hashes as they were some variations of the same thing. But they...
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...
4
by: flipdog | last post by:
Hello all, I didn't know there is a thread on hash function started in this newsgroup so reposted my posting from another group. Hope I can have some feedbacks. I am new to hash table. I came...
5
by: HamuNaptra | last post by:
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...
8
by: moondaddy | last post by:
I need to convert a byte array to a string and pass it as a parameter in a URL and then convert it back to the original byte array. However, its getting scrambled in the conversion. In short,...
5
by: Gary | last post by:
I found with Asp.Net, you can use this function to very easily create an MD5 hash: System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile( created.ToUpper(),"MD5"); Is there...
5
by: andrewcw | last post by:
I have a VB 5 module that duplicates the FCIV.exe from Microsoft. I need to move an application forward to C#, but the samples for MD5 hash using the framework I tried gave different hashes. ...
9
by: Jeremy Kitchen | last post by:
Are there any library functions that can help me to do this? If necessary I can convert the string to a byte array. I don't want to have to write my own Hex conversion if it isn't necessary. ...
6
by: shapper | last post by:
Hello, How can I hash a string in C#? I suppose I should use MD5 ... at least is what is used in ASP.NET membership. I need to has a string and compare it to an already hashed string. ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
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...
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)...
0
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
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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.