A more indepth article About the basics to encryption


A more indepth article About the basics to encryption by Axion
Requirements:
C compiler (optional)
Windows Calculator (for binary to decimal conversions etc)

In this article I will teach you howto write the simplest form of encryption so that others wont be able to read your stuff. This tutorial will be a great start for anyone interested in cryptography or learning more about the C language. Lets get started.


Normally when somebody programs an encryption program they make a program that takes a key from the user and then shoves this key into every character throughout the file. At a lower level each byte in the file being encrypted is being modified by a logical operator. This operator could be AND, OR, ones complimentary or XOR. All of these operators modify a byte bit for bit. Here is a simple example:

int a,b;
a = 8;
b = 3;

This code declares to variables 'a' and 'b' and assigns that values 8 and 3 to them respectively. It is now possible to change these variables so that they are blended with another number. Do not be confused by this it is rather
simple. From the above example you have two numbers 8 and 3. To truely understand how these numers can be blended together using logical operators we must break them down to binary level. Doing this gives us 1000 and
0011The top numbwer is 8 and the lower 3. We can now use logical operators to alter these variables at a binary
level. In the C language the logical operators are as follows:

| OR
^ XOR
& AND
~ Ones complementary <-- remember this.

I am not going to describe each of these as that is not the purpose of this article but I will give an example of both XOR and ~ Ones complimentary. In C you can xor both of the above variables together by doing this:

int c;
c = a ^ b;

The result of the blended variables will then be left in the c variable. Lets see what is going on at a lower level:

int a,b,c;
a = 8; /* 1000 in binary */
b = 3; /* 0011 in binary */
c = a ^ b; /* 1011 in binary */

As you can see the XOR operator goes through each bit in a number and evaluates that bit with the corresponding bit it is being xored with. Here is how XOR works:

If 1 is Xor'd with 1 the result is 0
If 1 is Xor'd with 0 the result is 1
If 0 is Xor'd with 0 the result is 0
If 0 is Xor'd with 1 the result is 1

Therefor with our two numbers (8 and 3):

1 0 0 0
C = ---------------------------
0 0 1 1

C will equal the result, after XORING each of these numerical values together. So when we evaluate we go top down then move one to the right. To find out what 1000 XOR 0011 is we do this:
Evaluate

1 XOR 0 = 1
0 XOR 0 = 0
0 XOR 1 = 1
0 XOR 1 = 1

Therefor the binary value saved into the C variable is 1011 which is 11 in decimal. Here are some other examples to enforce your understanding:

0101 (5 in decimal)
XOR 0111 (7 in decimal)
----
0010 (2 in decimal)

0001 (1 in decimal)
XOR 0111 (7 in decimal)
----
0110 (6 in decimal)

You may be wondering why anybody would possibly want to edit variables in this way. Your about to find out. As you should know all computer files are just large strings of characters. Because of this, we may now look at characters at a binary level and also change their values so that they are no longer the same character. Luckily, for our benefit we may then reverse what we have done to the character in order to be able to read it again. After we have done, we will be able to turn sentences like this:

They are after me. I am in the 2nd building. Luckily we encrypted this or they would of found my location.

into this

D#RWe9#ae#enCe2kSeDFhsad3e3dfjh29$DSFG

I just made that up but this is how it would look. Your sentence after each character has
been blended with a value will not at all be readable. This is how governments change their messages so people can not read their secret files!

Ok so now we will learn how characters are XORED. You know howto XOR a number right? Good well a character has a number representation. These numbers begin at 65 for A and count for another 26 digits until Z is reached at 90. The lower case alphabet then begins at 97 and continues until 122. So by breaking down these numerical representations for each number we can see that A is 65 and hence equal to the binary number 1000001 and Z is equal to 1011010.

So lets see what happens when we XOR the characters A and Z together:

char first = 'A'; /* 65 in decimal */
char second = 'Z'; /* 90 in decimal */
char third;

third = first ^ second; /* 65 XOR 90 */
this results in this evaluation:

1 0 0 0 0 0 1 (65 in decimal)
XOR --------------------------------------------------
1 0 1 1 0 1 0 (90 in decimal)


So all we have to do to find what result will be left in the third variable is go through each bit of each number and evalutate it with the coresponding bit in the number being
XORED against it as follows:

1 XOR 1 = 0
0 XOR 0 = 0
0 XOR 1 = 1
0 XOR 1 = 1
0 XOR 0 = 0
0 XOR 1 = 1
1 XOR 0 = 1

Therefor the decimal result left in the third variable will be 0011011 which when converted
back to its character form is decimal 27 and therefore character: 
As you can see the resultant was a non viewable character :) But thanks to the way XOR works we can turn this unviewable character back into viewable form by XORING this unviewable character with the initial value XORED into the first variable. So if we want to convert back to viewable format:

char second = 'Z';
char third; /* Holds decimal value 27 which is an unviewable character */
char viewable = second ^ third; /* Lets get our initial character back :) */

The variable viewable will then hold the result of the second variable being XORED with the unviewable character. At a binary level it looks like this:

(Z = 90) 1 0 1 1 0 1 0
( = 27) 0 0 1 1 0 1 1
---------------------------------------------------
RESULT (XOR) 1 0 0 0 0 0 1

As you can see we have XORED each bit of each number from 90 with 27 at a binary level. This has given us the resultant number 65 in decimal which is 'A' in character form. In this tutorial I am merely explaining the concepts rather than showing how to do it. If you want to see how XOR encryption can be used please read my Encryption Programming tutorial at http://www.axion.ozweb.nu. Ok so now you know how XOR works and your probably thinking how easy it would be to make a program using it. I will now draw your attention to a much more simply way to 'mess up' your old document so that it's not readable. This would not be called a method of encryption, but rather a cipher technique. All we will be doing is getting the decimal value from a character and going through bit by bit this number, inverting each bit. We will use another logical operator ~ <-- which is the Ones Compliment operator. At a binary level this operator takes every bit in a number, and inverts those bits. Ofcourse, a completely different number will result from this and hence a different character will be seen. This is a very very weak form of data hiding but it's still better than nothing. Ok observ how Ones Complimentary works on the character 'A' below:

char first = 'A'; /* 1000001 in binary */
char second = ~first; /* 0111110 in binary */

Basically, ~ will turn 0 bits into 1's and 1 bits into 0's. Simple hey? Ok so now we have the binary number 0111110 in the second variable. The decimal value for this binary number is 62. If 'A' is the start of the alphabet and it has the decimal value of 65 then a character with decimal value 62 is ofcoarse "unreadable" :)

So by inverting each bit in each character you are turning what was once readable into junk. Because Ones Complimetary is so simple, turning this junk back into useful data is as simple as applying Ones Compliment to the result as seen below:

char first = ~second; /* 1000001 <-- 'A' */

Now that you understand these principles you would be able to write a program that uses XOR and Ones complimentary to ensure your privacy. For a more indepth look at Cipher please read Psyq's Cipher tutorial at the Axion Network Tutorials Site (www.axion.ozweb.nu).


Credits


If you find any errors in this tutorial please email axionis@hotmail.com.
-Axion