Previous | Table of Contents | Next |
The POLY1.BAS program uses the previously developed INITIALIZE subroutine to initialize the letters of the alphabet into the string array PLAINTEXT$. The subroutine TABLE is used to fill the two-dimensional string array labeled TABLEAU$ with a sequence of 26 alphabets. Through the use of a nested pair of FOR-NEXT loops and a mod 26 operator, each row in the string array labeled TABLEAU$ is assigned an alphabet in which row n has a displacement of one character position with respect to row n-1. To illustrate the operation of the subroutine TABLE, note that when J is 0, I will vary from 0 to 25. Thus, the use of TABLEAU$(J,I) results in row 0 being assigned the values of PLAINTEXT$((I+0) mod 26), the non-displaced alphabet. Once the inner I loop is completed, J is incremented by 1 to a value of 1 and the I loop is again varied from 0 to 25. This results in TABLEAU$(J,I) having Row 1 assigned the alphabet displaced by one position, since PLAINTEXT$((I+J) mod 26) results in a value of 1 for J shifting the assignment of plaintext letters to TABLEAU$ by one position.
The subroutine PRINTIT was created to print the tableau as well as the row and column labels that represent vertically and horizontally positioned alphabets. This subroutine was developed simply to display the tableau, and its row and column positioning elements were used for the extraction of characters from the tableau during the enciphering process as we will shortly observe.
Listing 5.1 The POLY1.BAS program listing.
REM PROGRAM POLY1.BAS DIM PLAINTEXT$(26), TABLEAU$(26, 26) CLS GOSUB INITIALIZE GOSUB TABLE GOSUB PRINTIT STOP INITIALIZE: RESTORE REM Initialize plaintext values FOR I = 0 TO 25 READ PLAINTEXT$(I) NEXT I DATA "A","B","C","D","E","F","G","H","I","J","K","L","M","N" DATA "O","P","Q","R","S","T","U","V","W","X","Y","Z" RETURN TABLE: FOR J = 0 TO 25 FOR I = 0 TO 25 TABLEAU$(J, I) = PLAINTEXT$((I + J) MOD 26) NEXT I NEXT J RETURN PRINTIT: PRINT " "; FOR I = 0 TO 25: PRINT PLAINTEXT$(I);: NEXT I: PRINT: PRINT FOR J = 0 TO 25 PRINT PLAINTEXT$(J); " "; FOR I = 0 TO 25 PRINT TABLEAU$(J, I); NEXT I PRINT NEXT J RETURN
To add a bit of diversity, the C++ program version of POLY1.BAS was written to display one line at a time, requiring you to hit Enter at the end of each line to proceed to the next line. Listing 5.2 contains the listing of the program POLY1.CPP. Both that program and the executable version of the program named POLY1.EXE are stored in the C directory on the companion CD-ROM.
Listing 5.2 The POLY1.CPP program listing.
/*poly1.cpp C++ code written by Jonathan Held, March 26, 1998, using Microsoft's Visual C++ version 5.0. */ //standard include files #include <iostream.h> //constant we will use const int TWENTYSIX = 26, TWENTYSEVEN = 27, SIZE = 256; //function prototypes void initialize(char [][TWENTYSEVEN], const char []); void printHeader(const char[]); void printTable(const char [][TWENTYSEVEN]); //---------------------------------------------------------------- //Function: main() //Parameters: None //Return type: None //Purpose: Demonstrates a simple Vigenere tableau. //---------------------------------------------------------------- int main() { char plaintext[TWENTYSEVEN] = {'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','\0'}; char table[TWENTYSIX][TWENTYSEVEN]; initialize(table, plaintext); printHeader(plaintext); printTable(table); return 0; }//end main() //---------------------------------------------------------------- //Function: initialize() //Parameters: tbl - the two-dimensional Vigenere tableau // PTEXT - the plaintext alphabet //Return type: None //Purpose: Creates the Vigenere tableau by performing a shift on //the plaintext alphabet for each Vigenere row entry. //---------------------------------------------------------------- void initialize(char tbl[][TWENTYSEVEN], const char PTEXT[]) { for (int ix=0; ix<TWENTYSIX; ix++){ for (int jx=0; jx<TWENTYSIX; jx++){ tbl[ix][jx] = PTEXT[(ix+jx)%TWENTYSIX]; } tbl[ix][TWENTYSIX] = '\0'; } return; }//end initialize() //---------------------------------------------------------------- //Function: printHeader() //Parameters: PTEXT - the plaintext alphabet (also serves as the // Vigenere column header //Return type: None //Purpose: Prints the Vigenere column header and informs the user //to hit enter after each line of the table is printed. //---------------------------------------------------------------- void printHeader(const char PTEXT[]) { cout << "Hit enter at the end of each line" << endl << "to proceed to the next. " << endl << endl; cout << "\t"; for (int ix=0; ix<TWENTYSIX; ix++) cout << PTEXT[ix]; cout << endl << endl; return; }//end printHeader() //---------------------------------------------------------------- //Function: printTable() //Parameters: TABLE - the two-dimensional Vigenere tableau //Return type: None //Purpose: Creates the Vigenere tableau by performing a shift on //the plaintext alphabet for each Vigenere row entry. //---------------------------------------------------------------- void printTable(const char TABLE[][TWENTYSEVEN]) { char junk[SIZE]; for (int ix=0; ix<TWENTYSIX; ix++){ cout << TABLE[ix][0] << "\t"; cout << TABLE[ix]; //get any characters the user may have types off //the input stream and discard them cin.getline(junk, SIZE, '\n'); } return; }//end printTable() //end file poly1.cpp
Previous | Table of Contents | Next |