28.

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

The actual deciphering process is performed by the subroutine DECIPHER whose contents are listed in Listing 2.17. In examining the statements in the subroutine DECIPHER, readers should recognize its close resemblance to the subroutine CONVERTSTORE in the CIPHER4.BAS program. The only difference between the two subroutines is the search mechanisms bounded by the nested FOR-NEXT loops. In the DECIPHER subroutine, each character in the enciphered message is compared against the ciphertext alphabet. When a match occurs, the position of the character in the ciphertext alphabet is used as a pointer to extract a character from the plaintext alphabet. Also note that the first character of each line of data retrieved from the ciphertext file is examined to determine if a forward slash or backslash character is in the first position of the line. Similar to the encipherment process, the decipherment process uses those characters to bypass the decipherment process (/) or to terminate the decipherment process (\).

Listing 2.17 The DECIPHER subroutine.

 DECIPHER:    REM Routine to decipher 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 

The DPRTOUT Subroutine

The last subroutine required for decipherment is appropriately labeled DPRTOUT to reflect the fact that it prints the results of the decipherment process. Unlike the subroutine PRTOUT which limited the number of characters to 25 per line and required extensive logic to operate on strings and invoke the GROUPBY5 subroutine, DPRTOUT is very simple. DPRTOUT simply reads each line in the file containing the deciphered message, eliminates any forward slash character used as a line prefix, and prints each line as is. Listing 2.18 lists the contents of that subroutine.

Listing 2.18 The DPRTOUT subroutine.

 DPRTOUT: REM Subroutine to print results    INPUT "Press Return key to display resulting enciphered message", p$    CLS    PRINT "Resulting deciphered message is:"    OPEN OUTFILE$ FOR INPUT AS #2    DO UNTIL EOF(2)       INPUT #2, TEXT$       IF MID$(TEXT$, 1, 1) = "/" THEN PRINT RIGHT$(TEXT$,                          LEN(TEXT$) - 1)       IF MID$(TEXT$, 1, 1) <> "/" THEN PRINT TEXT$    LOOP RETURN 

To illustrate the composition of the resulting decipherment program, Listing 2.19 lists the main portion of the program DCIPHER4.BAS. You should note that two of the five subroutines, INITIALIZE and FORMCIPHER, are exactly the same as those subroutines used in CIPHER4.BAS. The three remaining subroutines, DMSGFILE, DECIPHER, and DPRTOUT, are modified versions of the subroutines MSGFILE, CONVERTSTORE, and PRTOUT. The entire decipherment program which deciphers a message enciphered using a simple monoalphabetic substitution process is stored on the file DCIPHER4.BAS on the CD-ROM under the BASIC directory. The executable program in that directory is named DCIPHER4.EXE.

Listing 2.19 The main portion of the DCIPHER4.BAS program.

 REM PROGRAM DCIPHER4.BAS DIM PLAINTEXT$(26), CIPHERTEXT$(26) CLS GOSUB INITIALIZE    PRINT "DCIPHER4.BAS PROGRAM deciphers a message using a simple"    PRINT "monoalphabetic substitution process." 1   INPUT "Enter UPPERCASE Alphabetic Shift Key: ", K$    FOR I = 0 TO 25    IF K$ = PLAINTEXT$(I) GOTO 2    NEXT I    PRINT "You must enter a letter from A to Z"    GOTO 1 2  REM Position I represents shift key letter GOSUB FORMCIPHER         'create cipher alphabet GOSUB DMSGFILE         'assign I/O files, place message on a file GOSUB DECIPHER         'convert enciphered message to plaintext GOSUB DPRTOUT          'print results STOP 

Program Execution

To illustrate the operation of the DCIPHER4.BAS program, you can execute the program with input from the keyboard and from a file. To facilitate a comparison of the deciphered message to the original plaintext message, you can use the enciphered message contained at the bottom of Figure 2.6 as input to DCIPHER4.BAS.

Figure 2.8 illustrates the operation of DCIPHER4.BAS in which keyboard input was selected. In this example, you must use the keyboard to enter the ciphertext message in the same manner as it was displayed as a result of the execution of CIPHER4.BAS. That is, you must prefix each cleartext line with a forward slash, enter each line using the five-character groups contained on the line, and terminate the enciphered message with a backslash character on a separate line.


Figure 2.8  The execution of DCIPHER4.BAS using keyboard input.

The execution of DCIPHER4.BAS using keyboard input will display up to 25 characters per line because input was limited to five groups of five characters per line. In comparison, the use of file input when DCIPHER4.BAS is executed deciphers each full line contained on the file whose name was assigned to the variable OUTFILE$ (Figure 2.9). The use of file input may provide a more natural conversion to plaintext since words do not have to be split among two lines if they do not fit into a five-character group terminating a line.


Figure 2.9  The execution of DCIPHER4.BAS using file input.


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