71.

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

One of the concerns you may have at this point is the possibility that each random seed results in a sequence of random numbers that repeat. To determine if this is a potential problem, a program was developed to generate the first five random numbers in a seed as two-digit integers and cycle through the seed looking for a match.

Listing 6.15 gives the statements in this program that was labeled RTEST.BAS. Figure 6.4 illustrates the results of the execution of RTEST.BAS when 1 was entered as the seed number. After a period of waiting, I terminated the execution of the program by entering a CTRL-BREAK key sequence because more than 5.7 million numbers had been tested without encountering a duplicate sequence of five numbers. With over 64,000 possible seeds and over 5.7 million non-repeating random number sequences per seed, it is obvious that it will be very difficult to duplicate a position selected by a well-thought-out code word that is manipulated in a well-thought-out manner.


Figure 6.4  Sample execution of the RTEST.BAS program.

Listing 6.15 The RTEST.BAS program listing, which tests for a repeated sequence within a random number seed.

 REM Random sequence test program RTEST.BAS     CLS     PRINT "TEST FOR 5 REPEATED DIGITS AT BEGINNING OF SEQUENCE"     POSITION = 3     INPUT "Enter random generator seed number "; x     RANDOMIZE x     FOR I = 1 TO 5  'get first five random numbers as 2-digit integers     A(I) = INT(RND * 100)     NEXT I     k = 1 again: FOR I = 1 TO 5   'get next group of five random numbers as                   2-digit integers     b(I) = INT(RND * 100)     NEXT I     FOR I = 1 TO 5    'do they match?     IF A(I) <> b(I) THEN GOTO skip     NEXT I     LOCATE POSITION, 1     PRINT "match occured at number =", k     POSITION = POSITION + 1     GOTO again skip: k = k + (6 - I)      LOCATE 1, 60      PRINT "search at #"; k      GOTO again 

Now that you have a subroutine that can be used for positioning with the BASIC and C++ random number generation routines, you can develop enciphering programs.

The RANDOM3.BAS Program

Listing 6.16 contains the statements in the main portion of the enciphering program stored in the file RANDOM3.BAS on the BASIC directory on the companion CD-ROM. This program was constructed to encipher messages using the built-in BASIC random number generator using the previously described subroutine SETUP to obtain a difficult-to-duplicate position within the random number generator.

Listing 6.16 Statements in the main portion of the RANDOM3.BAS program.

 REM Program RANDOM3.BAS     CLS     DIM PLAINTEXT$(25)     PRINT "RANDOM3.BAS - A program which enciphers messages"     PRINT "using the built-in BASIC random number generator"     PRINT RTN:     INPUT "Enter your secret code (6 characters maximum): "; CODE$     IF LEN(CODE$) > 6 THEN GOTO RTN GOSUB INITIALIZE          'initialize plaintext values GOSUB SETUP              'obtain random seed and position in seed GOSUB MSGFILE          'assign I/O files, place message on a file GOSUB RCONVERTSTORE         'convert and store ciphertext on a file GOSUB PRTOUT           'print results STOP 

When you examine the statements contained in Listing 6.16, note that the only new subroutine is RCONVERTSTORE, with the prefix R in the name of the subroutine used to identify its modification for use in our random program operation. Thus, in discussing the operation of this program, I will focus attention upon the new subroutine developed for use in the program.

The RCONVERTSTORE Subroutine

Listing 6.17 lists the statements in the subroutine RCONVERTSTORE. The difference between this subroutine and the previously described CONVERTSTORE subroutine are the statements bounded by the FOR-NEXT loop.

Listing 6.17 Statements in the RCONVERTSTORE subroutine.

 RCONVERTSTORE:    REM Routine to convert and store ciphertext on a file    OPEN INFILE$ FOR INPUT AS #1    OPEN OUTFILE$ FOR OUTPUT AS #2    DO UNTIL EOF(1)        INPUT #1, TEXT$        MSGLEN = LEN(TEXT$)        IF MID$(TEXT$, 1, 1) = "/" THEN GOTO CLEARTXT        IF MID$(TEXT$, 1, 1) = "\" THEN GOTO DONE1        REM Convert plaintext to ciphertext            FOR I = 1 TO MSGLEN            X = INT(RND * 100)       'get 2-digit integer            X = INT(X / 3.85)        'smooth to 0 to 25            FOR J = 0 TO 25            IF MID$(TEXT$, I, 1) = PLAINTEXT$(J) THEN GOTO                                                    GOTIT            NEXT J GOTIT:       MID$(TEXT$, I, 1) = PLAINTEXT$((X + J) MOD 26)            NEXT I CLEARTXT:      WRITE #2, TEXT$    LOOP DONE1:     CLOSE #2 RETURN 

When converting the subroutine for operation with random numbers, the statement X=INT(RND*100) obtains a one- or two-digit number between 0 and 99. The next statement lowers the maximum resulting value of the modified random number by dividing the number by 3.85 and taking the integer of the result. This smooths the random number to a value between 0 and 25, which is equivalent to the index values in the array PLAINTEXT in which the plaintext alphabet is stored.

The inner FOR-NEXT loop which increments J from 0 to 25 compares each character from a line of input from file #1 to a letter in the string array PLAINTEXT$. When a match occurs, J represents the position in the array PLAINTEXT$ where the match occurred. The branch to the label GOTIT results in the mod 26 addition of the value of J to the smoothed value of the extracted random number. The mod 26 addition of X and J is then used as an index to extract a new character from the string array PLAINTEXT$, which is used to replace the plaintext character in TEXT$. The subroutine RCONVERTSTORE uses the plaintext character set as a mechanism to generate ciphertext based upon the previously described algorithm.

Figure 6.5 illustrates the execution of the program RANDOM3.BAS. In this example, the code 9LIVES was used to obtain a position in the BASIC random number generator. Because the program uses the previously developed PRTOUT subroutine, the resulting enciphered message is printed in groups of five characters as illustrated at the bottom of Figure 6.5.


Figure 6.5  Execution of RANDOM3.BAS.


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