Implement One Way Encryption in ASP.NET using VB.NET

In this article you will learn that how can you encrypt your password before storing it by one way encryption technique.
  • 3708
 

Introduction

Here in this article I am describing that how you can encrypt your password in ASP.NET using One Way Encryption and how to secure your password before saving in the database or XML file etc. Attackers and Hackers are always try to attack the database and stole password stored in database. It is the responsibility of the developer to encrypt the password before storing the database. One Way Encryption is secure to protect the password. One Way Encryption is Encrypt the password but not Decrypt. In this example I have used SHA512Managed Class. The size of SHA512Managed algorithm is 512 bits.

Getting Started with One Way Encryption

  • Simply create a new ASP.NET web application. 
  • Drag one Textbox, RequiredFieldValidator, Button and two Label control on your web page. 
  • The design mode of your web page will look like below page.

    Encryption1.gif
     
  • The Source mode of the web page will look like below.

    <%@ Page Title="Home Page" Language="vb" MasterPageFile="~/Site.Master" AutoEventWireup="false"
        CodeBehind="Default.aspx.vb" Inherits="One_way_Encrytion._Default" %>
    <asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
    </asp:Content>
    <
    asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
               <style>
            body
            {
                background: #fefefe;
                font-size: .80em;
                font-family: "Helvetica Neue" , "Lucida Grande";
                margin: 0px;
                 padding: 0px;
                color: #222;
            }
        </style>
      <div>
            <h2>
            Implementing One-way Encryption in asp.net using 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>
        </asp:Content>
     
  • Then add the below code on the code behind file of the web page.

        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.GetByt
             (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

     
  • Now run your application.

Output

Encryption2.gif

Encryption3.gif

Encryption4.gif

Summary 

In this article you learned that how you can encrypt your password by One Way Encryption and save it from attackers.

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.