Security
  Home arrow Security arrow Page 3 - An Introduction to Cryptography
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
SECURITY

An Introduction to Cryptography
By: Barzan "Tony" Antal
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 7
    2007-07-23


    Table of Contents:
  • An Introduction to Cryptography
  • The Study of Cryptography
  • First Example: Byte Adder Algorithm
  • Second Example: Variation of the Byte-Adder

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    An Introduction to Cryptography - First Example: Byte Adder Algorithm
    ( Page 3 of 4 )

    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
     

       

    SECURITY ARTICLES

    - 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
    - What’s behind the curtain? Part I
    - Vectors
    - PKI: Looking at the Risks
    - A Quick Look at Cross Site Scripting
    - PKI Architectures: How to Choose One





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    Stay green...Green IT