65.

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

Listing 6.8 lists the statements in the C++ program DRANDOM2.CPP. This program represents the C++ version of DRANDOM2.BAS and can be considered to represent the decipher version of RANDOM2.CPP. That is, a message enciphered using RANDOM2.CPP can be deciphered using DRANDOM2.CPP as long as you enter the same seed for each program. Figure 6.2 illustrates the execution of RANDOM2.EXE and DRANDOM2.EXE using the same seed number. As indicated in Figure 6.2, the message HELP was enciphered as QAJA by RANDOM2.EXE based upon the use of 9 for the seed. Then DRANDOM2.EXE was executed using the same seed value; however, the one-line message input to the program is QAJA, which results in the deciphered message HELP.


Figure 6.2  Using the C++ executable file DRANDOM2.EXE to decipher a one-line message enciphered using RANDOM2.EXE.

Listing 6.8 The DRANDOM2.CPP program listing.

 /* drandom2.cpp C++ code written by Jonathan Held on May 1, 1998, using Microsoft Visual C++ version 5.0 */ #include <iostream.h> #include <stdlib.h> #include <string.h> #include <assert.h> //function prototypes bool checkInput(char *&); char* decipher(char *&); void formatData(char []); void getMessage(char *&); unsigned int getSeed(void); void gotit(const char [], char *&, const int, int); const int BUFFER_SIZE = 256; //---------------------------------------------------------------- //Function: main() //Parameters: None //Return Type: int - 0 if program terminated normally //Purpose: driver that makes calls to various procedures defined //in random1.cpp //---------------------------------------------------------------- int main(){   unsigned int seed;   char *message, *result;   while (seed = getSeed()) {    srand(seed);    getMessage(message);    cout << "\nYou entered: " << message << endl;    result = decipher(message);    cout << "Deciphered message is " << result << endl << endl;    delete message; }  return 0; }//end main() //---------------------------------------------------------------- //Function: checkInput() //Parameters: input - the message 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 < 65) || (char_value > 90)){     error = true;     cerr << "\aYou entered an invalid message!" << endl << endl;     break;   }  } if (count == 0){    cerr << "\aYou entered an invalid message!" << endl << endl;    error = true;  }  return error; }//end checkInput() //---------------------------------------------------------------- //Function: decipher() //Parameters: inp - the user's input that will be deciphered //Return Type: char * - a pointer to the decrypted string //Purpose: returns a pointer to the decrypted text //---------------------------------------------------------------- char* decipher(char *&inp){ //yet another, and easier way to represent the plaintext //array const char plaintext[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; int index = 0; char *decrypted = new char[strlen(inp)+1]; assert(decrypted); for (int ix=0; ix < strlen(inp); ix++){   for (int jx=0; jx <26; jx++){    if (plaintext[jx] == inp[ix]){          gotit(plaintext, decrypted, jx, index++);          break;    }   }  } //don't forget to insert the null terminator decrypted[strlen(inp)] = '\0'; return decrypted; }//end encipher(); //---------------------------------------------------------------- //Function: formatData() //Parameters: data - the array we want to format //Return Type: None //Purpose: Get rid of all spaces in the array. //---------------------------------------------------------------- void formatData(char data[]){  for (int mx=0, nx=0; (*(data+nx) != '\0'); nx++){   if (*(data+nx) == ' '){      //do nothing - skip over the space in the data   }   else {    *(data+mx++) = *(data+nx);   } } //don't forget to add the null terminator *(data+mx) = '\0';  return; }//end formatData() //---------------------------------------------------------------- //Function: getMessage() //Parameters: input - user's one-line message //Return Type: None //Purpose: Gets a one-line message from the user. //---------------------------------------------------------------- void getMessage(char *&input){  char buffer[BUFFER_SIZE] = {'\0'};  bool error = false;  do{   cout << "Enter a one-line message (no spaces) in UPPERCASE: ";   cin.getline(buffer, BUFFER_SIZE, '\n');   assert(input = new char[strlen(buffer) + 1]);   strcpy(input, buffer);   formatData(input);   error = checkInput(input);   if (error)      delete input;  } while (error);  return; }//getMessage() //---------------------------------------------------------------- //Function: getSeed() //Parameters: none //Return Type: unsigned int - the seed the client entered //Purpose: gets the seed for the random number generator.  If the //user enters a negative number, it causes the seed to underflow to //a very high non-negative number.  If the user enters an //alphanumeric sequence, e.g., "A123", then seed_Number is set to //zero. //---------------------------------------------------------------- unsigned getSeed(){  unsigned int seed_Number;  cout << "Enter seed, 0 (or non-digit) to terminate: ";  cin >> seed_Number;  cin.ignore();  return seed_Number; }//end getSeed() //---------------------------------------------------------------- //Function: gotit() //Parameters: p_text - the plaintext array, i.e., characters "A-Z" //      enc_msg - a pointer to the message that will be encoded //      loc - the location of each encoded character in the //      plaintext array //Return Type: None //Purpose: encodes the user's input using random numbers //---------------------------------------------------------------- void gotit(const char p_text[], char *&dec_msg, int loc, int index){  int random_num, z;  do {    random_num = rand() % 100;  } while (random_num > 25);  if (loc < random_num)          loc+= 26;  z = loc - random_num;  dec_msg[index] = p_text[z];  return; }//end gotit() //end file drandom2.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