Topic : XOR Encryption Tutorial
Author : Kias Henry
Page : << Previous 2  Next >>
Go to page :


TIP: Xor is good for encryption because applying it with the old value it
was XORED with turns the bits back into their previous value:


We have two characters:
'a' and 'b'
We want to encrypt 'a' by XORING the bits of 'b' into it.
Do this:

int encrypted;
char a,b;
a = 'a';
b = 'b';

  encrypted = a ^ b;

Which will yield this result:
'a' = 97 which is      01100001 in binary
'b' = 98 which is      01100010
encrypted now equals:  00000011

But were learning how to decrypt as well right?
Sure are, to restore the value 'a' to its original unencrypted state
we simple xor the encrypted value with 'b' again:

int new;
new = encrypted ^ b;

which gives us this binary number: 01100001 which is, you guesses it, 'a'!

So now we know the basic component behind the encryption of characters.
Continue on to learn how to encrypt a whole file of strings, numbers etc.

1.5 Structured C Programming

When writing code in any language it is important to write clear, efficient
code that is well documented and easy to read, here are some tips:

1: To make readability of code easier, #define values at the start
    of the file that can be reused throughout the code, here's an example:
    #define SIZE 5
    int array[SIZE] = {3,4,5,6,7};

2: Try to use pointers wherever possible, observe:
/* Pathetic version, recopies itself, inefficient */
   int z;
    z = 10;
     func(z); // create copy of z two times.
int func(int value)
  return = value * value; // return the square of a number


/* Good version, simply refers to memory locations, and does not copy
   variables, this is named call by reference whereas the first version
   was call by value
int z = 10;
func(&z);     // reference z twice, efficient.
void func(unsigned *value)
  *value *= *value;


3: Allocate Memory Correctly:

You could allocate 1000 of bytes like this, but you cannot free the
used memory.

int array[1000];

Try doing this, it really works!

int *value;
value = (int *) malloc(1000);

Wow, efficient code, finally!

4: Indent your god damn programs!, please!

Instead of this:

int array[] = {3434,4545,34534,656,
  for(i = 0; i <= SIZE -1; i++ ) {
sum += array[i] ;
// add some more unreadable stuff...

well this aint that good of example but its badly structured, observ the same code in structured format:

int array[] = { 3434,4545,34534,656,
for (i = 0; i <= SIZE -1; i++) {
   sum += array[i];
     // some more

You can clearly see what I am on about, and be sure to fix small things like
this, instead of


do this:

while (!a = EOF)
  func1(val, val2, val3) {
    i++; // increment i;
    j++; // dont add unnecessary comments

4: Ok nearly there, one last thing, add a signature, date etc.

There's nothing better then seeing a great program, and seeing the authors
name and contact so you can email them on there great efforts, BE EGOTISTICAL
ACCEPT ALL RECOGNITION FOR YOUR masterpiece. A good signature is as follows:
NAME:  Kias Henry
ICQ:   35614954
DATE:  27/09/2001

Wooooo I know its a big chunk to absorb, but absorb it damnit!
Continue onto the next section, now that your efficient... :)

1.6 Encryption Example
As I promised here's an example :)
Remember the XOR operator ^ ?
Xor operator == ^ in C :)
Were going to write a small documented, structured program that will
encrypt a file with the users key, the file can then be decrypted
with the key. Otherwise the resulting file is encrypted again.
The Encryption in this program is very weak, but to explain more complex
algorithmic is too hard for now, maybe in my next article :)

First, I will go over some features of this program that need be understood
for all you wanna be crypotologists out there!

Heres the code that loops through a file:
while (c = getc(*file) != EOF)   // loop through *file until End of File is reached, each time
encrypt();      // encrypting each character and incrementing it to the next character in
putc(*file,c); // the file stream.

  Here's the algorithm Im using:

c = c ^ *file2;

That's it?
Indeed it is, very small, very powerful..
Without further do, I represent a simple encryption program:
Please note, this is a weak form of encryption as only the first character of the key is actually xor'ed with
each byte of the file.  Fixing this problem would be rather simple, but seeing this is an introduction to encryption
It would be best if you realized this and accepted it.  There will be an upcoming tutorial on more advanced   encryption topics in the next Axion-Network tutorial.

  Standard XOR Encryption By Axion
  UIN  :          35614954
  DATE :          MAY 21st 2000
  USAGE:          xe filein fileout key

#include <stdio.h>

#define PROG_NAME "xe"
void usage()
printf("Invalid command line.\n");
printf("Usage:\n\t%s infile outfile key\n", PROG_NAME);

int main(int argc, char *argv[])
   int count,bytes; /* counter when looping through file, and bytes to count filesize */
   FILE *in,*out;   /* In and out FILE Streams to read/write data */

    if(argc < 4) {  /* Error check the command line */
     usage();       /* Display Usage Information on error */
      return 0;     /* Exit Program returning 0 - no error */

    if (( in = fopen(argv[1], "rb")) == NULL) /* Error Check File Streams */
       printf("Error opening %s.\n", argv[1]);
    if (( out = fopen(argv[2], "wb")) == NULL)
       printf("Error opening %s.\n", argv[2]);

       while(( count = getc(in)) != EOF)
           count = count ^ *argv[3]; /* Apply XOR  ( KEY ) */
           bytes++;    /* Increment counter of filesize */
           putc(count, out);  /* Write new file */

           printf("Encryption Success:\n");
          printf("\tEncrypted %s and stored data in %s.\n", argv[1],argv[2]);
         printf("\tWrote %d bytes to %s.\n", bytes,argv[2]);
   return 0;
/*  To compile under djgpp */
/*  dgjpp xe.c -o xe       */

Well that's it, my tutorial is almost complete..!
But not without a few more challenges, exercises for the smart people:
1. Write an encryptor that uses one complimentary instead of Xor.
2. Write a program that uses two keys on a file.
3. Write a program that uses no logical operators ?
HINT, refer to the ASCII character set.
Lets assume, *s points to ASCII value 'a'.
*s -= 31;
turns it from 'a' to 'A'
This should be enough to

Page : << Previous 2  Next >>