aspx Tutorial

NET Articles,jQuery demo, asp.net with jQuery, online tutorial,Jquery, SilverLight, Javascript, asp.net,JSON, MVC,.NET Articles,demo, Web Services,
Advertise Here

Toolbar

Get our toolbar!

Advertize



Posted by Aamir Hasan   on Tuesday, February 22, 2011 Total Views:  

This article describes how to secure your password before saving in database or XML file etc. Attacker always try to attack the database and stole all the passwords stored in database. It's developer responsibility to encrypt the password before sending to the database. One way encryption is secure to protect the passwords. One way encryption can encrypt the password but cannot decrypt.

In this example, I have used SHA512Managed Class. The size of SHA512Managed algorithm is 512 bits.

Here’s an example

 

Default.aspx page

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title></title>

    <style>

        body

        {

            background: #fefefe;

            font-size: .80em;

            font-family: "Helvetica Neue" , "Lucida Grande";

            margin: 0px;

            padding: 0px;

            color: #222;

        }

    </style>

</head>

<body>

    <form id="form1" runat="server">

    <div>

        <h2>

        Implementing One-way Encryption in asp.net using csharp/VB.NET        </h2>

        <p>

            Password:

            <asp:TextBox ID="TextBox1" runat="server" />

            <asp:Button ID="Button1" runat="server" Text="Submit" OnClick="Button1_Click" />

            <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="Enter the Password."

                ControlToValidate="TextBox1" ForeColor="Red"></asp:RequiredFieldValidator></br>

            <br />

            <asp:Label ID="Label1" runat="server" />

        </p>

    </div>

    </form>

</body>

</html>

 

Default.aspx.cs

    protected void Button1_Click(object sender, EventArgs e)

    {

        Label1.Text = "Encrypted password:  " +
                      getEncryptedCode(TextBox1.Text.Trim().ToString());

    }

    public static string getEncryptedCode(string inputString)

    {

        byte[] Hash = new System.Security.Cryptography
                      .SHA512Managed().ComputeHash(System.Text.ASCIIEncoding.ASCII.GetBytes(inputString));

        System.Text.StringBuilder outputString = new System.Text.StringBuilder();

        for (int i = 0; i < Hash.Length; i++)

            outputString.Append(Hash[i].ToString("x2"));

        return outputString.ToString();

    }

 

Default.aspx.vb

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)

        Label1.Text = "Encrypted password:  " +
                      getEncryptedCode(TextBox1.Text.Trim().ToString())

    End Sub

    Public Shared Function getEncryptedCode(ByVal inputString As String) As String

        Dim Hash As Byte() = New System.Security.Cryptography
                             .SHA512Managed().ComputeHash(System.Text.ASCIIEncoding.ASCII.GetBytes(inputString))

        Dim outputString As New System.Text.StringBuilder()

        For i As Integer = 0 To Hash.Length - 1

            outputString.Append(Hash(i).ToString("x2"))

        Next

        Return outputString.ToString()

    End Function

 

Note:If you forgot your password, you cannot decrypt it; you have to reset your password.

 

SHA512Managed Class Supported in .net version 1.0, 1.1, 2.0, 3.0, 3.5 and 4.O


If you have alternative solution, please comments below to share with us.

Output

Download

One-way-Encryption-cSharp.rar (1.30 kb)

One-way-EncryptionVB.rar (1.23 kb)

See live demo

Protected by Copyscape Online Plagiarism Tool

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading

Advertizement 1
Advertizement 2
Advertizement 3
Advertizement 4
Advertizement 5