31.

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 KEYWORD Subroutine

It may be useful to review the operation of the subroutine KEYWORD based upon each group of statements bounded by a REM statement. The first program module simply stores each character in the keyword into an element of the KEY$ string array. The second program module uses a nested pair of FOR-NEXT loops to eliminate duplicate letters in the keyword, replacing each duplicate letter with a null character. To illustrate the operation of this program module, assume that the keyword ALPHA, which has a string length of five, was used. In the program module, the inner loop (J) operates from 2 to 5, while the outer loop operates from 1 to 5. Thus, when I is 1, J varies from 2 to 5. This results in A being compared to L, P, H, and A. If KEY$(I) does not equal KEY$(J), a branch to the label NOTDUP occurs and J is incremented. If KEY$(I) equals KEY$(J) the statement KEY$(J)=“ ” sets KEY$(J) equal to a null character. After the value of J increments, K is incremented, which results in the second character in the keyword (L) being compared to the third (P) through the fifth (A) characters of the keyword.

The third program module removes the nulls previously inserted in place of duplicate characters. The statement X$=X$+LTRIM$(KEY$(I)) concatenates each element of KEY$ that is a non-null with the previous value of X$ because the string function LTRIM$ removes leading nulls from a string.

The fourth program module takes the recently formed string variable X$ which contains the keyword without duplicate characters and places those characters back into the array KEY$. The fifth program module then compares each character in the KEY$ array to each character in the PLAINTEXT$ array. If they match, the plaintext character is then set to a null. The sixth program module simply adds each non-blank element position of the PLAINTEXT array to the string X$, which now contains the keyword-based mixed alphabet. The seventh and last program module places the keyword-based alphabet into the array PLAINTEXT$.

The use of the previously described WORD.BAS program is illustrated in Figure 3.1, which indicates the QuickBASIC execution screen after the program WORD.BAS was executed three times in succession. The first time WORD.BAS was executed the keyword phrase SEVENBRIDES was used to develop the keyword-based mixed alphabet. The second time the program was executed the keyword phrase BROOKLYNBRIDGE was used, while the third keyword phrase was MICROSOFTWINDOWSISGREAT.


Figure 3.1   Results obtained from executing WORD.BAS several times.

The C++ version of WORD.BAS, which is appropriately named WORD.CPP, is contained in the C directory on the CD-ROM. Following our file naming conventions, the file WORD.EXE in the C directory is the executable version of the C++ program, while the file WORD.EXE located in the BASIC directory represents the executable version of the BASIC program.

Listing 3.2 provides the C++ version of WORD.BAS which was assigned the filename WORD.CPP. When you execute this program you will note several improvements. Those improvements include the ability to repetitively execute the program and to use the CTRL+C multi-key combination to terminate the program execution. Similar to all C++ programs developed for this book, WORD.CPP was written by subdividing activities into functions. Three key functions were developed—getKeyword, checkInput, and createAlphabet. The getKeyword function, as its name implies, prompts you for a keyword and continues prompting until a valid keyword without spaces or non-alphabetic characters is entered. The checkInput function is called by the getKeyword function and performs the actual verification of user input. The third function, createAlphabet, uses the verified keyword to construct the keyword alphabet. Since the program is documented through the use of a liberal amount of comment statements, you are referred to the program listing for a detailed explanation of the manner by which the code functions.


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