73.

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

To use consistent naming conventions, I labeled the programs that decipher messages enciphered using RANDOM3.BAS and RANDOM3.CPP as DRANDOM3.BAS and DRANDOM3.CPP. Listing 6.19 contains the statements in the main part of that program that are very similar to RANDOM3.BAS. In fact, the only change in addition to changes in the words in strings located in PRINT statements is the substitution of the subroutines DMSGFILE for MSGFILE and RDCONVERTSTORE for RCONVERTSTORE.

Listing 6.19 Statements in the main portion of the DRANDOM3.BAS program.

 REM Program DRANDOM3.BAS    CLS    DIM PLAINTEXT$(25)    PRINT "DRANDOM3.BAS - A program which deciphers messages"    PRINT "using the built-in BASIC random number generator"    PRINT "that were enciphered using the RANDOM3.BAS program"    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 DMSGFILE         'assign I/O files, place message on a file GOSUB RDCONVERTSTORE         'convert and store plaintext on a file GOSUB PRTOUT         'print results STOP 

The subroutine DMSGFILE essentially adjusts I/O file references to the opposite of the subroutine MSGFILE. The subroutine RDCONVERTSTORE is very similar to the subroutine RCONVERTSTORE, but converts ciphertext to plaintext instead of plaintext to ciphertext.

The RDCONVERTSTORE Subroutine

Listing 6.20 lists the statements in the subroutine RDCONVERTSTORE. In this subroutine a random number is extracted and smoothed to a value between 0 and 25 in the same manner as performed by the subroutine RCONVERTSTORE. Next, each character in the string variable TEXT$ is matched to a character in the string array PLAINTEXT$, with the position of the match assigned to the variable J.

Listing 6.20 Statements in the RDCONVERTSTORE subroutine.

 RDCONVERTSTORE:    REM Routine to convert and store plaintext 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 ciphertext to plaintext            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:       IF J < X THEN J = J + 26            MID$(TEXT$, I, 1) = PLAINTEXT$((J - X) MOD 26)            NEXT I CLEARTXT:      WRITE #2, TEXT$    LOOP DONE1:     CLOSE #2 RETURN 

To perform modulo 26 subtraction, the value of J is compared to the value of X. If J is less than X, J is incremented by 26. The value of X is then subtracted from J via modulo 26 arithmetic and the resulting value is used as an index into the string array PLAINTEXT$. The character at the index position represents the plaintext character and it is used to replace the ciphertext character in the variable TEXT$.

Figure 6.6 illustrates the operation of the program DRANDOM3.BAS. In this example the user simply enters a “secret” code and selects the default filenames and file input as the mechanism for reading the previously created enciphered message. The resulting deciphered message in the lower portion of Figure 6.6 is displayed in groups of five characters since the subroutine PRTOUT was used in the program.


Figure 6.6  Execution of the DRANDOM3.BAS program.

Readers may be curious as to the effect of entering a different “secret” code during the execution of the program DRANDOM3.BAS. To illustrate the effect, I used the code 9lives instead of 9LIVES. What may appear to be a small code change is a significant change to the mechanism developed for positioning to a place into the BASIC random number generator.

Figure 6.7 illustrates the effect of using the program DRANDOM3.BAS with an invalid secret code. If you compare the decipherment illustrated in Figure 6.6 to the decipherment illustrated in Figure 6.7, you will note that the use of an incorrect code results in a meaningless deciphered message. Thus, it is as important to keep your secret code secure as it is not to lose your code.


Figure 6.7  Using DRANDOM3.BAS with an incorrect code.


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