20.

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

In developing a C++ version of CIPHER2.BAS, a considerable visual improvement occurred by allowing spaces between words. Listing 2.6 contains a listing of the program CIPHER2.CPP. Since this program is well defined through the use of a significant number of comments concerning its coding, our comments on its coding will be minimized. As noted from the function prototypes section, the program uses five key functions. The getShiftKey function checks for and accepts an uppercase letter as the shift key to be used by the program. The createCipher function creates the ciphertext alphabet based upon the shift key previously entered. The third function, getMessage, retrieves the user’s message which can be up to 256 uppercase characters in length. The fourth function, formCipheredMessage, enciphers the previously entered message while the fifth function, printResults, displays the corresponding enciphered message.

Listing 2.6 The CIPHER2.CPP program listing.

 /* Program Cipher2.cpp C++ Code written by Jonathan Held, January 21, 1998, using Microsoft Visual C++, version 5.0 211 total lines of code. */ //standard include files #include <iostream.h> #include <string.h> //function prototypes void getShiftKey(char &); void createCipher(const char [], char[], const char); void getMessage(char []); void formCipheredMessage(const char[], const char [], char []); void printResults(const char[], const char[], const char[], const char []); //main program int main(){   //initialize plaintext   char plaintext[26] = {'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'};   //other variables we will use   char ciphertext[26], message_to_cipher[256],   enciphered_message[256], key;   //function calls   do {      //get the uppercase key      getShiftKey(key);      //create the cipher key      createCipher(plaintext, ciphertext, key);      //get the users message      getMessage(message_to_cipher);      //form the ciphered message      formCipheredMessage(ciphertext, message_to_cipher,      enciphered_message);      //print off the results      printResults(plaintext, ciphertext, message_to_cipher,                     enciphered_message);   } while (true);   return (0); }//end main() //----------------------------------------------------- //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 < 65) || (key_value > 90)){         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<26; 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<26; jx++){      ctext[jx] = PTEXT[(jx + location) % 26];   }   return; }//end createCipher(); //----------------------------------------------------- //Function: getMessage() //Parameters:  msg_to_cipher[] - the message entered by the user //Return Type: None //Purpose: Get the user's message and find out how long it is using //the standard string function strlen including in the string.h //library file. //----------------------------------------------------- void getMessage(char msg_to_cipher[]){   //get the newline character off of the input stream   cin.get();   cout << "Enter the message in UPPERCASE characters: " << endl;   //get the entire line, up to 256 characters   cin.getline(msg_to_cipher, 256, '\n');   return; }//end getMessage() //----------------------------------------------------- //Function: formCipheredMessage() //Parameters: CTEXT - the cipher alphabet we will use for substitution //      MESSAGETOCIPHER - the user's message //      enc_message - the enciphered message to be determined //Return Type: None //Purpose: Encipher the user's one-line message. //----------------------------------------------------- void formCipheredMessage(const char CTEXT[], const char MESSAGETOCIPHER[],             char enc_message[]){   int length = strlen(MESSAGETOCIPHER)+1;   for (int ix=0; ix<length; ix++){      //get character from MESSAGETOCIPHER      char encode = MESSAGETOCIPHER[ix];      int encode_value = static_cast<int>(encode);      //handle the case where an input character is not a letter      //A - Z      if ((encode_value < 65) || (encode_value > 90)){             enc_message[ix] = MESSAGETOCIPHER[ix];      }      else {         //valid character - the easy way to calculate the ciphered         //character is based on the plain text's ascii character value;         //since it has to be a capital letter, it must be in the range         //from 65 to 90, with A represented by 65, Z by 90. By simply         //subtracting 65 from the encode_value (the integer representation         //of the plaintext character), we now know what cipher character         //to use.         enc_message[ix] = CTEXT[encode_value-65];      }   }     return; }//end formCipheredMessage() //----------------------------------------------------- //Function: printResults() //Parameters: PTEXT - the plaintext alphabet //       CTEXT - the cipher alphabet we used for subsitution //       MESSAGETOCIPHER - the user's message //       ENCIPHEREDMESSAGE - the corresponding enciphered message //Return Type: None //Purpose: Display the values of all passed-in parameters. //----------------------------------------------------- void printResults(const char PTEXT[], const char CTEXT[],                       const char MESSAGETOCIPHER[], const char ENCIPHEREDMESSAGE[]){   cout << "\nPLAINTEXT:  ";   for (int ix=0; ix<26; ix++){      cout << PTEXT[ix] << " ";   }   cout << endl;   cout << "CIPHERTEXT: ";   for (int jx=0; jx<26; jx++){      cout << CTEXT[jx] << " ";   }   cout << "\n\nORIGINAL MESSAGE: " << MESSAGETOCIPHER << endl;   cout << "\nCIPHERED MESSAGE: " << ENCIPHEREDMESSAGE << endl << endl;   return; }//end printResults() //end file cipher5.cpp 

Key differences between the BASIC and C++ versions of the CIPHER2 program are in the validation of uppercase letters and termination of the program. The C++ version uses ASCII values from 65 through 90 as a check for uppercase A through Z and recognizes the CTRL-C key combination as a mechanism to quit the program.


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