Home arrow Security arrow 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.

TABLE OF CONTENTS:
  1. An Introduction to Cryptography
  2. The Study of Cryptography
  3. First Example: Byte Adder Algorithm
  4. Second Example: Variation of the Byte-Adder
By: Barzan "Tony" Antal
Rating: starstarstarstarstar / 7
July 23, 2007

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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. ");

}

If you want to download the compiled executable and the above source instead of copying, pasting and compiling it yourself, just click on the attached download button.



 
 
>>> More Security Articles          >>> More By Barzan "Tony" Antal
 

blog comments powered by Disqus
   

SECURITY ARTICLES

- Secure Your Business for Data Privacy Day
- Google Testing Security Fob Password Alterna...
- Security News Highlights Concerns
- Going to Extremes for Data Security
- Skipfish Website Vulnerability Scanner
- Critical Microsoft Visual Studio Security Pa...
- US Faces Tech Security Expert Deficit
- LAN Reconnaissance
- An Epilogue to Cryptography
- A Sequel to Cryptography
- An Introduction to Cryptography
- Security Overview
- Network Security Assessment
- Firewalls
- What’s behind the curtain? Part II

Developer Shed Affiliates

 



© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap

Dev Shed Tutorial Topics: