[Function] MD5 encryption in C# .NET

Started by Xhanch Studio, August 05, 2012, 02:45:09 AM

previous topic - next topic
Go Down

Xhanch Studio

Do you need to hash secret values (ex:passwords) with md5 algorithm in C#.NET? Actually, .NET framework has provided this cryptography, but, you cannot use it directly like what you can do the same thing in PHP. In PHP, you just need to call md5 function. In C#.NET, you have to define the function yourself. For your information, the encrypted values will not be able to be decrypted because md5 is a one-way encryption.


Source code

Code Select
//By    : Xhanch Studio
//URL   : http://xhanch.com/
//First thing first, you have to include these references
using System;
using System.Text;
using System.Security.Cryptography;
public string md5 (string plainText){
    MD5 enc = MD5.Create();
    byte[] rescBytes = Encoding.ASCII.GetBytes (plainText);
    byte[] hashBytes  = enc.ComputeHash (rescBytes);
    StringBuilder str = new StringBuilder();
    for (int i = 0; i < hashBytes.Length; i++){
        str.Append (hashBytes[i].ToString ("X2"));
        // You may use this alternative command to have the hex string
        // in lower-case letters instead of upper-case
        // str.Append(hashBytes[i].ToString("x2"));
    }
    return str.ToString();
}
Best Regards,
Susanto B.Sc
----------------------------------------------------------------------------
Web development services, WordPress plugin and theme development, PSD to XHTML conversion - http://xhanch.com
Read free manga online - http://authrone.com

Go Up