So the method actually looks like this:
public static string MD5(string password) {
byte[] textBytes = System.Text.Encoding.Default.GetBytes(password);
try {
System.Security.Cryptography.MD5CryptoServiceProvider cryptHandler;
cryptHandler = new System.Security.Cryptography.MD5CryptoServiceProvider();
byte[] hash = cryptHandler.ComputeHash (textBytes);
string ret = "";
foreach (byte a in hash) {
if (a<16)
ret += "0" + a.ToString ("x");
else
ret += a.ToString ("x");
}
return ret ;
}
catch {
throw;
}
}
And here is an example of a full application which uses it. Cut and paste all of the source code below into your editor, compile and run.
using System;
using System.Text;
using System.Security.Cryptography;
class md5demo {
static void Main(string[] args) {
string plain, hashed;
plain = "secretword";
hashed = MD5(plain);
Console.WriteLine("plain version is "+plain);
Console.WriteLine("hashed version is "+hashed);
}
public static string MD5(string password) {
byte[] textBytes = System.Text.Encoding.Default.GetBytes(password);
try {
System.Security.Cryptography.MD5CryptoServiceProvider cryptHandler;
cryptHandler = new System.Security.Cryptography.MD5CryptoServiceProvider();
byte[] hash = cryptHandler.ComputeHash (textBytes);
string ret = "";
foreach (byte a in hash) {
if (a<16)
ret += "0" + a.ToString ("x");
else
ret += a.ToString ("x");
}
return ret ;
}
catch {
throw;
}
}
}
I hope you find that useful. If you would like to see how to do that in Java, then please follow the java md5 example.christo