57.

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 DPOLY2.CPP Program

In concluding this chapter, dual coverage of BASIC and C++ programs will be continued by turning your attention to the C++ version of DPOLY2. That program, which has the filename DPOLY2.CPP, is contained on the CD-ROM in the C directory.

Listing 5.10 contains a listing of the statements in the header and the main function of DPOLY2.CPP. In examining the listing shown in Listing 5.10 you will note that many functions used in the program POLY2.CPP are reused, such as checkInput, createStream, and createTable. Thus, in this section I will focus your attention on the key difference between the two programs. That difference is primarily in the inclusion of the functions decipher and getFileToDecipher. Listing 5.11 lists the statements in those two functions. Since each function is explicitly documented through the use of comments, I will leave it to you to review the statements for each function.

Listing 5.10 Header and function main statements in DPOLY2.CPP.

 /*DPOLY2.CPP C++ code written by Jonathan Held Using Microsoft’s Visual C++ Version 5.0 */ //standard includes #include<iostream.h> #include<assert.h> #include<string.h> #include<ctype.h> #include<fstream.h> #include<stdlib.h> //constants we will use const int FIVE = 5, TWENTYFIVE = 25, TWENTYSIX = 26,       TWENTYSEVEN = 27, SIXTYFIVE = 65, NINETY = 90,       NINETYTWO = 92, SIZE = 256, BIGSIZE = 1000; //function prototypes bool checkInput(char * &); void createStream(char *, char []); void createTable(char [][TWENTYSEVEN], const char [], const char []); void decipher(ifstream, ofstream, const char[], char[]); void display(char *); void formatData(char []); void getFileNames(char *&, char *&); void getFileToDecipher(const char[], char[][TWENTYSEVEN]); int getInputType(void); int getKeyword(char *&); bool getMessage(char*, char*, char [], const char[], const                   char[][TWENTYSEVEN]); //---------------------------------------------------------------- //Function: main() //Parameters: None //Return Type: int - 0 means program terminated normally //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','\0'};    char *ptext_keyword, *ctext_keyword, *infile, *outfile;   //p_stream is used to help form the alphabetic-shifted cipher   //alphabets   char p_stream[TWENTYSIX] = {'\0'};   //c_stream represented the top row of the polyalphabetic ciphertext   //matrix   char c_stream[TWENTYSIX] = {'\0'};   //table is the actual polyalphabetic ciphertext matrix   char table[TWENTYSIX][TWENTYSEVEN];   char enc_msg[SIZE];   int type_of_input;   bool success = false;   cout << "Enter plaintext keyword or keyword phrase in UPPERCASE: ";   getKeyword(ptext_keyword);   cout << "Enter ciphertext keyword or keyword phrase in UPPERCASE: ";   getKeyword(ctext_keyword);   createStream(ptext_keyword, p_stream);   createStream(ctext_keyword, c_stream);   createTable(table, plaintext, c_stream);   type_of_input = getInputType();   if (type_of_input){      //decipher accordingly      getFileToDecipher(p_stream, table);   }   else {      getFileNames(infile, outfile);      getMessage(infile, outfile, enc_msg, p_stream, table);      cout << "Press return to display the deciphered text." << endl;      display(outfile);   }   return 0; } 

Listing 5.11 Statements in the decipher and getFileToDecipher functions from the program DPOLY2.CPP.

 //---------------------------------------------------------------- //Function: decipher() //Parameters: in - the input file we are deciphering //      out - the output file we are writing the deciphered //                  text to //      PSTREAM - the plaintext alphabet (used to decipher) //      TBL - the polyalphabetic substitution table //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 PSTREAM[], const            char TBL[][TWENTYSEVEN]) {   char enc_file_data[SIZE];   static int which_cipher_alphabet = 0;   //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] == TBL[which_cipher_alphabet%TWENTYSIX][jx])             break;     }         //conditionals for grouping by five and inserting           //new lines           if (!(ix%TWENTYFIVE))              out << endl;           if ((ix!=0) && (!(ix%FIVE))){              out << " " << PSTREAM[jx];           }           else {             out << PSTREAM[jx];           }             which_cipher_alphabet++;         }      }   }   return; }//end decipher() //---------------------------------------------------------------- ///---------------------------------------------------------------- //Function: getFileToDecipher() //Parameters: PSTREAM - the plaintext stream at the top of the //      polyalphabetic substitution table //      TBL - the polyalphabetic table //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 PSTREAM[], char TBL[][TWENTYSEVEN]) {   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, PSTREAM, TBL);   //don't forget to close the files   input.close();   output.close();   cout << "\nDeciphered text is in " << fileoutput << endl;   cout << "\nPress return to display deciphered text." << endl;   cin.get();   cin.get();   display(fileoutput);   return; }//end getFileToDecipher() //---------------------------------------------------------------- 


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