29.

Learn Encryption Techniques with BASIC and C++
(Publisher: Wordware Publishing, Inc.)
Author(s): Gil Held
ISBN: 1556225989
Publication Date: 10/01/98

Previous Table of Contents Next


The DCIPHER4.CPP Program

In concluding this chapter we will turn our attention to the C++ version of DCIPHER4.BAS. This program, which is appropriately named DCIPHER4.CPP, is included on the accompanying CD-ROM under the C directory. In addition, the executable version of the program, named DCIPHER4.EXE, is also included in that directory.

Listing 2.20 contains the contents of the DCIPHER4.CPP program. This program is very similar to previously developed C++ programs, using functions to perform specific tasks. In this program you will note that constants were defined using mnemonics that are associated with constant values. This coding technique was used to facilitate any changes in coding readers may wish to effect.

Listing 2.20 The DCIPHER4.CPP program listing.

 /* dcipher.cpp   C++ code written by Jonathan Held using Microsoft Visual C++, version5.0, on February 22, 1998. */ //standard include files #include<iostream.h> #include<fstream.h> #include<stdlib.h> #include<string.h> //function prototypes void getShiftKey(char &); void createCipher(const char[], char[], const char); void formatData(char []); void getFileToDecipher(const char[], char[]); void decipher(ifstream, ofstream, const char[], char[]); //constants we will use #define FIVE          5 #define TWENTYFIVE   25 #define TWENTYSIX    26 #define TWENTYSEVEN  27 #define SIXTYFIVE    65 #define NINETY       90 #define SIZE        256 //---------------------------------------------------- //Function: main() //Parameters: None //Return Type: int - 0 execution is normal, 1 abnormal termination //Purpose: Runs the main part of the program. //---------------------------------------------------- int main() {   char plaintext[TWENTYSEVEN]=  {'A','B','C','D','E','F','G','H',                     'I','J','K','L','M','N','O','P',                         'Q','R','S','T','U','V','W','X',                         'Y','Z'};   char ciphertext[TWENTYSEVEN], key;   //get the shift key that was used   getShiftKey(key);   //create the cipher text based on the shift key   createCipher(plaintext, ciphertext, key);   //decipher the file   getFileToDecipher(plaintext, ciphertext);   return (0); } //---------------------------------------------------- //Function: getShiftKey() //Parameters:  key_desired - uppercase key entered by the user //Return Type: None //Purpose: Get the key the user enters; error checking performed //until user enters a valid value. //---------------------------------------------------- void getShiftKey(char &key_desired){   bool error = true;   do {      //prompt user to enter an uppercase shift key      cout << "Enter UPPERCASE Alphabetic Shift Key (CTRL-C to quit): ";      cin >> key_desired;      int key_value = static_cast<int>(key_desired);      //do some error checking      if ((key_value < SIXTYFIVE) || (key_value > NINETY)){         cerr << "\nYou must enter a letter from A to Z!" << endl << endl;      }      else {         cout << endl;         error = false;      }   } while (error);   return; }//end getShiftKey() //---------------------------------------------------- //Function: createCipher() //Parameters: PTEXT - the plaintext alphabet //      ctext - the cipher alphabet we are going to create //      user_key - the key the user entered //Return Type: none //Purpose: Create the cipher stream we will use later to encode the //user's message. //---------------------------------------------------- void createCipher(const char PTEXT[], char ctext[], const char USER_KEY){   int location;   //find the location of the key in the plaintext   for (int ix=0; ix<TWENTYSIX; ix++){      if (USER_KEY == PTEXT[ix]){          //location is one more than ix         location = ix + 1;         break;      }   }   //create the cipher text   for (int jx=0; jx<TWENTYSIX; jx++){      ctext[jx] = PTEXT[(jx + location) % TWENTYSIX];   }   return; }//end createCipher(); //----------------------------------------------------- //Function: formatData() //Parameters: data - the array we want to format //Return Type: None //Purpose: Get rid of all spaces in the array. //----------------------------------------------------- void formatData(char data[]){   for (int mx=0, nx=0; (*(data+nx) != '\0'); nx++){      if (*(data+nx) == ' '){         //do nothing - skip over the space in the data      }      else {         *(data+mx++) = *(data+nx);      }   }   //don't forget to add the null terminator   *(data+mx) = '\0';   return; }//end formatData() //----------------------------------------------------- //Function: getFileToDecipher() //Parameters: PTEXT - the plaintext alphabet //            ctext - the corresponding cipher text //Return Type: None //Purpose: Prompt the user for the name of the encrypted file //and the file the user wants to store the decrypted text to. //----------------------------------------------------- void getFileToDecipher(const char PTEXT[], char ctext[]) {   char fileinput[SIZE], fileoutput[SIZE];   cout << "Enter name of file to decipher: ";   cin >> fileinput;   ifstream input(fileinput, ios::in);   if (!(input)){      cerr << "Input file not available. Exiting program." << endl;      exit(EXIT_FAILURE);   }   cout << "Enter name of file for output: ";   cin >> fileoutput;   ofstream output(fileoutput, ios::out);   if (!(output)){      cerr << "Output file not created.  Exiting program." << endl;      exit(EXIT_FAILURE);   }   decipher(input, output, PTEXT, ctext);   //don't forget to close the files   input.close();   output.close();   cout << "\nDeciphered text is in " << fileoutput << endl;   return; }//end getFileToDecipher() //----------------------------------------------------- //Function: decipher() //Parameters: in - the input file we are deciphering //      out - the output file we are writing the deciphered //        text to //      PTEXT - the plaintext alphabet (used to decipher) //      ctext - the ciphertext //Return Type: None //Purpose: Decipher the input file and write the contents to the //output file specified by the user. //----------------------------------------------------- void decipher(ifstream in, ofstream out, const char PTEXT[], char ctext[]) {   char enc_file_data[SIZE];   //continue this process until we get to the end of the file   while (in.getline(enc_file_data, SIZE, '\n')){      if (enc_file_data[0] == '/'){         out << enc_file_data << endl;      }      else {         //format the data - i.e. get rid of all spaces      formatData(enc_file_data);      //dump data to file      for (int ix=0; ix<strlen(enc_file_data); ix++){         //used to keep track of what plaintext character       //we are going to use      int jx;      for (jx=0; jx<TWENTYSIX; jx++){      //find where the encrypted data is in the      //ciphertext - this location corresponds to      //the plaintext character location        if (enc_file_data[ix] == ctext[jx])        break;        }      //conditionals for grouping by five and inserting   //new lines   if (!(ix%TWENTYFIVE))            out << endl;         if ((ix!=0) && (!(ix%FIVE))){            out << " " << PTEXT[jx];         }         else {            out << PTEXT[jx];         }      }      }   }   return; }//end decipher() //end file dcipher.cpp 

The DCIPHER4.CPP program includes five functions as indicated in the function prototype section in the listing. getShiftKey obtains the desired uppercase key entered by the user and createCipher creates the cipher stream that would normally be used to encode a message. The function getFileToDecipher, as its name implies, prompts you for the name of the encrypted file and the file to be used to store the results of the decryption operation. The function decipher deciphers the contents of the input file and writes the results to the specified output file. This function invokes the function formatData for data formatting. In addition, the decipher function uses a for jx statement to locate the encrypted data in the ciphertext since the location corresponds to the plaintext character location. This operation requires the use of a pointer into the ciphertext array and explains why the function createCipher is used to create a cipher alphabet. DCIPHER4.CPP is similar to the execution of DCIPHER4.BAS where, with the exception of an option for typing input from the keyboard, the execution of the C++ program is left for you to perform.


Previous Table of Contents Next


Learn Encryption Techniques with Basic and C++
Learn Encryption Techniques with BASIC and C++
ISBN: 1556225989
EAN: 2147483647
Year: 2005
Pages: 92
Authors: Gil Held

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net