32.

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 3.2 The WORD.CPP program listing.

 /* word.cpp C++ Code written by Jonathan Held, February 27, 1998, using Microsoft Visual C++, version 5.0.  Please read file for detailed description of how the program works. */ #include<iostream.h> #include<iomanip.h> #include<string.h> //function prototypes void getKeyword(char * &); bool checkInput(char * &); void createAlphabet(char *, char []); //constants #define TWENTYSIX  26 #define TWENTYSEVEN 27 #define SIXTYFIVE  65 #define NINETY    90 #define SIZE    256 //------------------------------------------------ //Function: main() //Parameters: None //Return Type: int - 0 if program terminated normally //Purpose: Runs the main part of the program. //------------------------------------------------ int main(){   char *keyword;   char ciphertext[TWENTYSEVEN]= {'\0'};   cout << "*** AUTOMATING KEYWORD CONSTRUCTION ***" << endl      << "***   Hit CTRL-C to quit program   ***" << endl << endl;   do {    getKeyword(keyword);    cout << "\nYou entered\t\t" << keyword << endl;    createAlphabet(keyword, ciphertext);    cout << "Keyword alphabet is: " << setw(29) << ciphertext                 << endl << endl;    delete [] keyword;   }while(true);   return (0); }//end main() //------------------------------------------------ //Function: getKeyword() //Parameters: text - the keyword that the user enters //Return Type: None //Purpose: Prompts the user for a keyword and continues until //a valid keyword has been entered. //------------------------------------------------ void getKeyword(char * &text) {   bool error = false;   do {     char buffer[SIZE];     cout << "Enter keyword in CAPS (do not use" << endl      << "spaces or non-alphabetic characters): ";     cin.getline(buffer, SIZE, '\n');     text = new char[strlen(buffer) + 1];     strcpy(text, buffer);     error = checkInput(text);   } while (error);   return; }//end getKeyword() //------------------------------------------------ //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: createAlphabet() //Parameters: input - the keyword the user entered //      cipher - the keyword alphabet that will be constructed //Return Type: None //Purpose: Creates the keyword alphabet. //------------------------------------------------ void createAlphabet(char *input, char cipher[]) {   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;       *(cipher+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])){        *(cipher+index++) = static_cast<char>(SIXTYFIVE+kx);     }   }   return; }//end createAlphabet() //end file word.cpp 

In examining the automated creation of three keyword-based alphabets illustrated in Figure 3.1, you will note that the trailing portion of the resulting alphabets is very similar. In fact, the characters U and Z are positioned at the same locations in all three alphabets. If you simply positioned a keyword-based alphabet directly under a plaintext alphabet, there is a high degree of probability that one or more plaintext and ciphertext characters would equate to one another. This situation would obviously provide a valuable clue to the decipherment of an intercepted message. To eliminate or reduce the number of coincidental characters between a plaintext alphabet and a keyword-based ciphertext alphabet, you need to apply an alphabetic shift key to one or both alphabets.

Incorporating an Alphabetic Shift Key

In Chapter 2 you developed a program labeled CIPHER4.BAS that creates enciphered text based upon the use of an alphabetic shift key. In this chapter the discussion of monoalphabetic substitution is continued by examining the development of a keyword-based mixed alphabet as well as the statements in a subroutine contained in the program WORD.BAS to automate the keyword-based mixed alphabet creation process. Combining the use of a keyword-based mixed alphabet with an alphabetic shift key should result in a higher level of enciphered data protection than the separate use of either method. Thus, this section focuses on combining both techniques for enciphering and deciphering messages.

By placing the KEYWORD subroutine contained in WORD.BAS or the createAlphabet function from WORD.CPP into the CIPHER4.BAS or CIPHER4.CPP program, you gain the ability to encipher a message based upon a keyword or keyword phrase and an alphabetic shift. I’ll call these new programs CIPHER5.BAS and CIPHER5.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