12,252,903 members (57,888 online)
alternative version

40.2K views
14 bookmarked
Posted

# Learn the TripleDES encryption algorithm in a minute

, 23 Feb 2008 CPOL
 Rate this:
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:

## 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:

```'' 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:

```''' <span class="code-SummaryComment"><summary> </span>
''' Represents the base class for Triple Data Encryption Standard
''' algorithms from which all TripleDES implementations must derive.
''' <span class="code-SummaryComment"></summary> </span>
''' <span class="code-SummaryComment"><remarks></remarks></span>
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

''' <span class="code-SummaryComment"><summary> </span>
''' Represents the base class for Triple Data Encryption Standard
''' algorithms from which all TripleDES implementations must derive.
''' <span class="code-SummaryComment"></summary> </span>
''' <span class="code-SummaryComment"><param name="Key">The secret key for the symmetric algorithm.</param> </span>
''' <span class="code-SummaryComment"><param name="IV">The initialization vector</span>
''' (IV) for the symmetric algorithm.<span class="code-SummaryComment"></param> </span>
''' <span class="code-SummaryComment"><remarks></remarks></span>
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 `Byte`s. Here is how it is done:

```'' 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:

```''' <span class="code-SummaryComment"><summary></span>
''' The process of Encryption using the plain text , key, vector.
''' <span class="code-SummaryComment"></summary></span>
''' <span class="code-SummaryComment"><param name="plainText">A string or text which needs to be encrypted.</param></span>
''' <span class="code-SummaryComment"><returns></returns></span>
''' <span class="code-SummaryComment"><remarks></remarks></span>
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
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:

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

Now, let's see the `Decryption` function:

```''' <span class="code-SummaryComment"><summary></span>
''' The process of Decryption using the the encrypted bytes , key, vector.
''' <span class="code-SummaryComment"></summary></span>
''' <span class="code-SummaryComment"><param name="cipherBytes">A chiper bytes which need to be decrypted.</param></span>
''' <span class="code-SummaryComment"><returns></returns></span>
''' <span class="code-SummaryComment"><remarks></remarks></span>
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
cryptStream.Close()
Dim myutf As UTF8Encoding = New UTF8Encoding()
Return myutf.GetString(result)
End Function```

## Share

 Software Developer (Junior) India
No Biography provided

## You may also be interested in...

 First Prev Next
 TDES Decryption of Hexstring using hexstring key Member 1128856711-Apr-16 21:46 Member 11288567 11-Apr-16 21:46
 Last Visit: 31-Dec-99 18:00     Last Update: 5-May-16 3:37 Refresh 1