Creating Your Own Encryption / Decryption Program Using VB.NET 2005

WEBINAR:On-demand webcast

How to Boost Database Development Productivity on Linux, Docker, and Kubernetes with Microsoft SQL Server 2017 REGISTER >

Introduction

These days, protecting your valuable information is absolutely vital.
Anything is possible. hackers & crackers frequently try to find new ways
to break programs and to find critical information, for their malicious
purposes. We shouldn't only care about hackers & crackers, we should be
concerned about coworkers using ( or having access to ) your computer.
With this article, I will try my best to assist you in protecting valuable
information such as Passwords, account numbers and so on. Before I continue, let
me explain the complicated stuff :)

Encryption

Encryption is the translation of data into a secret code, ciphertext. This code is scrambled, and results in an unreadable ( at least by a normal person ) string of characters.
There are 2 types of Encryption :
Asymetric
Asymmetric Encryption is a form of Encryption where keys come in pairs. What one key encrypts, only the other can decrypt. Asymmetric Encryption is also known as Public Key Cryptography, since users typically create a matching key pair, and make one public while keeping the other secret.
Symetric
Symmetric Encryption is an Encryption algorithm where the same key is used for both Encryption and Decryption. The key must be kept secret, and is shared by the message sender and recipient.

Decryption

Decryption is the process of decoding data that has been encrypted into a secret format. Decryption requires a secret key or password.

Encryption methods

Rijndael method

The Rijndael encryption algorithm has been designed to replace the aging DES algorithm. Like DES, it is a block cipher. It uses 128-bit, 192-bit or 256-bit keys. This implementation encrypts 128-bit blocks.
( DES used 56-bit keys and 64-bit blocks )

Hexadecimal

Hexadecimal (also base-16, hexa, or hex) is a numeral system with a radix, or base, of 16. It uses sixteen distinct symbols, most often the symbols 0-9 to represent values zero to nine, and A, B, C, D, E, F (or a through f) to represent values ten to fifteen.

Ascii

Creating Your Own Encryption / Decryption Program Using VB.NET 2005

WEBINAR:On-demand webcast

How to Boost Database Development Productivity on Linux, Docker, and Kubernetes with Microsoft SQL Server 2017 REGISTER >

Our sample program

Purpose

Our sample program's function is to be able to write to and read from an encrypted file. What's happening is that, when the program is first loaded, we can enter our "secret" text, and save it with a name and password. To access this file again, we need to supply the correct filename and password again. What I've done, was to create an onscreen keyboard to assist us when entering a password, so people never actually type the password, they enter it with the mouse. What I've done further was to convert the selected Filename to Hexadecimal code, so that it's not that easily known; when I need to refer to the file again, I just decode the Hexadecimal code back to normal text. Sound exciting B? let's start!

Design

Create a new Windows Application and call it CryptoViewer. CryptoViewer
consists of two forms, so use the Project menu to add another Form to your
project.

Add the following controls to Form1, and set the following properties on
Form1 - your main form :

Control Name

Property Name

Property Setting

Form

Name

frmCryptoViewer

Text

Crypto Viewer

FormBorderStyle

FixedDialog

BackColor

White

Button

Name

btnLoadFile

Text

Load File

Button

Name

btnCopy

Text

Copy

Button

Name

btnSave

Text

Save

Button

Name

btnExit

Text

Exit

TextBox

Name

txtCrypto

AcceptsReturn

True

Font

Arial, 9.75pt

ForeColor

MediumBlue

HideSelection

False

MultiLine

True

ScrollBars

Both

WordWrap

False

OpenFileDialog

Name

ofdCrypt

FileName

"" ( Empty String )

frmCryptoViewer should look the same as:

[frmCryptoViewer.png]

Figure 1 - frmCryptoViewer

Add the following controls to Form2, and set the following properties on
Form2 - your password form :

Creating Your Own Encryption / Decryption Program Using VB.NET 2005

WEBINAR:On-demand webcast

How to Boost Database Development Productivity on Linux, Docker, and Kubernetes with Microsoft SQL Server 2017 REGISTER >

Implementation

When this program is run, frmCryptoViewer will be
displayed and waiting for input. You can enter all your sensitive
information in here, it can be a username and password, it can be a love letter,
it doesn't matter :). Once all text has been input, you should save the
document. After you have clicked the Save button, your frmPass will show,
and will look like Figure 3. Here, you should click in your
password. Now, what's happening in the background?

A normal Textfile named Output.txt gets
created. The ApplyCrypt method then reads this document and applies the
Rijndael encryption onto it, and now, creates the Encrypted.enc, and when the
program exits, it deletes theOutput.txt file.

On the program's second run ( or anytime after
the Encrypted.enc file has been created ), you should load the file by clicking
on the Load File button. Here you should select the name of the encrypted
file you've saved - in our case, Encrypted.enc, as shown in Figure 4.

The Password box displays again, and only if you
have clicked in the correct password, it will load the file, as displayed in
Figure 5. If you've clicked in the wrong password, you will be notified as
Figure 6 shows.

With this article, I'm including my Encrypted.enc
file with, the Password that you have to click in is HTG

[frmPass.png]

Figure 3 - frmPass, during run time

[OpenFile.png]

Figure 4 - Open Your File

[FileLoaded.png]

Figure 5 - The file is loaded

[WrongPass.png]

Figure 6 - Wrong password notification

Conclusion

I sincerely hope you have enjoyed this lesson on
using the Rijndael Algorithm. With the next parts I will try to explore
more ways of doing encryption, and protecting your sensitive data. Until
next time, cheers!

About the Author

Hannes du Preez

Hannes du Preez is a Microsoft MVP for Visual Basic. He is a trainer at a South African-based company. He is the co-founder of hmsmp.co.za, a community for South African developers.