42.

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

Prior to developing the program CIPHER6 that will encipher messages based upon any one of the three alphabetic formation techniques previously presented in this chapter, let’s turn our attention to C++ coding of the subroutine TRANSPORT and the program CIPHERTR. The C++ versions of both are included on the CD-ROM in the C directory as both source and executable files.

Listing 4.4 lists the statements in the C++ version of TRANSPORT, which is stored on the CD-ROM under the filename TRANSPORT.CPP. The executable version of TRANSPORT.CPP is stored on the CD-ROM in the C directory under the filename TRANSPORT.EXE.

Listing 4.4 The TRANSPORT.CPP program listing.

 /* transport.cpp C++ code written by Jonathan Held using Microsoft Visual C++, version 5.0, on March 21, 1998. */ //standard include files #include<iostream.h> #include<assert.h> #include<string.h> #include<ctype.h> //function prototypes bool checkInput(char * &); void createCipherStream(char *, char[]); void createMatrix(char ** &, const char [], const int, const int); int findLowestValue(int *&, const int); int getKeyword(char *&); void printMatrix(char ** &, const int, const int); void reviseCipherStream(char **&, char [], const int, const int); void deleteMatrix(char **&, const int, const int); //constants we will use const int TWENTYFIVE = 25, TWENTYSIX = 26,       TWENTYSEVEN = 27, SIXTYFIVE = 65, NINETY = 90,       NINETYTWO = 92, 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', '\0'};   char cipherStream[TWENTYSEVEN] = {'\0'};   char ** cMatrix;   char *keyword;   int rows, columns;   //get the keyword we are going to use, determine number of rows   //and columns of our matrix   columns = getKeyword(keyword);   rows = TWENTYSIX/columns;   //integer division requires that we check and see if there is a   //remainder; if so, we need to add one extra row to our matrix   if (TWENTYSIX%columns){      rows++;   }   //create the initial ciphertext stream   createCipherStream(keyword, cipherStream);   //insert the stream into our matrix   createMatrix(cMatrix, cipherStream, rows, columns);   reviseCipherStream(cMatrix, cipherStream, rows, columns);   //echo to the user what we have   cout << "\nPlaintext-based alphabet is:" << plaintext << endl      << "Keyword-based alphabet is: " << cipherStream << endl << endl;   //delete the dynamically allocated matrix   deleteMatrix(cMatrix, rows, columns);   delete keyword;   return 0; }//end main() //---------------------------------------------------------------- //Function: checkInput() //Parameters: input - the keyword the user entered //Return Type: bool - true if the input string contains an error, //       false otherwise //Purpose: Checks the user's keyword for invalid characters. //---------------------------------------------------------------- bool checkInput(char * &input) {   bool error = false;   int count = strlen(input);   for (int ix=0; ix<count; ix++){    int char_value = static_cast<int>(*(input+ix));    //determine if the user did not enter an uppercase character    if ((char_value < SIXTYFIVE) || (char_value > NINETY)){       error = true;       cerr << "You entered an invalid keyword!" << endl << endl;       break;    }   }   return error; }//end checkInput() //---------------------------------------------------------------- //Function: createCipherStream() //Parameters: input - the keyword the user entered //      cipher - the keyword alphabet that will be constructed //Return Type: None //Purpose: Creates a preliminary cipher stream that will be used to //form the cipher matrix. //---------------------------------------------------------------- void createCipherStream(char *input, char stream[]) {  bool used[TWENTYSIX];  int index = 0,    count = strlen(input);  //no characters are initially used  for (int ix=0; ix<TWENTYSIX; ix++){    used[ix] = false;  }  //keep track of each character used, start forming the keyword  //alphabet  for (int jx=0; jx<count; jx++){    //get each character of the input string (integer value)    int char_value = static_cast<int>(*(input+jx));    if (used[char_value-SIXTYFIVE]){       //do nothing - the character was already used    }    else {      //mark as used and add to the keyword alphabet      used[char_value-SIXTYFIVE] = true;      *(stream+index++) = static_cast<char>(char_value);    }  }  //go through the list of characters used - those which weren't  //used should be added to the keyword alphabet  for (int kx=0; kx<TWENTYSIX; kx++){    if (!(used[kx])){       *(stream+index++) = static_cast<char>(SIXTYFIVE+kx);    }  }  return; }//end createCipherStream() //---------------------------------------------------------------- //Function: createMatrix() //Parameters: matrix - the matrix we are going to create //      CSTREAM - the initial cipher stream based on the //                      user's keyword //      ROWS - the number of rows in the matrix //      COLS - the number of columns in the matrix //Return Type: None //Purpose: Creates a numeric key transposed matrix, identical to //figure 4.4. //---------------------------------------------------------------- void createMatrix(char ** &matrix, const char CSTREAM[], const int ROWS,                     const int COLS) {   int count = 0;   //dynamically allocate memory for the RxC matrix   //we use assert to ensure that memory was allocated;   //if not, then the program will terminate abnormally   assert(matrix = new char*[ROWS]);   for (int ix=0; ix<ROWS; ix++){     *(matrix + ix) = new char[COLS];   }   //fill in the matrix   for (int jx=0; jx<ROWS; jx++){     for (int kx=0; kx<COLS; kx++){     //we only want to enter a character into the matrix     //twenty-six times - most of the time we allocate     //a matrix larger than what we will need to use; when     //we have more than 26 characters, we simply insert a     //null terminator into the matrix       if (count < TWENTYSIX)          *(*(matrix+jx)+kx) = CSTREAM[count++];      else          *(*(matrix+jx)+kx) = '\0';      }   }   //print the resulting matrix   printMatrix(matrix, ROWS, COLS);   return; }//end createMatrix() //---------------------------------------------------------------- //Function: deleteMatrix() //Parameters: matrix - the matrix we are going to destroy //      R - the number of rows in the matrix //      C - the number of columns in the matrix //Return Type: None //Purpose: Destroys the dynamically allocated matrix! //---------------------------------------------------------------- void deleteMatrix(char **&matrix, const int R, const int C) {  for(int ix=0; ix<R; ix++)    delete [] *(matrix+ix);  delete [] matrix;  return; }//end deleteMatrix() //---------------------------------------------------------------- //Function: findLowestValue() //Parameters: values - the ASCII values of all characters in the //            top row of the matrix //      COLS - the number of columns in the matrix //Return Type: int - the column we want. //Purpose: Determines what column we are going to extract from //the matrix and put into the cipher stream. //---------------------------------------------------------------- int findLowestValue(int *&values, const int COLS) {   int loc=0, lowest=999;   for (int ix = 0; ix < COLS; ix++){     if (*(values+ix) != 999){        if (*(values+ix) < lowest){           lowest = *(values+ix);           loc = ix;        }     }  }  *(values+loc) = 999;  return loc; }//end findLowestValue() //---------------------------------------------------------------- //Function: getKeyword() //Parameters: text - the keyword that the user enters //Return Type: int - the length of the keyword //Purpose: Prompts the user for a keyword and continues until //a valid keyword has been entered.  Returns the length of the //keyword. //---------------------------------------------------------------- int getKeyword(char * &text) {   bool error = false;   char buffer[SIZE];   do {      cout << "Enter keyword or keyword phrase in UPPERCASE" << endl         << "do not use spaces or non-alphabetic characters): ";      cin.getline(buffer, SIZE, '\n');      assert(text = new char[strlen(buffer) + 1]);      strcpy(text, buffer);      error = checkInput(text);     //delete text if there was an error     if (error){        delete [] text;     }   } while (error);   cout << endl;   return strlen(buffer); }//end getKeyword() //---------------------------------------------------------------- //Function: printMatrix() //Parameters: the_matrix - the matrix we want to display //      ROWS - the number of rows in the matrix //      COLS - the number of columns in the matrix //Return Type: None //Purpose: Displays the matrix. //---------------------------------------------------------------- void printMatrix(char ** &the_matrix, const int ROWS, const int COLUMNS) {   cout << "The matrix is:" << endl << endl;   for (int ix=0; ix<ROWS; ix++){      for (int kx=0; kx<COLUMNS; kx++){        cout << *(*(the_matrix+ix)+kx) << " ";      }      cout << endl;   }   return; }//end printMatrix() //---------------------------------------------------------------- //Function: reviseCipherStream() //Parameters: MATRIX - the numeric keyed transposed matrix //      cipher - the cipher stream that was originally created //                     when the user entered a keyword //      R - the number of rows in the matrix //      C - the number of columns in the matrix //Return Type: None //Purpose: Overwrites the cipher stream initially created with the //one we get by taking columns out of the matrix. //---------------------------------------------------------------- void reviseCipherStream(char ** &MATRIX, char cipher[], const int R,                        const int C) {   bool done = false;   int counter = 0;   //place ASCII values of first row's characters into a table;   //we will use these values to determine the order in which   //we pull columns out of the matrix   int *top_row_ASCII_values = new int[C];   assert(top_row_ASCII_values);   //no cast to integer type required since this is already taken   //care of for us   for (int ix=0; ix<C; ix++){       *(top_row_ASCII_values + ix) = *((*MATRIX)+ix);   }   for (int jx=0; jx<C; jx++){     //find out what column we want     int col = findLowestValue(top_row_ASCII_values, C);     //put contents of the column into the cipher stream, but     //only do this when we have a character!     for (int kx=0; kx<R; kx++){       if ((*(*(MATRIX+kx)+col)) != '\0'){          cipher[counter++] = (*(*(MATRIX+kx)+col));       }     }   }   //destroy dynamically allocated memory   delete [] top_row_ASCII_values;   return; }//end reviseCipherStream() //end file transport.cpp 

In examining the TRANSPORT.CPP listing shown in Listing 4.4, note that the function createMatrix is used to create a numeric key transposed matrix which is identical to Figure 4.4. This subroutine dynamically allocates memory based upon the rows and columns. Note that the function deleteMatrix is included in the program to remove any previously allocated matrix. This is good programming practice.

Two additional functions that warrant a bit of attention are findLowestValue and reviseCipherStream. The function findLowestValue is used to determine the column that is going to be used for extraction and placed into the cipher stream, while the function reviseCipherStream is used to overwrite the cipher stream initially created with the one obtained by taking columns out of the matrix.


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