Click here to Skip to main content
11,714,719 members (78,075 online)
Click here to Skip to main content

Learn the TripleDES encryption algorithm in a minute

, 23 Feb 2008 CPOL 38.9K 783 14
Rate this:
Please Sign up or sign in to vote.
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:

'' 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 Bytes. 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
    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:

'' 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
    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)

Share

About the Author

Imran A Momin
Software Developer (Junior)
India India
No Biography provided

You may also be interested in...

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150819.1 | Last Updated 24 Feb 2008
Article Copyright 2008 by Imran A Momin
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid