Sunday, January 28, 2018

MD5 Hash Generator using Visual Studio

Every developers build series of application system but they forget the concept of cryptology.


Cryptology the study of codes, or the art of writing and solving them. it is a mathematics such as number theory, application of algorithms that building cryptography and cryptanalysis.

Cryptanalysis is the study of analyzing information in order to study the hidden aspects of the system, it is used to breach security system and access to the contents, even if the cryptographic key is undefined. The concepts are complex and high specialized.


Syntax


Imports System
Imports System.Security.Cryptography
Imports System.Text

Public Class Form1

    Function GetMd5Hash(ByVal md5Hash As MD5, ByVal input As String) As String
        Dim data As Byte() = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input))
        Dim sBuilder As New StringBuilder()
        Dim i As Integer
        For i = 0 To data.Length - 1
            sBuilder.Append(data(i).ToString("x2"))
        Next i
        Return sBuilder.ToString()

    End Function

    Function VerifyMd5Hash(ByVal md5Hash As MD5, ByVal input As String, ByVal hash As String) As Boolean
        Dim hashOfInput As String = GetMd5Hash(md5Hash, input)
        Dim comparer As StringComparer = StringComparer.OrdinalIgnoreCase
        If 0 = comparer.Compare(hashOfInput, hash) Then
            Return True
        Else
            Return False
        End If

    End Function

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim [source] As String = TextBox1.Text
        Using md5Hash As MD5 = MD5.Create()

            Dim hash As String = GetMd5Hash(md5Hash, source)

            Label2.Text = hash

            If VerifyMd5Hash(md5Hash, [source], hash) Then
                MsgBox("The result are same.", MsgBoxStyle.Information, "MD5 Hash Generator")
            Else
                MsgBox("The result are not same.", MsgBoxStyle.Critical, "MD5 Hash Generator")
            End If
        End Using

    End Sub
End Class


Result/Image




Saturday, January 27, 2018

Simple Encrypt and Decrypt Function using Visual Studio

Let's create our own encryption code without using the existing encryption method like md5 or hash.

The Code below shows a simple trick on how to handle the Encryption using the loop, chr, asc, and mid method.


ChrW method returns a String containing the Unicode character except on platforms where Unicode is not supported, in which case, the behavior is identical to the Chr function. Chr() method that works in Visual Basic but I don't know in C# code.

AscW function returns the Unicode character code except on platforms where Unicode is not supported, in which case, the behavior is identical to the Asc function.

Mid function to return a specified number of characters from a string.


Encrypt
Function Encrypt(ByVal Inputtxt As String)
        Encrypt = ""
        Dim CharCount As Integer
        For CharCount = 1 To Len(Inputtxt)
            Encrypt = Encrypt + Chr(Asc(Mid(Inputtxt, CharCount, 1)) + 15)
        Next CharCount
    End Function

Decrypt
Function Decrypt(ByVal Inputtxt As String)
        Decrypt = ""
        Dim CharCount As Integer
        For CharCount = 1 To Len(Inputtxt)
            Decrypt = Decrypt + Chr(Asc(Mid(Inputtxt, CharCount, 1)) - 15)
        Next CharCount
    End Function




Image below show's how to use this code





Syntax




Result