Learn the TripleDES encryption algorithm in a minute






2.63/5 (7 votes)
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:
''' <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 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:
''' <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:
'' Array of bytes to store the encrypted data
Private DecrytedText as String
DecrytedText = NewTripleDES.Decryption(EncryptedBytes())
Now, let's see the Decryption
function:
''' <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