Home > C#.NET > MD5 Encryption In C#.NET

MD5 Encryption In C#.NET

February 28th, 2009 admin Leave a comment Go to comments

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


[csharp]//By : Isusx Programming Corner
//URL : http://isusx.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();
}[/csharp]

Copyright Note

This script is free to use and can be modified as your wish.

Related Links
About MD5
About C# .NET
C#.NET Programmer Reference

If you like this code/script, perhaps you would kindly treat me a drink. Any number is welcome. Thanks :)

Related Article

  1. No comments yet.
  1. No trackbacks yet.