XHanch Studio Log in | Register | Cart

Forum

[Function] MD5 encr...
 
Notifications
Clear all

[Function] MD5 encryption in C# .NET

1 Posts
1 Users
0 Likes
1,473 Views
XHanch
(@xhanch-alt)
Posts: 2105
Member Admin
Topic starter
 

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

//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.ToString ("X2"));
        // You may use this alternative command to have the hex string
        // in lower-case letters instead of upper-case
        // str.Append(hashBytes.ToString("x2"));
    }
    return str.ToString();
}
 
Posted : 05/08/2012 8:45 am
Share: