VB.NET: Encryption with Bytes and MD5

Encryption is usually needed for security things. To be specific, it uses by login process. So this time I'll show you an encryption that using Bytes and MD5. String value of password is converted into array bytes then encrypted again with MD5.


Make a simple UI as below: 

Here is the process:
User will input text into Textbox 1 then click button 1. It will result an encrypted string that will be displayed on Label1. 

and here is the code:

Imports System.Security.Cryptography
Imports System.Text

Public Class frmEncrypt

    'string --> bytes
    Function Convert2ByteArray(ByVal strInput As StringAs Byte()
        Dim encoder As New UTF8Encoding()
        Dim md5Hasher As New MD5CryptoServiceProvider
        Return md5Hasher.ComputeHash(Encoder.GetBytes(strInput))
    End Function

    'Encrypt text
    Function Encrypt_Data(ByVal strToEncrypt As StringAs String
        Dim objMD5 As MD5CryptoServiceProvider
        Dim strEncrypted As String

        objMD5 = New MD5CryptoServiceProvider
        strEncrypted = BitConverter.ToString(objMD5.ComputeHash( _
                       Convert2ByteArray(strToEncrypt)))

        Return strEncrypted
    End Function

    Private Sub frmEncrypt_Load(ByVal sender As System.Object, _
        ByVal As System.EventArgs) Handles MyBase.Load
        Button1.Text = "Encrypt"
        Label1.Text = String.Empty
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, _
        ByVal As System.EventArgs) Handles Button1.Click
        Label1.Text = Encrypt_Data(TextBox1.Text)
    End Sub

End Class


Okay, lets try to run...




Post a Comment

0 Comments