Why
should someone be interested in encryption? Encryption is very usefull for many
purposes. Be it to make your virus harder to detect or to share sensitive
information with friends.

Encryption was used long before the computers were invented. In cases of war
people used to encode their messages so that the enemy wouldn't be able to read
them. Why shouldn't you know how to encrypt if you ever need to?

First
you need to know how someone can decode something that you have already
encrypted. Suppose you encrypt a sentence by changeing each character to the
next.

"I hate to eat
vegetables" -----> "J!ibuf!up!fbu!wfhfubcmft"

One can use the following table with the frequency of different letters:

T ( 9 % )

in ( 1.5 % )

and ( 3.1 % )

o ( 8 % )

to ( 2.3 % )

A ( 7.8 % )

E ( 13 % )

the ( 5 % )

of ( 4 % )

There
are many more letters and words that i could add to this table but it is
pointless. Back to our example, look how many times the letter "f"
appears in the encrypted version. It is the most frequent letter, just like
"E" in our table. In bigger tables it is even easier to spot these
patterns.

DES Encryption

In
this subsection of the introduction we will discuss a bit about the DES
Encryption method. DES stands for Data Encryption Standard. Even though the DES
encryption algorythm has been publically released no-one has managed to break
the system as far as we know. What makes DES so strong is that it uses many
functions on the numbers together as to make a joined strong function. The
functions that it uses are those of substitution and transferring and many other
advanced techniques.

DES
works on every bit of the message and not the byte like most other algorytms. It
first takes a 64bit segment of the message and it uses a key to start
manipulating it. Then the 64bit segment of 8 characters is divided in two
smaller segments of 4 characters each and the process of encryption is repeated
16 times, then the small segments are joined again and the process is repeated
for the last time.

RSA
Encryption

It
would be a shame not to include RSA in this tutorial since I love this
encryption algorythm. The RSA method uses mathematical operations to encrypt.
Each user has his own private and public key. If someone wants to send me a
message for example, they will my public key to encrypt the message and I will
use my private key to decrypt it.

RSA is
based on prime numbers. When two prime numbers are multiplied then that product
can only be divided into those two prime numbers and not any other ones. But if
the product is an extremely large number it is near impossible to find out which
two prime numbers were used to make it up. This is what RSA is counting on.
Below is how the pair of private and public keys are created:

1. The system picks two
numbers 1024bits long which satisfy certain conditions.

2. The two numbers are
multiplied, and the product is used as the first half of the pubkic key.

3. An odd number that
satisfys certain conditions is picked and this number is used as the second half
of the public key.

4. We subtract 1 from
each of the numbers chosen, the prime numbers and the other odd number. Then
these numbers are multiplied and we add 1 to the product.

5. The the number that we
get in (4) is divided with the number that we got in (3) and this is our private
key.

EXERCISES:

1. How many bits are 8
characters?

2. Find the the only two
prime numbers that divide exactly 65, and hence find the private key of the
public key 65 3.

Chapter
2: Xor

Xor is
the most famous encryption method out there probably because of it's ease to
use. You can use the same function to encrypt and decrypt. Xor is a bitwise
logical operator. Consider the example below:

00000001 is the binary
equivelant of the decimal 19

00001000 is the binary
equivelant of the decimal 16

Xor 00001011

00001000

------------------------------

00000011 which is the binary equivelant of the decimal 3

Now lets Xor the result
from the previous action with anyone of the original two numbers. Lets use 19.

00000011

00001011

-----------------------

00001000
which is the same as the second part of our first action.

Xor
returns 1 if ONLY ONE of the bits compared is set on 1. Below is some visual
basic source code on how to perform simple Xor encryption:

Sub Encrypt()

key =
"4"

mystring = "abcdef"

l =
len(mystring)

for i
= 1 to l

char1 = asc(mid(mystring, i, l))

newchar = char1 Xor key

newmsg = newmsg + chr(newchar)

next i

end sub

Now
lets go through it:

######################

Sub
Encrypt()
#

key =
"4"
#

mystring = "abcdef" #

l =
len(mystring) #

#######################

Here
we start our sub and we initialise the variables. The Xor key in this example
can only be up to 255. In a following chapter we will see a more advanced method
that allowes you to use any key you want, and we will discuss a problem with
Xor. mystring is the string that we will encrypt and l holds the length of our
string.

###############################

for i
= 1 to
l
#

char1 = asc(mid(mystring, i, l)) #

newchar = char1 Xor
key
#

newmsg = newmsg + chr(newchar) #

next
i
#

end
sub
#

##############################

Here
we start a For... Next... Loop which will perform the same action for every
character in our string. First we get one character and we change it to its
ASCII equivelant. Then we Xor it using our key and then we make it back into a
character and add it to our new string. When the loop ends the new encrypted
message will be stored in the newmsg variable. To decrypt it just change the
"mystring" variable with the encrypted text.

EXERCISES:

1. Xor 00101110 and
11011101 manually.

2. What is the encrypted
value of "I love coderz.net" using 8 as a Xor key?

Chapter
3: Substitution

I
don't even know if this is the proper name for this method but I call it
substitution, because we actually substitute one character for another. In this
method we will get the ASCII code of one character and purform some mathematical
operation on it to change it to something else.

For
example, the ASCII code of "A" is 65. If we get this ASCII code and we
add 4 to it we will get 69 (nice number, huh?). This number corresponds to E. As
you can probably understant there are endless mathematical operations that u can
do. Like add 3 then multiply by 4 and then divide by 3, or anything you can
think of, as long as you keep in mind that the maximum a number should be is
255.

Now
how do we do this in VB? Easy:

Sub Encrypt()

mystring = "abcdef"

l =
len(mystring)

for i
= 1 to l

char1 = asc(mid(mystring, i, l))

newchar = char1 + 4

newmsg = newmsg + chr(newchar)

next i

end sub

Sub Decrypt()

mystring = "abcdef"

l =
len(mystring)

for i
= 1 to l

char1 = asc(mid(mystring, i, l))

newchar = char1 - 4

newmsg = newmsg + chr(newchar)

next i

end sub

Now
lets go through it:

######################

Sub
Encrypt()
#

mystring = "abcdef" #

l =
len(mystring) #

#######################

Here
we start our sub and we initialise the variables. mystring is the string that we will encrypt and l holds the length of our
string.

###############################

for i
= 1 to
l
#

char1 = asc(mid(mystring, i, l)) #

newchar = char1 + 4
#

newmsg = newmsg + chr(newchar) #

next
i
#

end
sub
#

##############################

Here
we start a For... Next... Loop which will perform the same action for every
character in our string. First we get one character and we change it to its
ASCII equivelant. Then we add 4 to it (it can be anything) and then we make it back into a
character and add it to our new string. When the loop ends the new encrypted
message will be stored in the newmsg variable. To decrypt it just change the
"mystring" variable with the encrypted text.

Then
to decrypt it we use the same function except that we subtract 4 instead of
adding it to make it back to it's original character.

EXCERCISES:

1.
Encrypt the message "I am not lame" by adding 3 to each character.

Chapter
4: Adding characters

This
is not an encryption method per se but you can use it to disguise your message.
What this basically does is that it adds some characters between each letter.
Lets look straight to the code:

In
this chapter I will not give any excersises because as you can see everything is
very straight forward. You may be able to improve on the decryption part because
i wrote it late at night but this works just fine as it is.

Chapter
5: Advanced

First
of all lets discuss some more things about Xor. Did you know that even though
Xor is a very nice method of encryption it has a very big flow? Try encrypting
something with the Xor method that we discussed in chapter 2. Works fine right?
Now try encrypting a string containing many 0s (zeros). That's right the result
you get is the Xor key that you have used. To cover up for this I suggest that
you use Xor as well as substitution in the same function so that any errors due
to Xoring zeros can be avoided.

Another thing that we will discuss here is how to use a Xor key that can even be
composed by letters and not only a number less than 255. This way your Xor key
can be something like "Fuck this piece of shit tutorial 375185423196"
and it will not have a problem.