Click here to Skip to main content
15,886,110 members
Articles / Programming Languages / Visual Basic
Article

Learn the TripleDES encryption algorithm in a minute

Rate me:
Please Sign up or sign in to vote.
2.63/5 (7 votes)
23 Feb 2008CPOL1 min read 56K   983   14   2
Triple Data Encryption Standard algorithms from which all TripleDES implementations must derive.

Introduction

I was looking for an encryption method for my project, and I came across the TripleDES algorithm. But, later I found it annoying to declare and initialize the TripleDES class again and again. So, I created a simple and basic class which solves the purpose.

Background

The idea behind the class was to make it simple to encrypt and decrypt data that I want to store in a database, like the username, password, and other stuff which are sensitive to be exposed. The class that I created is simple to use, with just the methods for a basic implementation. Let's see the class diagram:

TripleDES.JPG

Using the code

To implement the class, you need to initialize the class. During the initialization of the class, the new method has been overloaded so that a user can define his own key and Vector (IV) for the symmetric algorithm. The default value for the key and the vector is as follows:

VB
'' Default value of the key and the vector
Dim Newkey() As Byte = {7, 99, 3, 44, 52, 76, 79, 84, 63, 63, 95, _
             12, 10, 14, 15, 16, 64, 99, 38, 77, 68, 2, 3, 74}
Dim Newiv() As Byte = {75, 220, 255, 151, 65, 212, 209, 162}

Here is how the new class has been defined:

VB
''' <summary> 
''' Represents the base class for Triple Data Encryption Standard
''' algorithms from which all TripleDES implementations must derive. 
''' </summary> 
''' <remarks></remarks>
Public Sub New()
    Dim Newkey() As Byte = {7, 99, 3, 44, 52, 76, 79, 84, 63, 63, 95, _
                            12, 10, 14, 15, 16, 64, 99, 38, 77, 68, 2, 3, 74}
    Dim Newiv() As Byte = {75, 220, 255, 151, 65, 212, 209, 162}
    Keys = Newkey
    IVs = Newiv 
End Sub 

''' <summary> 
''' Represents the base class for Triple Data Encryption Standard
''' algorithms from which all TripleDES implementations must derive. 
''' </summary> 
''' <param name="Key">The secret key for the symmetric algorithm.</param> 
''' <param name="IV">The initialization vector
''' (IV) for the symmetric algorithm.</param> 
''' <remarks></remarks>
Public Sub New(ByVal Key() As Byte, ByVal IV() As Byte)
    Keys = Key
    IVs = IV
End Sub

To encrypt plain text

To encrypt text, you need to create an instance of a class and call the the Encryption function. The return type of this function is an array of Bytes. Here is how it is done:

VB
'' Create an instance of a class
Private NewTripleDES as new TripleDES()
'' Array of bytes to store the encrypted data
Private EncryptedBytes() as Byte
EncrpytedBytes() = NewTripleDES.Encryption("PlainText")

Now, let's see the Encryption function:

VB
''' <summary>
''' The process of Encryption using the plain text , key, vector.
''' </summary>
''' <param name="plainText">A string or text which needs to be encrypted.</param>
''' <returns></returns>
''' <remarks></remarks>
Public Function Encryption(ByVal plainText As String) As Byte()
    Dim utf8encoder As UTF8Encoding = New UTF8Encoding()
    Dim inputInBytes() As Byte = utf8encoder.GetBytes(plainText)
    Dim tdesProvider As TripleDESCryptoServiceProvider = _
                        New TripleDESCryptoServiceProvider()
    Dim cryptoTransform As ICryptoTransform = tdesProvider.CreateEncryptor(Me.Keys, Me.IVs)
    Dim encryptedStream As MemoryStream = New MemoryStream()
    Dim cryptStream As CryptoStream = New CryptoStream(encryptedStream, _
                                          cryptoTransform, CryptoStreamMode.Write)
    cryptStream.Write(inputInBytes, 0, inputInBytes.Length)
    cryptStream.FlushFinalBlock()
    encryptedStream.Position = 0
    Dim result(encryptedStream.Length - 1) As Byte
    encryptedStream.Read(result, 0, encryptedStream.Length)
    cryptStream.Close()
    Return result
End Function

To decrypt the encrypted data

To decrypt the encrypted data, you need to call the decryption function. The return type of this function is String. Here is how it is done:

VB
'' Array of bytes to store the encrypted data
Private DecrytedText as String
DecrytedText = NewTripleDES.Decryption(EncryptedBytes())

Now, let's see the Decryption function:

VB
''' <summary>
''' The process of Decryption using the the encrypted bytes , key, vector.
''' </summary>
''' <param name="cipherBytes">A chiper bytes which need to be decrypted.</param>
''' <returns></returns>
''' <remarks></remarks>
Public Function Decryption(ByVal cipherBytes() As Byte) As String
    Dim utf8encoder As UTF8Encoding = New UTF8Encoding()
    Dim tdesProvider As TripleDESCryptoServiceProvider = _
                        New TripleDESCryptoServiceProvider()
    Dim cryptoTransform As ICryptoTransform = _
                   tdesProvider.CreateDecryptor(Me.Keys, Me.IVs)
    Dim decryptedStream As MemoryStream = New MemoryStream()
    Dim cryptStream As CryptoStream = New CryptoStream(decryptedStream, _
                       cryptoTransform, CryptoStreamMode.Write)
    cryptStream.Write(cipherBytes, 0, cipherBytes.Length)
    cryptStream.FlushFinalBlock()
    decryptedStream.Position = 0
    Dim result(decryptedStream.Length - 1) As Byte
    decryptedStream.Read(result, 0, decryptedStream.Length)
    cryptStream.Close()
    Dim myutf As UTF8Encoding = New UTF8Encoding()
    Return myutf.GetString(result)
End Function

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Junior)
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionI'm using C# Pin
Member 1437211811-Jul-19 2:20
Member 1437211811-Jul-19 2:20 
QuestionTDES Decryption of Hexstring using hexstring key Pin
Member 1128856711-Apr-16 21:46
Member 1128856711-Apr-16 21:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.