Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Source] TripleDES String Encryption / Decryption
#1
The following source codes showed how to encrypt / decrypt TripleDES algorithm.

I have converted the C# code into VB.NET and coded a sample.
Code:
http://www.dijksterhuis.org/encrypting-decrypting-string/

[Image: PJddg.png]

Form Codes:
Code:
Public Class Template
    ' Derived from http://www.dijksterhuis.org/encrypting-decrypting-string/

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        ' The message to encrypt.
        Dim Msg As String = TextBox1.Text
        Dim Password As String = TextBox2.Text

        Try
            Dim EncryptedString As String = MainClass.EncryptString(Msg, Password)
            TextBox3.Text = EncryptedString
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Error")
        End Try
    End Sub

    Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
        Try
            Dim DecryptedString As String = MainClass.DecryptString(TextBox3.Text, TextBox2.Text)
            TextBox4.Text = DecryptedString
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Error")
        End Try

    End Sub

    Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
        TextBox1.Text = ""
        TextBox2.Text = ""
    End Sub

    Private Sub Button4_Click(sender As System.Object, e As System.EventArgs) Handles Button4.Click
        TextBox3.Text = ""
    End Sub
End Class

Module Codes:
Code:
Imports System
Imports System.Text
Imports System.Security.Cryptography

Class MainClass

    Public Shared Function EncryptString(ByVal Message As String, ByVal Passphrase As String) As String
        Dim Results() As Byte
        Dim UTF8 As System.Text.UTF8Encoding = New System.Text.UTF8Encoding
        ' Step 1. We hash the Passphrase using MD5
        ' We use the MD5 hash generator as the result is a 128 bit byte array
        ' which is a valid length for the TripleDES encoder we use below
        Using HashProvider As MD5CryptoServiceProvider = New MD5CryptoServiceProvider()
            Dim TDESKey() As Byte = HashProvider.ComputeHash(UTF8.GetBytes(Passphrase))

            ' Step 2. Create a new TripleDESCryptoServiceProvider object

            ' Step 3. Setup the encoder
            Using TDESAlgorithm As TripleDESCryptoServiceProvider = New TripleDESCryptoServiceProvider() With {.Key = TDESKey, .Mode = CipherMode.ECB, .Padding = PaddingMode.PKCS7}
                ' Step 4. Convert the input string to a byte[]

                Dim DataToEncrypt() As Byte = UTF8.GetBytes(Message)

                ' Step 5. Attempt to encrypt the string
                Try
                    Dim Encryptor As ICryptoTransform = TDESAlgorithm.CreateEncryptor
                    Results = Encryptor.TransformFinalBlock(DataToEncrypt, 0, DataToEncrypt.Length)
                Finally
                    ' Clear the TripleDes and Hashprovider services of any sensitive information
                    TDESAlgorithm.Clear()
                    HashProvider.Clear()
                End Try
            End Using
        End Using

        ' Step 6. Return the encrypted string as a base64 encoded string
        Return Convert.ToBase64String(Results)
    End Function

    Public Shared Function DecryptString(ByVal Message As String, ByVal Passphrase As String) As String
        Dim Results() As Byte
        Dim UTF8 As System.Text.UTF8Encoding = New System.Text.UTF8Encoding

        ' Step 1. We hash the Pass phrase using MD5
        ' We use the MD5 hash generator as the result is a 128 bit byte array
        ' which is a valid length for the TripleDES encoder we use below
        Using HashProvider As MD5CryptoServiceProvider = New MD5CryptoServiceProvider()
            Dim TDESKey() As Byte = HashProvider.ComputeHash(UTF8.GetBytes(Passphrase))

            ' Step 2. Create a new TripleDESCryptoServiceProvider object
            ' Step 3. Setup the decoder
            Using TDESAlgorithm As TripleDESCryptoServiceProvider = New TripleDESCryptoServiceProvider() With {.Key = TDESKey, .Mode = CipherMode.ECB, .Padding = PaddingMode.PKCS7}

                ' Step 4. Convert the input string to a byte[]
                Dim DataToDecrypt() As Byte = Convert.FromBase64String(Message)
                ' Step 5. Attempt to decrypt the string
                Try
                    Dim Decryptor As ICryptoTransform = TDESAlgorithm.CreateDecryptor
                    Results = Decryptor.TransformFinalBlock(DataToDecrypt, 0, DataToDecrypt.Length)
                Finally

                    ' Clear the TripleDes and Hash provider services of any sensitive information
                    TDESAlgorithm.Clear()
                    HashProvider.Clear()
                End Try
            End Using
        End Using

        ' Step 6. Return the decrypted string in UTF8 format
        Return UTF8.GetString(Results)
    End Function

End Class

I hope this helps.
USB Anti-virus? Try USB Drive Defender
[Image: 8bQCusS.jpg]
Reply
#2
Thanks for this, this will help members that are new to encrypting and decrypting
Reply
#3
Thanks a lot for this nice share .
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Encrypt String using x509Certificate private Key wih RSA jeffstan 0 2,295 01-26-2014, 04:18 PM
Last Post: jeffstan
  Combination Cracker - String Variant Builder - Developed by AceInfinity AceInfinity 0 1,240 03-15-2012, 06:05 PM
Last Post: AceInfinity
  [Source ] Hemp Tycoon App [/Source] VB.net KoBE 8 9,291 03-05-2012, 10:30 PM
Last Post: SomeWhiteGuy?
  [Source]Sql Database Project [Source] Digital-Punk 0 1,339 10-16-2011, 07:01 AM
Last Post: Digital-Punk
  [Source]Batch to Exe converter[Source] Digital-Punk 6 2,611 10-15-2011, 03:00 AM
Last Post: Digital-Punk

Forum Jump:


Users browsing this thread: 1 Guest(s)