Rick van den Bosch - Blog

... on .NET, software architecture, software development and whatnot

Recent Posts

Tags

News

  • Live space

    Photo blog

    Follow me at twitter

    Rick  van den Bosch

    LinkedIn profile

    Add to Technorati Favorites

    Disclaimer
    The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

Community

Email Notifications

Blogs I read

Interesting links

Archives

HOWTO: Encode a password using MD5 in C# (or: howto calculate the MD5 hash for a string)

The following method returns the MD5 hash for any given string. For instance for a password. It might be of some assistance when you're trying to validate user credentials but you don't want to store the password readable in the database.

For this method, you'll need the following using statements:

using System;
using
System.Text;
using
System.Security.Cryptography;

...

public
string EncodePassword(string originalPassword)
{
  //Declarations
  Byte[] originalBytes;
  Byte[] encodedBytes;
  MD5 md5;

  //Instantiate MD5CryptoServiceProvider, get bytes for original password and compute hash (encoded password)
  md5 = new
MD5CryptoServiceProvider();
  originalBytes = ASCIIEncoding.Default.GetBytes(originalPassword);
  encodedBytes = md5.ComputeHash(originalBytes);

  //Convert encoded bytes back to a 'readable' string
  return
BitConverter.ToString(encodedBytes);
}

Hmmm... I seem to write comments when I'm trying to explain something ;)

Comments

Rick van den Bosch said:

Isn't it a lot easier to use the HashPasswordForStoringInConfigFile method of the FormsAuthentication class?
# May 18, 2005 4:20 PM

Rick van den Bosch said:

That method does do the same, so it seems...
# May 27, 2005 7:41 AM

joe said:

Thanks, I needed a way to get the byte[] md5 hash of a string password...worked great
# August 22, 2006 2:05 PM

Vlad said:

Your site is realy very interesting.
# August 23, 2006 5:56 AM

kennedy & kate said:

Nice! I'lll use it on my site. -kak http://www.kennedyandkate.com
# October 6, 2006 5:33 PM

Kenshi said:

As a side note: if you want to return a string without the dashes in them, replace the last statement with : return Regex.Replace(BitConverter.ToString(encodedBytes), "-", "");
# November 7, 2006 3:51 PM

RICK said:

I have a problem, the password MD5 in my server OpenLDAP is saved with {MD5}o5c3WBNN50D/iYO5RJxcvw==, but to compare with the password in my aplication ASP.net the string is different, something so : j+ZGDCZGqIQtr/6+ZDxa4w==, somebody would help me in this case ?

# April 5, 2007 2:55 PM

Aaron said:

Hi, your code was very useful. however, i would like to enquire something. Is there any way the code your code for MD5 encrpytion be modified to produce an encrypted ouput of this format?

aTXuyYqHUUudx0Km2bsZKlH4/WM=

WEo/tELTiM0dVGGxgsF+5XmDr6s=

koOQgZFIppxqOVFsmWKiOSMJ3RY=

The password i have in my databse are of this format, and i am trying to find out how to insert new passwords as well as decrpyt passwords of this format.

I thank you in advance for your help. Thank you

# April 21, 2007 7:57 PM

mousemee said:

HI. the source code u provided for MD5 encrption was very useful. however, i would like to enquire if it was possible to make modifications to your coding such that the ouput encrpyted passwords were of this format.

SZCi/864aZZbnCv6hqp+FV/cUPw=

This is so as i am required to encode passwords as well as decode passwords of this format. Thank you very much in advance.

# April 22, 2007 12:58 AM

mousemee said:

Hi, your code showed how to encode a string into a hased string. How do i do the reverse? input a hashed string and the original password is outputted?

# April 24, 2007 5:47 AM

John Doe said:

TO: mousemee

You don't too that. You need a hashed-string to string database. There's LOTS of combinations that generate the same hash.

# April 24, 2007 8:24 AM

mousemee said:

Hi. is anybody familiar with using membership. crete user web service? I need a web service that adds in new users to database which consists of several tables. Thank you

# April 24, 2007 10:01 PM

Ly said:

Please tell me, how decode a password using MD5 in c#/

# May 16, 2007 2:27 AM

Rick van den Bosch said:

@ly: MD5 hashing only works one way. You can hash an entered string to see if its hash mathes the one in the database (or wherever). It is not possible to recover a password based on its MD5 hash.

# May 22, 2007 1:53 PM

Robajz said:

It is possible to get a collision for the computed hash with brute force possibly optimized to abandon invalid iteration branches. But that is a whole science and I am not a scientist in that field.

Remember MD5 is not considered safe for storing passwords, or security any more. Use some stronger hash instead (SHA). However it is still good enough to hash file to check if there is no mod.

Check out wikipedia: http://en.wikipedia.org/wiki/MD5

# May 24, 2007 8:22 AM

Nurchi said:

I seriously doubt you can easily decode passwords encoded using MD5 (it is possible, but it may take you 10 million years...)

Why not just encrypt the entered password and then compare it against the record in the database?

Or am I missing something?

# August 7, 2007 7:42 PM

Dan said:

It is now possible to "decode" passwords that use MD5 (It won't take you 10 million years either).

Computers are powerful enough now that you can use a rainbow table (a table for MD5 hashes for every combination of letters, numbers, and sometimes symbols, up to 14 characters) to check a hash against a rainbow table relatively easily. There's a program (the name escapes me at the moment) that will actually do this to crack windows passwords given their hash.

That is why it is important to salt your passwords before hashing. For example instead of this:

string hashedPass = MD5(password);

you would want to use this:

string hashedPass = MD5(aComplicatedSaltString + password);

Typically, a good salt would be the user's UserName and a programmer defined salt concatenated together.

That makes it harder to decode because then a hacker would have to add the salt when checking every password which would take the 10,000 years mentioned above (probably less than that, but still futile to attempt).

# September 23, 2007 6:32 PM

Mihai said:

Hi guys,

I'm using MD5 to encypt my site passwords but I want to implement a "Forget Password" feature.

Is it possible to decrypt the array of bytes to what the user had entered initialy?

Thanks.

# October 4, 2007 7:26 PM

Jahedur Rahman said:

Hi

Thanks for this help.

# January 9, 2008 2:10 PM

Jeremy Ault said:

If you're asking is it possible to simply reverse a hash the answer is NO.

A hash is a one-way function. It's simply a fixed-length "checksum" of sorts that is created based on the data presented. There is no reverse algorithm. That's the whole point.

The problem with trying to decode an MD5 hash is that it doesn't matter whether the message was three letters long or 500 pages long, the resulting hash it only 128 bits or 32 characters.

The most common way to crack MD5 hashed passwords is to use a program to systematically hash a dictionary or list of words (or random combinations of letters and numbers) until the resultant hash equals the hash you want to crack.

It's called a brute-force attack because you basically forcibly try every combination until you crack it.

If the password you are trying to crack is a simple or single word found in the dictionary, you can crack it in a matter of minutes or even seconds. If the password is long and complex, this method is typically fruitless.

That's why it is important to enforce complex password requirements of at least 8 characters, upper and lower case with at least one symbol (*&^, etc.

# January 22, 2008 4:33 PM

John Malcolm said:

Why doesn't this work in dot.net within a apxs file?  Where do I put the with statements?  In the script tag or within the <%@ tag?

# February 8, 2008 10:48 AM

mani said:

can u please provide the code for decoding also...

encoding i did but i want to retrieve the encoded string from the database n compare it with the password field when the user logs in again for this i need to decode the string so tht it comes to its original value n then i can compare it with the password tht user enters...

reply asap

thanks

# April 22, 2008 3:07 PM

Simucal said:

For everyone asking for how to decode hashes, YOU CAN'T.  That is the POINT.  You can hash a string and compare it to your already computed hash, that is IT.  Read a wikipedia on computer hashes for gods sake.

Also, to the original poster, Rick van den Bosch... shame on you for using ASCIIEncoding.Default.GetBytes().  You do realize that would seriously restrict the usefulness of your method to working only in languages that ASCII has the character sets for, right?

I suggest you read this article entitled:

The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!)

www.joelonsoftware.com/.../Unicode.html

# May 29, 2008 2:14 AM

Chuck said:

mani, you do not "decode" a hashed password. It's impossible.  Hashing is not encryption.   Once hashed, you cannot restore it to the original password.

To test to see if the user entered the right password, you hash the password the user enters on the login screen, and compare the result to the password-hash stored in the database.

The benefit here is that even someone who can see into the database cannot know what the users password is.

The downside is that if a user looses their password, you cannot look it up to tell them.  You need to have the system generate a temp password and mail it to them.

# June 3, 2008 1:07 AM

sime said:

Thank you man. It work exactly as I needed.

# June 5, 2008 11:40 PM

ScRePt said:

What is the length of the generated string ?

Is it fixed?

Somebody should know the length (at least the max) to

store this to database.

# July 1, 2008 8:12 PM

MUFMS said:

Thank u that was helpfull.

# July 29, 2008 1:48 PM

madhava reddy said:

i created a c# web form contains 2labels,2textboxes and button(ok).when click on ok in should verify the userid and  password fron sqlserver if the userid is correct the permit to go for next page.

for this i need code for that

# August 2, 2008 11:32 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)