Tuesday, 19 July 2011

Secret by MD5 using ASP.NET 2.0 and C#.NET


This example demonstrates how to use MD5 add secret using Asp.Net2.0 and C#.Net

This example demonstrates how to use MD5 add secret.

First, you will need to import the System.Collections.Cryptography and System.Collections.Text namespace.

using System.Security.Cryptography;
using System.Text;

We used over 10 web hosting companies before we found Server Intellect. Their dedicated servers and add-ons were setup swiftly, in less than 24 hours. We were able to confirm our order over the phone. They respond to our inquiries within an hour. Server Intellect's customer support and assistance are the best we've ever experienced.

The System.Security.Cryptography namespace provides cryptographic services, including secure encoding and decoding of data, as well as many other operations, such as hashing, random number generation, and message authentication.

The System.Text namespace contains classes representing ASCII, Unicode, UTF-7, and UTF-8 character encodings; abstract base classes for converting blocks of characters to and from blocks of bytes; and a helper class that manipulates and formats string objects without creating intermediate instances of String.

We use the Md5AddSecret Function to do the work.

public string Md5AddSecret(string strChange)
{
byte[] pass = Encoding.UTF8.GetBytes(strChange);
MD5 md5 = new MD5CryptoServiceProvider();
string strPassword = Encoding.UTF8.GetString(md5.ComputeHash(pass));
return strPassword;
}

Try Server Intellect for Windows Server Hosting. Quality and Quantity!

The front end Md5AddSecretCsharp2005.aspx page looks something like this:

<table border="1"style="width: 323px; height: 76px;">
<tr>
<td style="width: 190px; height: 28px;">&nbsp;Need To Add Secret :</td>
<td style="width: 91px; height: 28px;">
<asp:TextBox ID="txtNeed" runat="server" Width="128px"></asp:TextBox></td>
</tr>
<tr>
<td style="width: 190px">
&nbsp;Encrypt &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Result :</td>
<td style="width: 91px">
<asp:TextBox ID="TxtResult" runat="server" Height="17px" Width="130px" ReadOnly="True"></asp:TextBox></td>
</tr>
<tr>
<td colspan="2" style="height: 26px">
<asp:Button ID="bntDisp" runat="server" OnClick="bntDisp_Click" Text="Add secret" /></td>
</tr>
</table>

Need help with Windows Dedicated Hosting? Try Server Intellect. I'm a happy customer!

The flow for the code behind page is as follows.

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Security.Cryptography;
using System.Text;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}

public string Md5AddSecret(string strChange)
{
//Change the syllable into UTF8 code
byte[] pass = Encoding.UTF8.GetBytes(strChange);

MD5 md5 = new MD5CryptoServiceProvider();
string strPassword = Encoding.UTF8.GetString(md5.ComputeHash(pass));
return strPassword;
}

protected void bntDisp_Click(object sender, EventArgs e)
{
try
{
this.TxtResult.Text = this.Md5AddSecret(this.txtNeed.Text);
}
catch (Exception ex)
{
Response.Write(ex.ToString());
}
}
}

Looking for the VB.NET 2005 Version? Click Here!

Looking for more ASP.NET Tutorials? Click Here!

No comments:

Post a Comment