Home Security Page 3 - An Introduction to Cryptography

# First Example: Byte Adder Algorithm - Security

In the last few decades the science and study of cryptography has earned an outstanding reputation due to its insane applicability and efficiency. Cryptography is the science of message secrecy. Its importance is easily explicable -- it is used everywhere: online purchasing, secured money transfers, cellular phones, broadcast of TV channels, emails, confidential data, and so forth. Our life would be quite different without cryptography.

Rating:  / 7
July 23, 2007

SEARCH DEV SHED

TOOLS YOU CAN USE

In a nutshell, here is the encryption process: we will open the file and linearly add N number of bytes to each of its bytes. The decryption process is reversed: we will subtract N number of bytes from each byte. Throughout this example the number N stands for the key. It might be a user-specified "whole number" or can be generated from a specific string (we are going to discuss this variation in the second example later on). To make this example even simpler we will request an int' for N.

The algorithm is quite easy. Check it out:

while (s.get(origbyte))

{

byte=(int)origbyte+digits;

d.put(byte);

}

In the above algorithm string "s" stands for source and "d" for destination. Digits is a numeric integer; it represents the numbers of bytes that are going to be added. The algorithm is the same for decryption  the only difference is that we change the sign of the "digits." If it was, say, 50 for encryption, then it is going to be -50 for decryption. Easy.

Now let's run our code to encrypt the following text file with N=50:

This is a text file for testing purposes. Hope you enjoy!

Developer Shed. Test, test, test. sdfsdfsdf. 123456789

The encrypted outcome looks like this:

₯R₯R"R¦ͺ¦RR‘€R¦₯¦ R’§€’‘₯₯`Rz‘’R«‘§R ‘«S?<?<v¨
‘’€R`R₯¦^R¦₯¦^R¦₯¦`R₯₯₯`Rcdefghijk

Here is the full code:

#include <fstream.h>        //or <fstream> and using namespace std;'

#include <stdio.h>

#include <string.h>

char srcfile[30], dstfile[30];

int digits;

void main()

{

printf("Source file: "); scanf("%s",srcfile);

printf("nDestination file: "); scanf("%s",dstfile);

printf("nN= "); scanf("%d",&digits);

printf("nPress 1.> if Encryption or 2.> if Decryption: ");

int opt;

scanf("%d",&opt);

if (opt==2) digits*=-1;                      // or digits=-digits;

ifstream s(srcfile, ios::binary);

ofstream d(dstfile, ios::binary);

if (!s || !d)

printf("nERROR: Couldn't open one of the files! ");

char origbyte, byte;

while (s.get(origbyte))

{

byte=(int)origbyte+digits;

d.put(byte);

}

s.close();  d.close();

printf("nAll done. ");

}