61.

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

Listing 6.4 contains the program listing and sample execution of the program RANDOM2.BAS. This simple program was developed to illustrate how you can use random numbers to encipher messages. In this example, our plaintext alphabet is restricted to the 26 uppercase letters. This alphabet is initialized by the INITIALIZE subroutine. Next, the program uses a user-entered number to seed the random number generator and accepts a one-line message read into the string variable TEXT$. The characters in that message are then placed into the string array KEY$.

Listing 6.4 The RANDOM2.BAS program listing and its repeated execution using different random seed numbers and the same plaintext message.

 REM Program RANDOM2.BAS REM Sample program to demonstrate use of random numbers for enciphering DIM KEY$(80), PLAINTEXT$(26) CLS    PRINT "Program RANDOM2.BAS to demonstrate use of random numbers and"    PRINT "random number seed in enciphering operations" GOSUB INITIALIZE START: PRINT    INPUT "Enter a random seed number, 0 to terminate ", Y    IF Y = 0 THEN STOP    RANDOMIZE (Y)    INPUT "Enter a one line message in UPPERCASE: ", TEXT$    FOR I = 1 TO LEN(TEXT$)    KEY$(I) = MID$(TEXT$, I, 1)    NEXT I REM Encipher    PRINT "Enciphered message is               : ";    FOR I = 1 TO LEN(TEXT$)    FOR J = 0 TO 25    IF PLAINTEXT$(J) = KEY$(I) GOTO GOTIT   'locate character position    NEXT J                                  ' in plaintext array GOTIT: X = INT(RND * 100)                      'get random number    IF X > 25 THEN GOTO GOTIT               ' less than 25    Z = (J + X) MOD 26                      'use MOD 26 addition to add    PRINT PLAINTEXT$(Z);                    'display enciphered character    NEXT I    GOTO START 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 Program RANDOM2.BAS to demonstrate use of random numbers and random number seed in enciphering operations Enter a random seed number, 0 to terminate 5 Enter a one-line message in UPPERCASE: FIREFREDNOW Enciphered message is: CPKWBXVSORN Enter a random seed number, 0 to terminate 8 Enter a one-line message in UPPERCASE: FIREFREDNOW Enciphered message is: RDMXNQVCJXE Enter a random seed number, 0 to terminate 51 Enter a one-line message in UPPERCASE: FIREFREDNOW Enciphered message is: ZXDDMCBNHGZ Enter a random seed number, 0 to terminate 0 

The routine labeled ENCIPHER matches each character in the message that was placed in the string array KEY$ to a plaintext character. When a match is found, a branch to the label GOTIT occurs, and a two-digit random number is obtained. Next, the program discards the random number if it exceeded 25 and obtains a new random number, forcing x to be between 0 and 25.

Once an acceptable value of x is obtained, the index value of J, which represents the plaintext character location in the alphabet, is added via modulo 26 addition to the random number. By restricting the largest random number to 25 and the plaintext character position to 26, the largest number produced by adding the two becomes 51. Performing modulo 26 addition prevents a resulting number occurring from different additions, such as would be the case if a random number added to the index resulted in a value of 52 or more. For example, 1 mod 26 is 1, but so is 53 mod 26 and 79 mod 26. By limiting the maximum value of the sum of the index and random number to 51, you ensure you can correctly decipher a message that was enciphered using the technique previously described.

The RANDOM1.CPP Program

Since the purpose of this book is to acquaint you with both BASIC and C++ encryption and decryption coding methods, let’s turn our attention to two C++ random number programs. The first program, RANDOM1.CPP, prompts you for a seed and uses that seed to generate a sequence of random numbers between 1 and 100, only displaying those numbers that are less than 25.

The top portion of Listing 6.5 contains the statement in the program RANDOM1.CPP, while the lower portion shows three examples of its execution. Although this program was written to attempt to duplicate the program RANDOM1.BAS, the lack of a C++ RANDOMIZE statement resulted in the development of the function getSeed which is used to return a user-entered seed value. That value is used with the C++ function SRAND to generate 75 random numbers, with only those numbers whose values do not exceed 25 being displayed.

If you compare the random numbers shown at the bottom of Listing 6.5 to those produced by the execution of the BASIC version of the program, you will note different results even though the same seed values were used. These differences result from the fact that most programming languages use a different algorithm to generate random numbers.


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