18.

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


Listing 2.4 The CIPHER1.CPP program listing.

 /* PROGRAM :CIPHER1.CPP   All C++ code written by Jonathan Held, January 20, 1998,   using Microsoft's Visual C++ version 5.1. */ //standard include files #include <iostream.h> //function prototypes void check(char &); void findKey(char, const char [], int &); void formCipher(const char[], char[], int); void printResults(const char[], const char[]); //main program int main(){     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'};     char CIPHERTEXT[26], key;     int key_location;     cout << "Enter UPPERCASE Alphabetic Shift Key: ";     cin >> key;     check(key);     findKey(key, PLAINTEXT, key_location);     formCipher(PLAINTEXT, CIPHERTEXT, key_location);     printResults(PLAINTEXT, CIPHERTEXT);     return (0); }//end main() //----------------------------------------------------- //Function: check() //Parameters: key - alphabetic key entered by user //Return type: none //Purpose: Ensure key is a valid alphabetic character.  If it is //a lowercase letter, this procedure will automatically convert it //to an uppercase one.  This procedure also handles all error //checking! //----------------------------------------------------- void check(char &key){   int key_value;   bool error = false;   do {        //only change came in this line, where I removed the call        //to the function toupper()        key_value = static_cast<int>(key);        if ((key_value < 65) || (key_value >90)){             cerr << "\n\aPlease enter an UPPERCASE Alphabetic Shift Key                      (A-Z): ";             cin >> key;        }        else {        error = true;        }   } while (!(error));   key = static_cast<char>(key_value);   cout << "\nKey entered was: " << key << endl;   return; }//end check() //----------------------------------------------------- //Function: findkey() //Parameters: key - alphabetic key entered by user //      PLAINTEXT - the plaintext alphabet //      key_location - the location of the key in the //      PLAINTEXT alphabet //Return type: none //Purpose: Find the key in the PLAINTEXT alphabet.  This position //will be used when we form the cipher. //----------------------------------------------------- void findKey(char key, const char PLAINTEXT[], int &key_location){    for (int ix=0; ix<26; ix++){       if (key == PLAINTEXT[ix]){           key_location = ix + 1;           break;       }     }     return; }//end findKey() //----------------------------------------------------- //Function: formcipher() //Parameters: PLAINTEXT - the plaintext alphabet //      CIPHERTEXT - the cipher alphabet we will create //      loc - the key location in the plaintext //Return type: none //Purpose: Create the ciphertext using modulo arithmetic. //----------------------------------------------------- void formCipher(const char PLAINTEXT[], char CIPHERTEXT[], int loc){    for (int ix=0; ix<26; ix++){        CIPHERTEXT[ix] = PLAINTEXT[(ix + loc) % 26];    }    return; }//end formCipher() //----------------------------------------------------- //Function: printresults() //Parameters: PLAINTEXT - the plaintext alphabet //      CIPHERTEXT - the ciphertext //Return type: none //Purpose: Print the plaintext and corresponding ciphertext based //on the key the user selected. //----------------------------------------------------- void printResults(const char PLAINTEXT[], const char CIPHERTEXT[]){    cout << "PLAINTEXT:  ";    for (int ix=0; ix<26; ix++){          cout << PLAINTEXT[ix] << " ";    }    cout << "\nCIPHERTEXT: ";    for (int jx=0; jx<26; jx++){          cout << CIPHERTEXT[jx] << " ";    }    cout << endl;    return; }//end printResults() 

In examining the C++ program listing for CIPHER1.CPP, note that four functions were used—check, findKey, formCipher, and printResults. Each function is documented within the program listing to include a description of their parameter(s), return type, and purpose. The execution of CIPHER1.CPP will produce the same results as the execution of CIPHER1.BAS, which was previously illustrated in Listing 2.3. The executable version of CIPHER1.CPP is named CIPHER1.EXE and is located under the C directory on the CD-ROM included with this book.


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