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 POSITION.BAS Program

The top of Listing 6.13 gives the statements in the demonstration program POSITION.BAS which accepts a “secret” code up to six characters in length and uses the ASCII values of those characters in the manner previously discussed as a mechanism for positioning into a place in the random number generator. In this program, the subroutine SETUP actually performs the previously described manipulation of the ASCII value of each code character. The lower portion of Listing 6.13 illustrates the execution of the program using five different “secret” codes. The program displays the numeric (ASCII) value of each code character and the first five random numbers following the execution of the positioning subroutine. Any ASCII character can be used for each code character; however, control characters that are non-printable are obviously not printed when used as code characters.

Listing 6.13 The POSITION.BAS program listing and examples of its repeated execution.

REM Program POSITION.BAS demonstrates the positioning to a place in the
REM random number generator based upon a secret code up to 6 characters
AGN:
   INPUT "Enter your secret code (6 characters maximum): "; CODE$
   IF LEN(CODE$) > 6 THEN GOTO AGN
GOSUB SETUP
   PRINT "Numeric value of code characters: ";
   FOR I = 1 TO 6
   PRINT CODE(I); " ";
   NEXT I
   PRINT: PRINT "1st five random numbers: ";
   FOR I = 1 TO 5: PRINT RND;: NEXT I: PRINT: PRINT
   STOP
SETUP:
   REM routine to position random number generator
   FOR I = 1 TO LEN(CODE$)
   CODE(I) = ASC(MID$(CODE$, I, 1))
   NEXT I
   SEED = CODE(1) * CODE2
   MAX = CODE(3) * CODE(4)
   RANDOMIZE SEED
   FOR I = 1 TO MAX STEP CODE(5)        'position into sequence
   DUMMY = RND
   NEXT I
   FOR I = 1 TO CODE(5) * CODE(6) STEP CODE(4) 'further offset into
    sequence
   DUMMY = RND
   NEXT I
RETURN


Enter your secret code (6 characters maximum): ? HELPME
Numeric value of code characters:  72   69   76   80   77   69
1st five random numbers: .8671363 .3378422 .4237103 .6096486  .9087062

Enter your secret code (6 characters maximum): ? 9NINES
Numeric value of code characters:  57   78   73   78   69   83
1st five random numbers:  .4766153 .8707944 .8031864 .3487226 .6214768

Enter your secret code (6 characters maximum): ? 382438
Numeric value of code characters:  51   56   50   52   51   56
1st five random numbers: .944414 .949271 .5596389 .7912182 9.593111E-02

Enter your secret code (6 characters maximum): ? 3CARDS
Numeric value of code characters:  51   67   65   82   68   83
1st five random numbers: .4237103 .6096486 .9087062 .8784156 .0561406

Enter your secret code (6 characters maximum): ? helpme
Numeric value of code characters:  104   101   108   112   109   101
1st five random numbers: .5051253 .7871563 .9859242 .1925381 .3514438

The POSITION.CPP Program

Continuing our dual language approach to illustrate major encryption coding techniques, the top portion of Listing 6.14 contains the program listing for the C++ version of POSITION.BAS. This program, which has the filename POSITION.CPP, is contained on the CD-ROM in the C directory. Similarly, the executable version of this program is stored under the name POSITION.EXE in the same directory. The lower portion of Listing 6.14 illustrates two examples of the execution of POSITION.CPP. If you compare the execution of POSITION.CPP to the execution of POSITION.BAS, you will note that the use of the same secret code produces different results. As previously explained, the random number generators used in BASIC and C++ differ and result in the difference between the results display when executing POSITION.BAS and POSITION.CPP.

Listing 6.14 The POSITION.CPP program listing and examples of the execution of the program.

/*
position.cpp
C++ code written by Jonathan Held, May 8, 1998, using Microsoft's
Visual C++ version 5.0.
*/

//standard includes
#include <iostream.h>
#include <string.h>
#include <stdlib.h>
#include <iomanip.h>

//function prototypes
void firstFiveRndNumbers(char *);

void getCode(char *&);
void numericValues(char *);
//----------------------------------------------------------------
//Function: main()
//Parameters: None
//Return Type: int - 0 means program terminated normally, any other
//value means abnormal termination
//Purpose: Runs the main part of the program.
//----------------------------------------------------------------
int main(){

  cout << "\aHit CTRL-C to terminate this progam." << endl << endl;
  while (true){
   char * the_Code;
   getCode(the_Code);
   numericValues(the_Code);
   firstFiveRndNumbers(the_Code);
   cout << endl;
 }
 return 0;
}//end main()

//----------------------------------------------------------------
//Function: firstFiveRndNumbers()
//Parameters: the_Code - a pointer to the keyword entered by the
//client
//Return Type: None
//Purpose: Generates the first five random numbers in the sequence.
//Random number generation is done based on a six-character code,
//entered by the client.
//----------------------------------------------------------------
void firstFiveRndNumbers(char *the_Code){

 int seed = (int)*the_Code * (int)(*(the_Code+1));
 int max = (int)(*(the_Code+2)) * (int)(*(the_Code+3));
 float dummy;
 int increment;
 srand(seed);
 cout << "1st five random numbers: ";
 increment = (int) (*(the_Code + 4));

for (int ix=1; ix<max; ix+=increment)
   dummy = ((float) rand() )/RAND_MAX;
        max = (int)(*(the_Code + 4)) * (int) (*(the_Code+5));

   for (int jx=1; jx<max; jx+=increment)
      dummy = ((float) rand() )/RAND_MAX;

   for (int kx=1; kx<=5; kx++)
      cout << ((float) rand() )/RAND_MAX << " ";

   cout << endl;
   return;
}//end firstFiveRndNumbers()
//----------------------------------------------------------------
//Function: getCode()
//Parameters: the_Code - the code that will be entered by the client
//Return Type: None
//Purpose: Prompts the client for a keyword of at least six characters.
//Checks the length of the keyword entered to make sure it has the
//required number of characters.  If it doesn't, it prints an error
//message and prompts for a keyword again.
//----------------------------------------------------------------
void getCode(char *&the_Code){

 int length;
 bool go_on = true;
 char buffer[1000];
 while(go_on){
  cout << "Enter your secret code (6 characters minimum): ";
  cin >> buffer;
  length = strlen(buffer);
  if (length < 6) {
     cerr << "\aCode must be 6 characters or more!"
          << endl << endl;
 }
 else {
      the_Code = new char[length];
      strcpy(the_Code, buffer);
      go_on = false;
  }
 }
 return;
}//end getCode()
//----------------------------------------------------------------
//Function: numericValues()
//Parameters: the_Code - the code entered by the client
//Return Type: None
//Purpose: Displays the ASCII values of the characters in the
//keyword.
//----------------------------------------------------------------
void numericValues(char *the_Code){

  char *alias = the_Code;
  int count = 0;
  char *spacing = "                               ";
  cout <<  "Numeric value of code characters: ";
  while (*alias){
   if ((count !=0) && (!(count%10)))
      cout << endl << spacing;
      cout << setw(4) << (int) (*alias++);
        count++;
   }

  cout << endl;
  return;
}//end numericValues()
//end file position.cpp

A:\c>position
Hit CTRL-C to terminate this progam.

Enter your secret code (6 characters minimum): HELPME
Numeric value of code characters:   72  69  76  80  77  69
1st five random numbers: 0.856807 0.441908 0.621845 0.638844 0.32

Enter your secret code (6 characters minimum): 9NINES
Numeric value of code characters:   57  78  73  78  69  83
1st five random numbers: 0.516404 0.737236 0.561724 0.396344 0.16


Previous Table of Contents Next