Tuesday, 19 July 2011

Secret by MD5 using ASP.NET 2.0 and VB.NET


This example demonstrates how to use MD5 add secret using Asp.Net2.0 and VB.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.

Imports System.Security.Cryptography;
Imports System.Text;

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

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 Function Md5AddSecret(ByVal strChange As String) As String
Dim pass() As Byte = Encoding.UTF8.GetBytes(strChange)
Dim md5 As MD5 = New MD5CryptoServiceProvider()
Dim strPassword As String = Encoding.UTF8.GetString(md5.ComputeHash(pass))
Return strPassword
End Function

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 front end Md5AddSecretVB2005.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>

Yes, it is possible to find a good web host. Sometimes it takes a while. After trying several, we went with Server Intellect and have been very happy. They are the most professional, customer service friendly and technically knowledgeable host we've found so far.

The flow for the code behind page is as follows.

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

Partial Public Class _Default
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)

End Sub

Public Function Md5AddSecret(ByVal strChange As String) As String
'Change the syllable into UTF8 code
Dim pass() As Byte = Encoding.UTF8.GetBytes(strChange)

Dim md5 As MD5 = New MD5CryptoServiceProvider()
Dim strPassword As String = Encoding.UTF8.GetString(md5.ComputeHash(pass))
Return strPassword
End Function

Protected Sub bntDisp_Click(ByVal sender As Object, ByVal e As EventArgs)
Try
Me.TxtResult.Text = Me.Md5AddSecret(Me.txtNeed.Text)
Catch ex As Exception
Response.Write(ex.ToString())
End Try
End Sub
End Class

Looking for the C#.NET 2005 Version? Click Here!

Looking for more ASP.NET Tutorials? Click Here!

No comments:

Post a Comment