473,687 Members | 3,538 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.G etString( 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.ReadLin e() ;

MD5 md5 = new MD5CryptoServic eProvider();

byte[] result = md5.ComputeHash ( textConverter.G etBytes( inputData ) );
String outputData = textConverter.G etString( result ) ;

Console.WriteLi ne( "MD5 Hash: " + outputData ) ;
Console.ReadLin e() ;

Nov 17 '05 #1
7 17050
string outputData = Convert.ToBase6 4String(result) ;

byte[] backResult = Convert.FromBas e64String(outpu tData);
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.G etString( 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.co m>
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
"d7efa19fbe7d39 72fd5adb6024223 d74".

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.To String( result ) ;
works in doing what I need done - but why doesn't
outputData = textConverter.G etString( 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
"d7efa19fbe7d39 72fd5adb6024223 d74".
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.To String( result ) ;
works in doing what I need done - but why doesn't
outputData = textConverter.G etString( 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 misunderstandin gs about what
encodings are about. See
http://www.pobox.com/~skeet/csharp/unicode.html for a bit of an
introduction.

--
Jon Skeet - <sk***@pobox.co m>
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.c om 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
"d7efa19fbe7d39 72fd5adb6024223 d74".
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.To String( result ) ;
works in doing what I need done - but why doesn't
outputData = textConverter.G etString( 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
5066
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 are not at all. If you are doing *array", then you have to use only integer values for array index, as it was since ALGOL.
2
3782
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 produce two messages having the same message digest. That conjecture is false, as demonstrated by Wang, Feng, Lai and Yu in 2004 . Just recently, Wang, Yu, and Lin showed a short- cut solution for finding collisions in SHA-1 . Their result
4
7249
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 across a very well presented tutorial web page wrt hash table. In its content, it listed a number of hash functions which the web master(?) quoted from other web sites. So no explainations for these hash functions and I failed to understand a...
5
3851
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 String) As String Dim Ue As New UnicodeEncoding()
8
4193
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, here's the code: ====================================== Dim textConverter As New ASCIIEncoding Dim sParam As String = "This is my cool param" Dim bytParam() As Byte 'load the byte array here...
5
2628
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 a similar one for Windows programs? I like that function because you don't have to convert to bytes. The variable created is simply a string which makes life easy. :) I tried using it in a windows program but it didn't like it :? --
5
3498
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. What do I feed the framework and how do I get the same values ?? The following code DOES NOT GIVE THE FCIV value: public string getFileHash(string filePath) { string retVal = "";
9
9726
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. Thanks for any help Jeremy Kitchen
6
3475
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. Thanks, Miguel
0
8527
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8947
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8784
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6453
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5806
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4543
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2964
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2214
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
1948
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.