Encryption and Decryption of strings with vb.net

Good morning all! Last night "The Shield" was pretty good. Mackey must have been born under a lucky star to not have been the one that dropped off that suitcase. But poor Ronnie. They have him dead to rights. Somehow I thought he deserved a better fate than that. We always knew Julian would not cross the line and he proved it again last night.

My topic today is encryption and decryption of strings. It is straightforward but I had a situation occur where I needed to do this yesterday so I thought you might find it useful. Simply pass in the string you want to encrypt/decrypt and the key you want to use. Obviously I removed a lot that is not relevant to this sample but you get the idea. Make it a great day!

Imports System.IO
Imports System.Text
Imports System.Security.Cryptography
Public Class Crypto
    Private Shared DES As New TripleDESCryptoServiceProvider
    Private Shared MD5 As New MD5CryptoServiceProvider
    Public Shared Function MD5Hash(ByVal value As String) As Byte()
        Return MD5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(value))
    End Function
    Public Shared Function Encrypt(ByVal stringToEncrypt As String, ByVal key As String) As String
        DES.Key = Crypto.MD5Hash(key)
        DES.Mode = CipherMode.ECB
        Dim Buffer As Byte() = ASCIIEncoding.ASCII.GetBytes(stringToEncrypt)
        Return Convert.ToBase64String(DES.CreateEncryptor().TransformFinalBlock(Buffer, 0, Buffer.Length))
    End Function
    Public Shared Function Decrypt(ByVal encryptedString As String, ByVal key As String) As String
        Try
            DES.Key = Crypto.MD5Hash(key)
            DES.Mode = CipherMode.ECB
            Dim Buffer As Byte() = Convert.FromBase64String(encryptedString)
            Return ASCIIEncoding.ASCII.GetString(DES.CreateDecryptor().TransformFinalBlock(Buffer, 0, Buffer.Length))
        Catch ex As Exception
            MessageBox.Show("Invalid Key", "Decryption Failed", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        End Try
    End Function
End Class

 

It’s usage…..

 

Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim key As String = InputBox("Enter a Key:")
        Label1.Text = Crypto.Encrypt(TextBox1.Text, key)
        TextBox1.Clear()
    End Sub
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim key As String = InputBox("Enter a Key:")
        TextBox1.Text = Crypto.Decrypt(Label1.Text, key)
    End Sub
End Class

 

Advertisements
  1. #1 by Rujuta on June 18, 2009 - 2:27 am

    After trying so many codes, i got this code ,which is really very simple to understand and works properly…It really made my day grt!!thanks…

  2. #2 by Daniel Dijkhuizen on May 29, 2013 - 2:54 pm

    Tnx very much….this way i could crypt en de decrypt as file. Now they can’t read my config file. I am very glad…….Daniel from the Netherlands…..

  3. #3 by Mohammad Khader on November 15, 2013 - 7:55 am

    i want to ask u plz ,,,,
    what is the KEY ???
    what should i write into key inputbox ???

  4. #4 by Mohammad Khader on November 15, 2013 - 7:56 am

    btw
    it’s a great work u have done

  1. Encryption and Decryption of strings with C# | Kelly's Chronicles

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: