Encryption with SHA1 and MD5 in VB.NET

In this article you will learn that how you can perform Encryption with SHA1 and MD5.
  • 5651
 

Introduction

Here in this article we are discussing that how can you Encrypt your password or any string by selecting any of the cryptography Algorithm Either by SHA1 or MD5. The implementing of this example needs to import System.web.Security namespace and you will also add four controls as two TextBox, Button, DropDownList and a Label control. You will select Algorithm for Encryption from DropDownList. The namespace will be inherited by Forms Authentication class in which you will get the HashPasswordForStoringInConfigFile method. This method is used for the conversion of password or any string into Encrypted format.

Getting Started

  • Simply Create a new ASP.NET web application.
  • Drag two TextBox, Button, DropDownList and a Label control on your page. The page will look like below.

    pass2.gif
     
  • Your Default.aspx page will look like below.

    <%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <
    title></title>
    </head>
    <
    body>
    <form id="form1" runat="server">
    <
    div>
    </
    div>
    <
    asp:TextBox ID="txtResult" runat="server"></asp:TextBox>
    &nbsp;&nbsp;&nbsp;
    <asp:Button ID="btnEncrypt" runat="server" Text="PerformEncryption" />
    &nbsp;&nbsp;
    <br />
    <
    br />
    <
    asp:DropDownList ID="Selectalgo" runat="server" Height="16px">
    <
    asp:ListItem>SHA1</asp:ListItem>
    <
    asp:ListItem>MD5</asp:ListItem>
    </
    asp:DropDownList>
    <
    br />
    <
    br />
    <
    br />
    <
    br />
    <
    asp:Label ID="DisplayResult" runat="server" Text="EncryptedResult"></asp:Label>
    &nbsp;&nbsp;
    <asp:TextBox ID="txtDisplayValue" runat="server"></asp:TextBox>
    </
    form>
    </body>
    </
    html>
     
  • Then add the below code in code behind file of the web page.

    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
    If Me.IsPostBack = False Then
    Selectalgo.Items.Add("")
    Selectalgo.SelectedValue = ""
    End If
    End
    Sub
    Protected
    Sub btnEncrypt_Click(sender As Object, e As System.EventArgs) Handles btnEncrypt.Click
    txtDisplayValue.Text = FormsAuthentication.HashPasswordForStoringInConfigFile(txtResult.Text, Selectalgo.SelectedValue)
    End Sub

     
  • Now run your application.

Output

pass3.gif

pass4.gif

pass5.gif

pass6.gif

pass8.gif

Summary
 
In this article you learned that how to perform Encryption

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.