Data Encryption - For Beginners

Submitted on: 1/21/2015 4:04:00 PM
By: Jonathan Roach (from psc cd)  
Level: Beginner
User Rating: By 11 Users
Compatibility: VB 3.0, VB 4.0 (16-bit), VB 4.0 (32-bit), VB 5.0, VB 6.0, VB Script, ASP (Active Server Pages) , VBA MS Access, VBA MS Excel
Views: 2210
     Explains the basic technique used to achieve data encryption and get you on your way to understanding Cryptography in general.


Cryptography Primer

By Jonathan Roach



Hello and welcome to my primer article on cryptography!


cryp-tog-ra-phy (krip-taw-graph-e)

The process or skill of communicating in or deciphering

secret writings or ciphers.





Prying eyes, espionage, fraud, and theft of personal information.


These are a few of the reasons for concealing, masking, shadowing

or encrypting information in order to minimize the chance of that

information being revealed to potentially dangerous or mischievous



Okay, okay... maybe it's not that big of a deal, maybe you just want

to feel covert when you send email to your friends or something.


The method used to achieve the above is referred to as "Cryptography",

and this article is aimed at giving you a basic look into the world of

data encryption and providing you with Visual Basic source code to get

you started on your way.



What does Cryptography do?


Cryptography conceals or hides data in order to make it un-readable to

the average person, it is used to secure documents and data by mixing

or scrambling the original data into mumbo jumbo basically.


Take this generic example of encryption, lets say you want to send an

email to your friend, and you don't want anyone else to see the true

message you are going to send, because... it's top secret of course.


Your original message would look something like this:


"Hey Frank... I got that new encryption handbook."


You would then perform an encryption routine on the message before

sending your email and the result would look something like this:


"’ˆ/žŸ“ˆ ??|“œˆ??† ˆ?|“œ?†"


You get the idea, the original text is all scrambled and basically not of any

use to anyone, or so it appears...


So now you send your encrypted mail off to Frank, if Frank is unaware of

encryption then he will probably mail you back and say something along

the lines of "What the heck is this stuff you sent me?".


However, because you sent it off to your good buddy Frank and he is

using the same encryption/decryption software that you are, and he is

aware of the code/key needed to reverse your scrambled message all

is well and he can view your message, while any others who may have

intercepted it along the way could not, or at least had a heck of a time

in doing so...


So there you have it, Cryptography scrambles/transforms data.



Cryptography Overview


Cryptography requires an encryption algorithm and a key; in it's basic form

that is.


Due to the nature of this article I will not go into great detail on the many

methods of encryption and key methods in use today but I will provide

you with the base foundation for encryption/decryption.


An encryption algorithm is simply the engine or code that handles all of

the processes that transform the original text (plaintext) into encoded text



This engine simply performs mathematical and/or logical operations on the

plaintext to transform it into the ciphertext and vice versa.


The key as it's name implies is just that, it is the key (code) that allows the

algorithm to encrypt/decrypt data.



Common Cryptography Algorithms


There are many different algorithms for encrypting/decrypting data in use

around the world today, some of them are very complex and others are

more simplistic, however they all serve the same purpose.


Below is a short listing on some of the different cryptography algorithms;


DES - United States Data Encryption Standard

3DES - The above, encoded 3 times

RSA - Rivest, Shamir and Adleman

GOST - Developed by scientists of the former Soviet Union

IDEA - A component of PGP (Pretty Good Privacy)


There are many more, but the above should be a starting point for you

to seek out more info on the net.



The One-Time Pad


The one-time pad is one of the simplest encryption algorithms, it involves

a key being used which is the same length as the plaintext and then using

simple math on the plaintext via the key, the math could be multiplication

or exclusive-or (XOR) for example;


Dim plainText As String

Dim cipherKey As String

Dim Counter As Integer


Dim Char As String

Dim keyChar As String

Dim cipherText As String


Private Sub Crypt()

plainText = "CovertText"

cipherKey = "password42"

MsgBox “Before: “ & plainText

'Encrypt it

For Counter = 1 To Len(plainText)

Char = Asc(Mid(plainText, Counter, 1))

keyChar = Asc(Mid(cipherKey, Counter, 1))

cipherText = cipherText & Chr(Char Xor keyChar)

Next Counter

MsgBox “After: “ & cipherText


plainText = ""


'Decrypt it

For Counter = 1 To Len(cipherText)

Char = Asc(Mid(cipherText, Counter, 1))

keyChar = Asc(Mid(cipherKey, Counter, 1))

plainText = plainText & Chr(Char Xor keyChar)

Next Counter

MsgBox “Back to original: “ & plainText


cipherText = ""

End Sub


Just copy the above code and paste it into a new project, then add a

command button and in its click event put a call to the Crypt() sub.


As follows,


Private Sub Command1_Click()


End Sub


Run the project and click the button to see it in action.



There is a problem with the above encryption algorithm though, first

off if you want to encrypt something that is large in size the key size

would also be very large.


For example if you wanted to encrypt a string that is 50 characters in

length then your key would also have to be 50 characters in length,

kind of a pain for our good friend Frank to have to enter a 50 character

decoding key for a simple message.


This problem can be overcome with our next topic, which deals with

the key length problem by using a repeating key.



Repeating Key Algorithm - Viginere Cipher


This type of encryption algorithm deals with a key that repeats during

the encryption/decryption process, for example the algorithm above

uses a character by character algorithm, it performs math operations

on each character in the plaintext and key until the length of the key

and plaintext is reached - because the key and plaintext are the same



However with a repeating key, our key can be any length we choose

as when the end of the key is reached in our algorithm we simple start

again at the begin of the key until our plaintext encryption is completed.


Take this generic example, if our plaintext is "I have top secret codes"

and our key is "Pass"; obviously the key is shorter than our plaintext.


Thus we are in our loop to encrypt our plaintext and this is how it looks:


plaintext char = I key char = P

plaintext char = Space key char = a

plaintext char = h key char = s

plaintext char = a key char = s

plaintext char = v key char = P

plaintext char = e key char = a

plaintext char = Space key char = s


You get the idea? The key just repeats until the length of the plaintext

is reached. This method is much more practical and flexible for key names



Below is a sample algorithm that uses a repeating key.


Private Sub Crypt()

cipherKey = "pw201"

plainText = "Top-Secret Message from Roach"

cipherText = ""

KeyIndex = 1

MsgBox "Before: " & plainText, 0, "Before Encryption"

For Counter = 1 To Len(plainText)

Char = Asc(Mid(plainText, Counter, 1))

keyChar = Asc(Mid(cipherKey, KeyIndex, 1))

cipherText = cipherText & Chr(Char Xor keyChar)

KeyIndex = KeyIndex + 1

If KeyIndex > Len(cipherKey) Then KeyIndex = 1

Next Counter

MsgBox "After Encryption: " & cipherText, 0, "Original:" & plainText

End Sub


Once again you can paste this into a new project and call the Crypt()

sub from a buttons click event to try it out. To reverse the encryption

just run the encrypted text back through the counter loop in place of

the plaintext.


Thank you for sticking with me through this brief article on the subject,

I hope that you gained a little knowledge about encryption from this.


Note: I do not claim that you shall become an encryption expert or

that any of the methods described in this article are bomb proof,

crack proof, water proof... whatever, I merely wanted to share the

basic knowledge of the subject.



Jonathan Roach

Other 5 submission(s) by this author


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Beginner category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)

Other User Comments

 There are no comments on this submission.

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)

To post feedback, first please login.