ARTICLE

How to implement Message Authentication mechanism with HMAC Algorithm in VB.NET

Posted by Dea Saddler Articles | Cryptography in VB.NET May 13, 2011
In this article you will learn that how to authenticate a Message with secret key.
Download Files:
 
Reader Level:

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.

    message1.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:-

message2.gif

message-3.gif

message4.gif

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

Login to add your contents and source code to this article
share this article :
post comment
 
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Team Foundation Server Hosting
Become a Sponsor