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

The process of assigning I/O files must be reversed for decipherment. That is, the filename used for the input of the plaintext message in the CIPHER5.BAS program becomes the output file. Similarly, the filename used for the storage of the enciphered message in the CIPHER5.BAS program becomes the input file for decipherment operations. To provide those modifications, you must change the use of the string variables OUTFILE$ and INFILE$ previously used in the subroutine MSGFILE. To change I/O file assignments, simply reverse the use of the previously mentioned string variables.

Listing 3.6 lists the statements in the new subroutine named DMSGFILE, which contains the reversal of I/O file assignments. If you compare this subroutine to the previously developed subroutine MSGFILE, you should note that the difference between the two is limited to the previously mentioned reversal of I/O filename assignments.

Listing 3.6 The DMSGFILE subroutine.

DMSGFILE:
   REM Routine to assign I/O files and accept keyboard or file input
   REM and remove spaces between words
       INPUT "Enter filename to store plaintext message,
                         default=MESSAGE.DAT", OUTFILE$
       IF OUTFILE$ = "" THEN OUTFILE$ = "MESSAGE.DAT"
       INPUT "Enter filename for enciphered message,
                         default=CIPHERTX.DAT", INFILE$
       IF INFILE$ = "" THEN INFILE$ = "CIPHERTX.DAT"
       INPUT "Select keyboard (k) or file (f) message input: ",
                         IN$
       IF IN$ = "F" OR IN$ = "f" THEN RETURN
       OPEN INFILE$ FOR OUTPUT AS #1
   REM Routine to place message on a file removing spaces between
                         words
       PRINT "Enter your message - place a / at the beginning of
                         each line"
       PRINT "that should remain in plaintext and a \ on a
                         separate line"
       PRINT "to indicate the end of the enciphered message"
       PRINT
AGN:     LINE INPUT TEXT$
       IF MID$(TEXT$, 1, 1) = "/" THEN GOTO XT
       NTEXT$ = ""
       FOR I = 1 TO LEN(TEXT$)
       NTEXT$ = NTEXT$ + LTRIM$(MID$(TEXT$, I, 1))
       NEXT I
       WRITE #1, NTEXT$
       IF MID$(TEXT$, 1, 1) = "\" GOTO DONE
       GOTO AGN
XT:     WRITE #1, TEXT$
       GOTO AGN
DONE:     CLOSE #1
RETURN

The DCONVERTSTORE Subroutine

The second subroutine that required modification for deciphering is CONVERTSTORE. As previously discussed, the modified subroutine was renamed DCONVERTSTORE to reflect its modification for decipherment operations.

Lisitng 3.7 gives the statements contained in the subroutine DCONVERTSTORE. Other than changing two REM statements to reflect the fact that the routine now converts ciphertext to plaintext, the only additional changes relate to search and replacement operations. The IF statement in the nested FOR-NEXT loop now compares each character in the string variable TEXT$ to each character in the CIPHERTEXT$ array instead of the PLAINTEXT$ array. When a match occurs, the character in TEXT$ is replaced by a character in the array PLAINTEXT$ located at the same position in the array where the character in TEXT$ matched the character in the array CIPHERTEXT$. Thus, DCONVERTSTORE simply makes use of the plaintext alphabet stored in the array PLAINTEXT$ and the ciphertext alphabet stored in the array CIPHERTEXT$ in the reverse manner in which they were used in the subroutine CONVERTSTORE.

Listing 3.7 THE DCONVERTSTORE subroutine.

DCONVERTSTORE:
   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
            FOR J = 0 TO 25
            IF MID$(TEXT$, I, 1) = CIPHERTEXT$(J) THEN GOTO GOTIT
            NEXT J
GOTIT:        MID$(TEXT$, I, 1) = PLAINTEXT$(J)
            NEXT I
CLEARTXT:       WRITE #2, TEXT$
   LOOP
DONE1:     CLOSE #2
RETURN

Program Operation

To illustrate the operation of the DCIPHER5.BAS program, you can execute it using the previously enciphered message to the fictional JOHN P. BIDDER as input. Regardless of whether you use the keyboard or specify a file for input of the ciphered messaged, the program produces the same result. Figure 3.3 illustrates the execution of DCIPHER5.BAS in which the keyboard is used to enter the received enciphered message. Because the previously developed subroutine PRTOUT was used to display the resulting deciphered message, that message is displayed in groups of five characters as indicated in the lower portion of Figure 3.3. Although the program converted the ciphertext message back into its plaintext character representation, it did not restore the message to its original format with spaces between words and words rebuilt from two or more five-character groupings when it crossed such groupings. Those functions cannot be performed when the encipherment method does not substitute a character for the space character and results in the human recipient of the deciphered message being responsible for understanding how to correctly read the deciphered message.


Figure 3.3  The execution of DCIPHERS.BAS


Previous Table of Contents Next