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

 
Developer Updates  
Free Website Content 
IBM Developerworks
 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: 5 stars5 stars5 stars5 stars5 stars / 5
    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:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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

    TestComplete™ automates software testing for a fraction of what the big guys charge. Easy functional and load testing for all Windows, .NET, Java and Web apps. Download a free trial now.

    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


       · I hope that you've enjoyed this introduction into cryptography. Be prepared for the...
     

       

    SECURITY ARTICLES

    - 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
    - Trust, Access Control, and Rights for Web Se...
    - Basic Concepts of Web Services Security
    - Safeguarding the Identity and Integrity of X...

     
    Accelerating Trading Partner Performance
     
    Competing on Analytics
     
    Cost Effective Scaling with Virtualization and Coyote Point Systems
     
    Five Checkpoints to Implementing IP Telephony
     
    Hosted Email Security: Staying Ahead of New Threats
     




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway