File Encryption Using DES Algorithm: CSS - Computer Systems Security
File Encryption Using DES Algorithm: CSS - Computer Systems Security
These guidelines show how implement file encryption/decryption using DES algorithm in VB.NET
platform.
Time needed to accomplish this task: 20 - 30 minutes
GUI
Create a new VB.NET project “Windows Form Application”, change the project name and path
according to your preferences.
Create a new windows form (if it is not created by default), name it “frmEncr”, and change its
caption/text to “Encryption”
In “frmEnc” form, create 1 textbox, 1 label, and 3 buttons and change their properties as follows:
By the end of this step you should have something looks like the following picture:
Coding
At the beginning, let’s do some imports. Add these lines to the beginning of your “frmEncr.vb” file
Imports System
Imports System.IO
Imports System.Text
Imports System.Security.Cryptography
CSS - Computer Systems Security
Note: The above imports help us to minimize the time needed to write the code.
With the above imports statements, instead of using statements like:
Dim DESx As New System.Security.Cryptography.DESCryptoServiceProvider
Both statements above will do the same, however the last one is shorter…
To select the file that you want to encrypt/decrypt, we will use “OpenFileDialog” tool that you can find
it in the Toolbox under “Dialogs” subcategory.
Drag and drop “OpenFileDialog” into your form. You will notice that this tool is added to under
the divider below your form.
Since the file is selected successfully, now it is time to do the real work…
Encryption
To do the encryption of the file, we will use the services provided by .NET platform.
The file encryption is carried out following these steps:
Open and read the selected input file.
Dim sr As StreamReader = New StreamReader(TxtInp.Text)
Dim strInput As String = (sr).ReadToEnd()
sr.Close()
Convert/encode the data read from the input file into Byte format
Dim byteArrayInput() As Byte = Encoding.Default.GetBytes(strInput)
Use .NET services to create a cryptosystem that encrypts the input file (in Byte format) and store it
in the output file.
des = New DESCryptoServiceProvider
Dim DESencrypt As ICryptoTransform = des.CreateEncryptor()
Dim DEScryptostream As CryptoStream = New CryptoStream(fs, DESencrypt,
CryptoStreamMode.Write)
DEScryptostream.Write(bytearrayinput, 0, bytearrayinput.Length)
DEScryptostream.Close()
fs.Close()
To show the user that the file was encrypted successfully, a messagebox is used as follows:
MessageBox.Show("File is encrypted successfully")
Decryption
Similarly, to implement the decryption part, we will use the services provided by .NET platform.
To do so, write the following code in “btnDec_Click” event sub:
Open and read the selected input file.
Dim fsread As FileStream = New FileStream(TextBox1.Text, FileMode.Open, FileAccess.Read)
Use the “des” DESCryptoServiceProvider created earlier to produce a cryptosystem that decrypts
the chosen file.
Dim desdecrypt As ICryptoTransform = des.CreateDecryptor()
Dim cryptostreamDecr As CryptoStream = New CryptoStream(fsread, desdecrypt,
CryptoStreamMode.Read)
Dim decryptedFile As String = New StreamReader(cryptostreamDecr).ReadToEnd()
Prepare the output file and write the decrypted data to that file
Dim fi As FileInfo = New FileInfo(TextBox1.Text)
Dim originalFile As String = TextBox1.Text.Substring(0, TextBox1.Text.Length -
fi.Extension.Length)
Dim fileWriter As StreamWriter = New StreamWriter(originalFile)
fileWriter.Write(decryptedFile)
fileWriter.Close()
To show the user that the file was encrypted successfully, a messagebox is used as follows:
At the end of this session you should have the following codes:
Question 1:
DES is a symmetric algorithm that needs one key for the encryption and decryption processes. Did we
use a key in our coding? If Yes, what is the key used?
Question 2:
How can you do a similar program as above but using user-specific or custom key for encryption and
decryption? What are the changes that you need to make in the above program to perform this task?