MD5

Description :


MD5 is the security concept.
We can convet the string into Hexadecimal.

Following is the sample string to be convert into MD5


string md5hash = CreateMD5Hash
("abcdefghijklmnopqrstuvwxyz");



Following is the sample code for convert the MD5
using System;



public string CreateMD5Hash (string input)
{
MD5 Mymd5 = System.Security.Cryptography.MD5.Create();
byte[] MyinputBytes = System.Text.Encoding.ASCII.GetBytes (input);
byte[] MyhashBytes = Mymd5.ComputeHash (MyinputBytes);

StringBuilder Mysb = new StringBuilder();
for (int i = 0; i < MyhashBytes.Length; i++)
{
Mysb.Append (MyhashBytes[i].ToString ("X2"));
}
return Mysb.ToString();
}



Code Explanation

1. Use input string to calculate MD5 hash
2. Convert the byte array to hexadecimal string
3. To force the hex string to lower-case letters instead of
upper-case, use he following line instead:
sb.Append(hashBytes[i].ToString("x2"));


We can use this for FormsAuthentication


FormsAuthentication.HashPasswordForStoringInConfigFile (string, "MD5")




By
Nathan


Comments

Author: Neeraj Kumar SIngh07 Aug 2009 Member Level: Silver   Points : 2

hi,
this is a useful code.
but there is some problem.
it is showing this error-:
"The type or namespace name 'MD5' could not be found (are you missing a using directive or an assembly reference?)"
and where i have to write the last line code.
solve this problem.

thanks & regards,
Neeraj Singh.

Author: Nathan07 Aug 2009 Member Level: Gold   Points : 1

yes, you need to use the following namespace.
using System.Security.Cryptography;

Whereever you are using the formsauthentication you can use that line.

Author: Neeraj Kumar SIngh07 Aug 2009 Member Level: Silver   Points : 1

hello sir,
i used that namespace,but again getting that error.
the 'MD5' is giving error.

please solve my problem.

thanks & regards,
Neeraj Singh.

Author: Abhay Kumar Raut13 Aug 2009 Member Level: Gold   Points : 1

Hi Shunmuganathan m,

good one .

keep it up.

contribute more.

Thanks and Regards
Abhay



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: