22.

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

The C++ version of CIPHER3.BAS follows our naming convention and is stored in the C directory under the filename CIPHER3.CPP. This program is similar to the BASIC language version in that it first asks for the entry of an uppercase alphabetic shift key character. Next, it displays the plaintext and ciphertext alphabet resulting from the shift and prompts you to enter your “original message.” Using the shift key the program generates an enciphered message in which the resulting enciphered text is displayed in military fashion in groups of five characters.

Listing 2.9 contains the CIPHER3.CPP program listing. Note that the function printResults places the enciphered text into groups of five characters and adds a space when the variable “counter” does not equal 0 and the value of counter mod 5 equals 0.

Listing 2.9 The CIPHER3.CPP program listing.

 /* Program Cipher3.cpp C++ Code written by Jonathan Held, January 21, 1998, using Microsoft Visual C++, version 5.0. 248 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 []); void printCipher(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: " << endl << MESSAGETOCIPHER << endl;   cout << "\nEnciphered Message: " << endl;   printCipher(ENCIPHEREDMESSAGE);   return; }//end printResults() //----------------------------------------------------- //Function: printCipher() //Parameters:  MSG - the cipher text we are displaying //Return Type: None //Purpose: Group the cipher in 5-block characters //----------------------------------------------------- void printCipher(const char MSG[]){   int counter = 0;   int length = strlen(MSG)+1;   for (int kx=0;kx<length;kx++){      if (*(MSG+kx) != ' '){         if ((counter != 0) && (counter%5 == 0)) {            cout << " ";         }         cout << *(MSG+kx);         counter++;      }   }   cout << endl << endl;   return; }//end printCipher() //end file cipher3.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