10 Replies - 571 Views - Last Post: 30 July 2015 - 04:09 PM Rate Topic: -----

#1 O'Niel   User is offline

  • D.I.C Regular

Reputation: 15
  • View blog
  • Posts: 464
  • Joined: 13-September 15

Problems converting string to MD5

Posted 30 July 2015 - 04:30 AM

Hello

I need help converting a string to MD5, I've tried several tutorials, but none worked.
First I didn't got a result, then I edited the code a bit, now I only get a few weird characters as output.

This is my code:
	public static string MD5encrypt(string inputString) {
		byte[] tmpSource, tmpHash;
		tmpSource = ASCIIEncoding.ASCII.GetBytes(inputString);
		tmpHash = new MD5CryptoServiceProvider().ComputeHash(tmpSource);
		string x = System.Text.Encoding.UTF8.GetString(tmpHash);
		return x;
	}


When I input 'hello', this is the output:
��S�a��'���x�

Is this because of UTF8? Should it be ASCII?

I need this, to save data to a CSV-database. This is the last problem I encounter.

Thanks for reading,
Niely

Is This A Good Question/Topic? 0
  • +

Replies To: Problems converting string to MD5

#2 Momerath   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1021
  • View blog
  • Posts: 2,463
  • Joined: 04-October 09

Re: Problems converting string to MD5

Posted 30 July 2015 - 06:22 AM

You get that because the MD5 code isn't a string, it's a set of hex digits. You'll need to convert. You can tell this because ComputeHash returns a byte array, not a string.

This post has been edited by Momerath: 30 July 2015 - 06:24 AM

Was This Post Helpful? 0
  • +
  • -

#3 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5106
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: Problems converting string to MD5

Posted 30 July 2015 - 08:01 AM

I'm assuming you want to print the Base64 encoded version of the hash, right? When you MD5 something and see a readable hash, that's the hash's bytes in base64 representation. IDK what I was thinking. It's just the bytes being printed as hex.

Replace the "GetString" method with this: https://msdn.microso...(v=vs.110).aspx
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7053
  • View blog
  • Posts: 23,977
  • Joined: 05-May 12

Re: Problems converting string to MD5

Posted 30 July 2015 - 08:19 AM

As a side note. You do not convert a string to MD5. You can compute the MD5 hash of a string. The hash was originally designed so that it would be impossible or at least very expensive to figure out what the original string was. Even with the information that MD5 had been broken in 2004, it was only broken in the way that it was possible to create another string that would generate the same MD5 hash as your string. As far as I know, nobody has yet succeeded in retrieving your original string.
Was This Post Helpful? 0
  • +
  • -

#5 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5106
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: Problems converting string to MD5

Posted 30 July 2015 - 09:14 AM

Quote

The hash was originally designed so that it would be impossible or at least very expensive to figure out what the original string was


That's really the way every cryptographic hash is designed. Realistically, there's very little way to recover original hashed information. It's like lossy compression; you can't regain the lost information. Hash algorithms have a fixed output size for an arbitrary input size. That means that by definition, information isn't just encrypted, it's lost.

MD5's weakness is its potential for "collisions". For those who don't know, hashes by definition have a limited output "space" (since they have a fixed output size, that defines the total number of potential outputs. But since they can take input of an arbitrary length, that means that at least some inputs must map to the same outputs.

A well designed hash will have an even distribution in it's output space. A poorly designed hash will have clusters. MD5 has a weakness that makes it pretty easy to derive an input string that will match a particular hash. But from the hash's perspective, it doesn't matter if it isn't the original string or not; as long as it hashes to the hash, it's a match.

MD5 is a weak hash, and really shouldn't be used for much. SHA1 is better, but there are even better options. For passwords, options like bcrypt and scrypt are way better.
Was This Post Helpful? 0
  • +
  • -

#6 O'Niel   User is offline

  • D.I.C Regular

Reputation: 15
  • View blog
  • Posts: 464
  • Joined: 13-September 15

Re: Problems converting string to MD5

Posted 30 July 2015 - 02:41 PM

Actually, I don't want the Base64 version.
If I generate it using an online MD5 generator, this is the result of 'Hello': 8b1a9953c4611296a827abf8c47804d7
A real MD5 hash, how do I get that exact output also in C#?

Thanks for the extra info though.
Was This Post Helpful? 0
  • +
  • -

#7 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7053
  • View blog
  • Posts: 23,977
  • Joined: 05-May 12

Re: Problems converting string to MD5

Posted 30 July 2015 - 02:55 PM

Brute force approach would be to iterate over the byte array and print each each byte value in hex. I'm quite sure there are other helper API's in the Framework to help you do this.
Was This Post Helpful? 0
  • +
  • -

#8 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5106
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: Problems converting string to MD5

Posted 30 July 2015 - 03:18 PM

There actually aren't any "simple" helpers that dump a byte array as a hex string, at least not in the format he's looking for. BitConverter.ToString comes very, very close, but puts a '-' between each byte.

Here's two ways to do it that I got, from messing around with it in LINQPad:

var algo = System.Security.Cryptography.MD5.Create();
var bytes = System.Text.Encoding.ASCII.GetBytes("Hello");
var output = algo.ComputeHash(bytes);
var outputHexString = output.Select(b => b.ToString("X2")).Aggregate((w, n) => w + n);
var outputHexString2 = BitConverter.ToString(output).Replace("-", string.Empty);


Another more-obvious but less one-line is using a loop and a StringBuilder; that's the "Brute Force" method described by Skydiver. Of course, so is the first way I chose too. It abstracts the loop(s) away from you, but they're still happening one way or another.

Side note: since the output size will always be 16 bytes (32 characters in hex representation), you don't really have to worry about the efficiency of using LINQ or Loops, unless you're going to be hammering this as hard as you can repeatedly.
Was This Post Helpful? 0
  • +
  • -

#9 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7053
  • View blog
  • Posts: 23,977
  • Joined: 05-May 12

Re: Problems converting string to MD5

Posted 30 July 2015 - 03:44 PM

Check out Guid.ToString("N").
Was This Post Helpful? 0
  • +
  • -

#10 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5106
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: Problems converting string to MD5

Posted 30 July 2015 - 04:06 PM

Neat

Spoiler


Seriously though, I didn't even realize Guid had a format parameter in its ToString().
Was This Post Helpful? 0
  • +
  • -

#11 O'Niel   User is offline

  • D.I.C Regular

Reputation: 15
  • View blog
  • Posts: 464
  • Joined: 13-September 15

Re: Problems converting string to MD5

Posted 30 July 2015 - 04:09 PM

View PostCurtis Rutland, on 30 July 2015 - 03:18 PM, said:

There actually aren't any "simple" helpers that dump a byte array as a hex string, at least not in the format he's looking for. BitConverter.ToString comes very, very close, but puts a '-' between each byte.

Here's two ways to do it that I got, from messing around with it in LINQPad:

var algo = System.Security.Cryptography.MD5.Create();
var bytes = System.Text.Encoding.ASCII.GetBytes("Hello");
var output = algo.ComputeHash(bytes);
var outputHexString = output.Select(b => b.ToString("X2")).Aggregate((w, n) => w + n);
var outputHexString2 = BitConverter.ToString(output).Replace("-", string.Empty);


Another more-obvious but less one-line is using a loop and a StringBuilder; that's the "Brute Force" method described by Skydiver. Of course, so is the first way I chose too. It abstracts the loop(s) away from you, but they're still happening one way or another.

Side note: since the output size will always be 16 bytes (32 characters in hex representation), you don't really have to worry about the efficiency of using LINQ or Loops, unless you're going to be hammering this as hard as you can repeatedly.


Thanks a lot! BitConverter.ToString() was indeed what I was looking for! Everything is working fine now.

The final working code:
	public static string MD5encrypt(string inputString) {
		byte[] tmpSource, tmpHash;
		tmpSource = ASCIIEncoding.ASCII.GetBytes(inputString);
		tmpHash = new MD5CryptoServiceProvider().ComputeHash(tmpSource);
		string x = BitConverter.ToString(tmpHash).Replace("-", "").ToLower();
		return x;
	}



Thanks a lot for the help all!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1