Search Tools Links Login

Data Encryption - For Beginners

Explains the basic technique used to achieve data encryption and get you on your way to understanding Cryptography in general.

Original Author: Jonathan Roach



mso-bidi-font-family:"Times New Roman"'>Cryptography Primer

By Jonathan Roach



Hello and welcome to
my primer article on cryptography!


"Times New Roman"'>cryp-tog-ra-phy (krip-taw-graph-e)

"Times New Roman"'>The process or skill of communicating in or deciphering

"Times New Roman"'>secret writings or ciphers.
"Times New Roman"'>



mso-bidi-font-family:"Times New Roman"'>Introduction


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



mso-bidi-font-family:"Times New Roman"'>What does Cryptography do?

mso-bidi-font-family:"Times New Roman"'> 

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:


style='mso-tab-count:1'> "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:


style='mso-tab-count:1'> "’ˆ/žŸ“ˆ ??|“œˆstyle='font-size:9.0pt;mso-bidi-font-size:12.0pt;font-family:Gulim'>?style='font-size:9.0pt;mso-bidi-font-size:12.0pt;font-family:Tahoma;mso-bidi-font-family:
"Times New Roman"'>?† ˆ?|“œ?†"


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.



mso-bidi-font-family:"Times New Roman"'>Cryptography Overview

mso-bidi-font-family:"Times New Roman"'> 

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.



mso-bidi-font-family:"Times New Roman"'>Common Cryptography Algorithms

mso-bidi-font-family:"Times New Roman"'> 

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.



mso-bidi-font-family:"Times New Roman"'>The One-Time Pad

mso-bidi-font-family:"Times New Roman"'> 

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

Dim cipherKey As

Dim Counter As


Dim Char As String

Dim keyChar As

Dim cipherText As


Private Sub Crypt()

plainText =

cipherKey =

MsgBox “Before: “
& plainText

'Encrypt it

For Counter = 1 To

style="mso-spacerun: yes"> Char = Asc(Mid(plainText, Counter, 1))

style="mso-spacerun: yes"> keyChar = Asc(Mid(cipherKey, Counter, 1))

style="mso-spacerun: yes"> cipherText = cipherText & Chr(Char Xor

Next Counter

MsgBox “After: “
& cipherText


plainText =


'Decrypt it

For Counter = 1 To

style="mso-spacerun: yes"> Char = Asc(Mid(cipherText, Counter, 1))

style="mso-spacerun: yes"> keyChar = Asc(Mid(cipherKey, Counter, 1))

style="mso-spacerun: yes"> plainText = plainText & Chr(Char Xor

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


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


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.



mso-bidi-font-family:"Times New Roman"'>Repeating Key Algorithm - Viginere

mso-bidi-font-family:"Times New Roman"'> 

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

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:


style='mso-tab-count:1'> plaintext char = Istyle='mso-tab-count:1'> key
char = P

style='mso-tab-count:1'> plaintext char = Spacestyle='mso-tab-count:1'> key char = a

style='mso-tab-count:1'> plaintext char = hstyle='mso-tab-count:1'> key char = s

style='mso-tab-count:1'> plaintext char = astyle='mso-tab-count:1'> key char = s

style='mso-tab-count:1'> plaintext char = vstyle='mso-tab-count:1'> key char = P

style='mso-tab-count:1'> plaintext char = estyle='mso-tab-count:1'> key char = a

style='mso-tab-count:1'> plaintext char = Spacestyle='mso-tab-count:1'> 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 =

plainText =
"Top-Secret Message from Roach"

cipherText =

KeyIndex = 1

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

For Counter = 1 To

style="mso-spacerun: yes"> Char = Asc(Mid(plainText, Counter, 1))

style="mso-spacerun: yes"> keyChar = Asc(Mid(cipherKey, KeyIndex, 1))

style="mso-spacerun: yes"> cipherText = cipherText & Chr(Char Xor

style="mso-spacerun: yes"> KeyIndex = KeyIndex + 1

style="mso-spacerun: yes"> 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

About this post

Posted: 2002-06-01
By: ArchiveBot
Viewed: 90 times


Visual Basic 6


No attachments for this post

Loading Comments ...


No comments have been added for this post.

You must be logged in to make a comment.