On my Default.aspx page i have taken following controls:
1.) Label lblMessage
2.) TextBox txtPalinStr
3.) Button btnEncrypt
4.) Label lblEnryptedString
5.) TextBox txtcheck
6.) Button btncheck onclick="btncheck_Click"
7.) Label lblcheck
This is my Default.aspx.cs page :
using System;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using MD5Encrypt;
public partial class _Default : System.Web.UI.Page
{
private MD5 obj_MD5;
protected void Page_Load(object sender, EventArgs e)
{
obj_MD5 = new MD5();
}
protected void btnEncrypt_Click(object sender, EventArgs e)
{
lblEnryptedString.Text = obj_MD5.Encrypt(txtPalinStr.Text);
}
protected void btncheck_Click(object sender, EventArgs e)
{
lblcheck.Text = obj_MD5.verify(txtcheck.Text);
if (lblEnryptedString.Text == lblcheck.Text)
{
lblMessage.Text = "You Are Authenticated";
}
else
{
lblMessage.Text = "Authentication Failed";
}
}
}
And Finally My MD5 Class :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
namespace MD5Encrypt
{
public class MD5
{
private Byte[] EncStringBytes;
private UTF8Encoding Encoder = new UTF8Encoding();
private MD5CryptoServiceProvider MD5Hasher = new MD5CryptoServiceProvider();
private static string PasswordSalt;
string MD5String;
public MD5()
{ }
public string Encrypt(string EncString)
{
Random RanGen = new Random();
string RanString = "";
string RanSaltLoc;
while(RanString.Length<=3)
{
RanString = RanString + RanGen.Next(0, 9);
}
EncStringBytes = Encoder.GetBytes(EncString + RanString);
EncStringBytes = MD5Hasher.ComputeHash(EncStringBytes);
MD5String = BitConverter.ToString(EncStringBytes);
MD5String = MD5String.Replace("-", String.Empty);
RanSaltLoc = (RanGen.Next(4, MD5String.Length)).ToString();
MD5String = MD5String.Insert(Convert.ToInt32(RanSaltLoc),RanString);
if (Convert.ToInt32(RanSaltLoc) < 10)
{
RanSaltLoc = "0" + RanSaltLoc;
}
MD5String = MD5String.Insert(3, RanSaltLoc);
PasswordSalt = RanSaltLoc + RanString;
return MD5String;
}
public string verify(string chkString)
{
string SaltString = PasswordSalt.Substring(2,4);
string SaltLoaction = PasswordSalt.Substring(0, 2);
EncStringBytes = Encoder.GetBytes(chkString + SaltString);
EncStringBytes = MD5Hasher.ComputeHash(EncStringBytes);
MD5String = BitConverter.ToString(EncStringBytes);
MD5String = MD5String.Replace("-", String.Empty);
MD5String = MD5String.Insert(Convert.ToInt32(SaltLoaction), SaltString);
MD5String = MD5String.Insert(3, SaltLoaction);
return MD5String;
}
}
}
No comments:
Post a Comment