54.

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

As a “bonus” for readers who have followed the alphabets in this chapter, I will provide a bonus program in this section in addition to the customary C++ version of a previously explained BASIC program. The bonus C++ program is appropriately named COUNT.CPP as it counts the characters in a file, performing a frequency analysis.

Listing 5.6 contains a listing of the statements in the C++ bonus program. Although the program COUNT.CPP was written to analyze encrypted files that contain uppercase alphabetic characters, you can easily modify it to operate on all characters in the character set by changing its computeFrequencyCount function. This modification is left as an exercise for you to perform.

Listing 5.6 The COUNT.CPP program listing.

 /* count.cpp C++ code written by Jonathan Held, March 27, 1998, using Microsoft's Visual C++ version 5.0. */ //standard include files #include <assert.h> #include <ctype.h> #include <iostream.h> #include <iomanip.h> #include <fstream.h> #include <string.h> //function prototypes bool computeFrequencyCount(const char *&, const char*&); void displayOutput(ofstream, const char *&); void getFileNames(char *&, char *&); void initialize(void); void welcome(void); //constants const int TWENTYSIX = 26, SIXTYFIVE = 65, NINETY = 90,       ONE_HUNDRED = 100, SIZE = 256; //globals int count[TWENTYSIX]; int total_characters = 0; //---------------------------------------------------------------- //Function: main() //Parameters:  None //Return type: int //Purpose: Performs a frequency analysis on encrypted text that //was enciphered using monoalphabetic substitution techniques. //---------------------------------------------------------------- int main() {   bool success = false;   char *inp_file, *op_file;   welcome();   initialize();   getFileNames(inp_file, op_file);   success = computeFrequencyCount(inp_file, op_file);   if (!success){      cerr << "\a\nProblem opening input file and/or "         << "writing to output file!" << endl         << "Program terminating abnormally." << endl;   }   delete [] inp_file;   delete [] op_file;   return (success); }//end main //---------------------------------------------------------------- //Function: computeFrequencyCount() //Parameters:  INP - the name of the input file //       OP - the name of the output file //Return type: bool, true if we are successful, false otherwise //Purpose: Computes the frequency count of an encrypted file by //reading a line of text, analyzing the line one character at a //time, indexing into the global count array and incrementing the //count of that character, and continuing until it reaches the //end of the line.  This process is continued until the eof is //reached.  Since all characters are uppercase, we index into the //count array on the ASCII value of the character minus 65.  Hence, //an A is at index 0 (65-65=0), and Z is at index 25 (90-65=25). //---------------------------------------------------------------- bool computeFrequencyCount(const char *&INP, const char *&OP) {  bool success = false;  char temp[SIZE];  ifstream input(INP, ios:: in);  ofstream output(OP, ios:: out);  //if we can't open the input file, we need to report the  //error to the user  if ((!input) || (!output)){     //do nothing  }  else {    success = true;    //read each character    char one_character;    //character's ASCII value    int value;    //perform frequency count on each line    while (input.getline(temp, SIZE, '\n')){     for (int ix=0; ix<strlen(temp); ix++){       one_character = *(temp+ix);       value = static_cast<int>(one_character);       //check for a valid character       if ((value >= SIXTYFIVE) || (value <= NINETY)){       //increment the number of characters         total_characters++;          //add the count of the character to the array          count[value - SIXTYFIVE] += 1;          }       }    }   //check for no characters in the input file   if (total_characters == 0){      success = false;   }   else {     //echo the output to the screen and save it to a file     displayOutput(output, OP);   } } return success; }//end computeFrequencyCount() //---------------------------------------------------------------- //Function: displayOutput() //Parameters:  OUTFILE - the file we are writing the results to //       FILE_NAME - the name of the output file //Return type: None //Purpose: Displays the results to the screen and saves them to a //file named FILE_NAME. //---------------------------------------------------------------- void displayOutput(ofstream OUTFILE, const char*& FILE_NAME) {   cout << "Frequency count is: " << endl << endl;   OUTFILE << "Frequency count is: " << endl <<   endl;   cout << "Character" << "\t" << "Count" << "\t" << "Frequency" << endl;   OUTFILE << "Character" << "\t" << "Count" << "\t" << "Frequency" << endl;   for (int ix=0; ix<TWENTYSIX; ix++){     cout << setw(4) << static_cast<char>(ix + SIXTYFIVE) << "\t\t"              << setw(3) << count[ix] << "\t"              << static_cast<float>(count[ix])/total_characters*ONE_HUNDRED              << "%" << endl;   OUTFILE << setw(4) << static_cast<char>(ix + SIXTYFIVE) << "\t\t"               << setw(3) << count[ix] << "\t"              << static_cast<float>(count[ix])/total_characters*ONE_HUNDRED              << "%" << endl;   }   cout << "\nTotal number of characters is: " << total_characters << endl;   cout << "Data saved to file " << FILE_NAME << endl;   OUTFILE << "\nTotal number of characters is: " << total_characters <<   endl;   return; }//end displayOutput() //---------------------------------------------------------------- //Function: getFileNames() //Parameters:  input - the input file //       output - the output file where the frequency data //            will be saved //Return type: None //Purpose: Queries the user for the input and output file names. //---------------------------------------------------------------- void getFileNames(char *&input, char *&output) {   char temp[SIZE];   int char_count = 0;   cout << "Input file (the file you want to perform" << endl        << "the frequency count on): ";   cin.getline(temp, SIZE, '\n');   char_count = strlen(temp);   assert(input = new char[char_count+1]);   strcpy(input, temp);   cout << "\nOutput file: ";   cin.getline(temp, SIZE, '\n');   char_count = strlen(temp);   assert(output = new char[char_count+1]);   strcpy(output, temp);   return; }//end getFileNames() //---------------------------------------------------------------- //Function: initialize() //Parameters:  None //Return type: None //Purpose: Initializes the character count to 0 for each character //in the global count array. //---------------------------------------------------------------- void initialize() {   for (int ix=0; ix<TWENTYSIX; ix++){     count[ix] = 0;   }   return; }//end initialize //---------------------------------------------------------------- //Function: welcome() //Parameters:  None //Return type: None //Purpose: Describes the count.cpp program. //---------------------------------------------------------------- void welcome(void) {  cout << "Program count.cpp performs a frequency count "        << "on an encrypted file." << endl        << "Results can be used to help decipher messages "        << "that used a mono-" << endl        << "alphabetic substitution scheme." << endl << endl;   return; }//end welcome() //end file count.cpp 


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