Search Tools Links Login

Data Encryption - For Beginners


Visual Basic 6, or VB Classic

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

Original Author: Jonathan Roach

Code

xmlns:w="urn:schemas-microsoft-com:office:word"
xmlns="http://www.w3.org/TR/REC-html40">






style='font-size:18.0pt;mso-bidi-font-size:12.0pt;font-family:Tahoma;
mso-bidi-font-family:"Times New Roman"'>Cryptography Primer


By Jonathan Roach


 


 


Hello and welcome to
my primer article on cryptography!


 


style='font-size:9.0pt;mso-bidi-font-size:12.0pt;font-family:Tahoma;mso-bidi-font-family:
"Times New Roman"'>cryp-tog-ra-phy (krip-taw-graph-e)


style='font-size:9.0pt;mso-bidi-font-size:12.0pt;font-family:Tahoma;mso-bidi-font-family:
"Times New Roman"'>The process or skill of communicating in or deciphering


style='font-size:9.0pt;mso-bidi-font-size:12.0pt;font-family:Tahoma;mso-bidi-font-family:
"Times New Roman"'>secret writings or ciphers.
style='font-size:9.0pt;mso-bidi-font-size:12.0pt;font-family:Tahoma;mso-bidi-font-family:
"Times New Roman"'>


 


 


style='font-size:11.0pt;mso-bidi-font-size:12.0pt;font-family:Tahoma;
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


individuals/organizations.


 


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.


 


 


style='font-size:11.0pt;mso-bidi-font-size:12.0pt;font-family:Tahoma;
mso-bidi-font-family:"Times New Roman"'>What does Cryptography do?


style='font-size:11.0pt;mso-bidi-font-size:12.0pt;font-family:Tahoma;
mso-bidi-font-family:"Times New Roman"'> 


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:


 


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.


 


 


style='font-size:11.0pt;mso-bidi-font-size:12.0pt;font-family:Tahoma;
mso-bidi-font-family:"Times New Roman"'>Cryptography Overview


style='font-size:11.0pt;mso-bidi-font-size:12.0pt;font-family:Tahoma;
mso-bidi-font-family:"Times New Roman"'> 


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


(ciphertext).


 


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.


 


 


style='font-size:11.0pt;mso-bidi-font-size:12.0pt;font-family:Tahoma;
mso-bidi-font-family:"Times New Roman"'>Common Cryptography Algorithms


style='font-size:11.0pt;mso-bidi-font-size:12.0pt;font-family:Tahoma;
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.


 


 


style='font-size:11.0pt;mso-bidi-font-size:12.0pt;font-family:Tahoma;
mso-bidi-font-family:"Times New Roman"'>The One-Time Pad


style='font-size:11.0pt;mso-bidi-font-size:12.0pt;font-family:Tahoma;
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
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)


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
keyChar)


Next Counter


MsgBox “After: “
& cipherText


 


plainText =
""


 


'Decrypt it


For Counter = 1 To
Len(cipherText)


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
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()


Crypt


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.


 


 


style='font-size:11.0pt;mso-bidi-font-size:12.0pt;font-family:Tahoma;
mso-bidi-font-family:"Times New Roman"'>Repeating Key Algorithm - Viginere
Cipher


style='font-size:11.0pt;mso-bidi-font-size:12.0pt;font-family:Tahoma;
mso-bidi-font-family:"Times New Roman"'> 


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


length.


 


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


anyway.


 


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)


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
keyChar)


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.


 


Regards,


Jonathan Roach




About this post

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

Categories

Visual Basic 6

Attachments

No attachments for this post


Loading Comments ...

Comments

No comments have been added for this post.

You must be logged in to make a comment.