Cryptography HMAC Algorithm in VB.NET

In this article you will learn that how to authenticate a Message with secret key.
  • 8713
 

Introduction

Here in this article I am discussing that how can you use the HMAC Algorithm of cryptography to check the integrity and authenticity of code. If you want to check the message authenticity you have to give the message and also a secret key as we used key 'Hello' in this article. Then you click on the Authenticate button and you will get the Authenticated message with different algorithms of HMAC. The Implementation of this needs seven TextBox and one Button control.

Getting Started

  • Simply create a new ASP.NET web application.
  • Drag seven TextBox and a button on your web page. Your page will look like below.

    message-3.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>
    Results are after authentication<br />
    <
    br />
    &nbsp; Result of HMAC MD5&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <asp:TextBox ID="txt1" runat="server"></asp:TextBox>
    <
    br />
    &nbsp; Result of HMAC SHA1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <asp:TextBox ID="txt2" runat="server"></asp:TextBox>
    <
    br />
    &nbsp; Result of HMAC SHA256&nbsp;
    <asp:TextBox ID="txt3" runat="server"></asp:TextBox>
    <
    br />
    &nbsp; Result of HMAC SHA384&nbsp;
    <asp:TextBox ID="txt4" runat="server"></asp:TextBox>
    <
    br />
    &nbsp; Result of HMAC SHA512&nbsp;
    <asp:TextBox ID="txt5" runat="server"></asp:TextBox>
    <
    br />
    <
    br />
    <
    br />
    <
    br />
    &nbsp;GetMessage&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <asp:TextBox ID="txtKey" runat="server"></asp:TextBox>
    <
    br
    />
    &nbsp;GetKey&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    &nbsp;&nbsp;&nbsp;&nbsp;
    <asp:TextBox ID="txtMessage" runat="server"></asp:TextBox>
    <
    br />
    <
    br
    />
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <asp:Button ID="btnAuthenticate" runat="server" Text="Authenticate" />
    <
    br />
    <
    br />
    <
    br />
    </
    div>
    </
    form>
    </body>
    </
    html>

     
  • Then add the below code in code Behind file of the web page.

    Protected Sub btnAuthenticate_Click(sender As Object, e As System.EventArgs) Handles btnAuthenticate.Click
    Dim getmessage As String
    Dim
    getkey As String
    getkey = Me.txtKey.Text
    getmessage = Me.txtMessage.Text
    Dim encoding As New System.Text.ASCIIEncoding()
    Dim getkeyByte As Byte() = Encoding.GetBytes(getkey)
    Dim gethmacmd5 As New HMACMD5(getkeyByte)
    Dim gethmacsha1 As New HMACSHA1(getkeyByte)
    Dim gethmacsha256 As New HMACSHA256(getkeyByte)
    Dim gethmacsha384 As New HMACSHA384(getkeyByte)
    Dim gethmacsha512 As New HMACSHA512(getkeyByte)
    Dim getmessageBytes As Byte() = encoding.GetBytes(getmessage)
    Dim hashmessage As Byte() = gethmacmd5.ComputeHash(getmessageBytes)
    Me.txt1.Text = ByteToString(hashmessage)
    hashmessage = gethmacsha1.ComputeHash(getmessageBytes)
    Me.txt2.Text = ByteToString(hashmessage)
    hashmessage = gethmacsha256.ComputeHash(getmessageBytes)
    Me.txt3.Text = ByteToString(hashmessage)
    hashmessage = gethmacsha384.ComputeHash(getmessageBytes)
    Me.txt4.Text = ByteToString(hashmessage)
    hashmessage = gethmacsha512.ComputeHash(getmessageBytes)
    Me.txt5.Text = ByteToString(hashmessage)
    End Sub
    Public
    Shared Function ByteToString(buff As Byte()) As String
    Dim
    getbinary As String = ""
    For i As Integer = 0 To buff.Length - 1
    getbinary += buff(i).ToString("X2")
    Next
    Return
    (getbinary)
    End Function

     
  • Now run your application
Output

message1.gif

message2.gif

message4.gif

Summary
 
In this article you learned that how to Authenticate a message with HMAC.

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.