Previous | Table of Contents | Next |
In this section, you will construct a program for enciphering and deciphering based upon the use of Microsofts QuickBASIC random number generator, as well as a C++ equivalent. To do so, you will develop a mechanism to obtain a position in the random number generator which is difficult to duplicate.
To develop a subroutine to obtain a difficult-to-duplicate position within the random number generator, you must consider both the number of character positions you will use in a code your program will operate upon to obtain a location in the random number generator as well as the characters that can be used for each position. For example, a two-position code restricted to numerics is limited to containing values from 00 to 99, or 100 (102) combinations. If those two positions can contain alphanumeric characters, each position can contain 36 combinations, while the two positions represent a total of 362, or 1,296, combinations.
Table 6.2 lists the number of combinations resulting from the use of code lengths varying from 1 to 10 positions, with each position containing up to 10 (numeric only), 26 (alphabetic only), 36 (alphanumeric), and 128 (any seven-bit ASCII) permissible characters. As indicated in Table 6.2, an expansion of the number of code positions and/or the number of characters permitted to be entered in each position significantly increases the possible number of code combinations. Because most people can easily remember a six-position code, use this type of code to develop a mechanism to obtain a difficult-to-duplicate position within the built-in BASIC random number generator. Using six positions and allowing alphanumeric characters to be placed in each position results in 2,176,782,336 possible combinations! If you extend the characters that can be placed into each code position to the normal ASCII code, you would obtain 439*1010 possible combinations. Because characters should be converted to their ASCII numeric representation to use for positioning purposes, you need a mechanism to perform this operation.
X | 10^X | 26^X | 36^X | 128^X |
1 | 100.00E-01 | 260.00E-01 | 360.00E-01 | 128.00E+00 |
2 | 100.00E+00 | 676.00E+00 | 129.60E+01 | 163.84E+02 |
3 | 100.00E+01 | 175.76E+02 | 466.56E+02 | 209.72E+04 |
4 | 100.00E+02 | 456.98E+03 | 167.96E+04 | 268.44E+06 |
5 | 100.00E+03 | 118.81E+05 | 604.66E+05 | 343.60E+08 |
6 | 100.00E+04 | 308.92E+06 | 217.68E+07 | 439.80E+10 |
7 | 100.00E+05 | 803.18E+07 | 783.64E+08 | 562.95E+12 |
8 | 100.00E+06 | 208.83E+09 | 282.11E+10 | 720.58E+14 |
9 | 100.00E+07 | 542.95E+10 | 101.56E+12 | 922.34E+16 |
10 | 100.00E+08 | 141.17E+12 | 365.62E+13 | 118.06E+19 |
Listing 6.11 illustrates a short segment of code to read characters and display their ASCII values. In this example, the program executes continuously until a break occurs. After reading a string that is assigned to the string variable A$, the program obtains the length of the string. Next, a FOR-NEXT loop extracts each character from the string through the use of the MID$ function and prints the ASCII value of each character through the use of the ASC function. The lower portion of Listing 6.11 illustrates the resulting ASCII values of each character from six strings entered as codes. Note that the code can be all alphabetic (GODAWGS), numeric (362436), alphanumeric (NINE9), or include special characters such as the exclamation mark that are included in the normal ASCII character set (POWER!).
Listing 6.11 Using ASCII character values.
CLS START: INPUT A$ X = LEN(A$) FOR I = 1 TO X PRINT ASC(MID$(A$, I, 1)); ; NEXT I PRINT GOTO START ? GODAWGS 71 79 68 65 87 71 83 ? YEATIGERS 89 69 65 84 73 71 69 82 83 ? 362436 51 54 50 52 51 54 ? POWER! 80 78 87 69 82 33 ? NINE9 78 73 78 69 57 ? $39.99 36 51 57 46 57 57 ?
At the top of Listing 6.12 you will find a C++ version of the BASIC program in Listing 6.11. The C++ version looks at each character entered. This can result in an interesting display that may require a bit of interpretation. Thus, lets examine the output of the program shown in the lower portion of Listing 6.12.
In examining the execution of the program ASCII.CPP, note that if you enter characters separated by a space, such as 1 2 4 6, the ASCII value of 32 which represents a space will be displayed. Thus, the ASCII values for the sequence 1 2 4 6 is displayed as 49 32 50 32 52 32 54. The preceding is true for both alphabetic and numeric characters as indicated by the entry of the character sequence A B C D in the lower portion of Listing 6.12.
Previous | Table of Contents | Next |