56.

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

Earlier in this chapter you developed the programs POLY2.BAS and POLY2.CPP to encipher messages using a polyalphabetic substitution technique. Because you need a mechanism to automate the decipherment of such messages, I will now focus your attention upon the development of a program to reverse the encipherment process. I will label these two programs DPOLY2.BAS and DPOLY2.CPP to denote that they decipher messages enciphered using the POLY2.BAS and POLY2.CPP programs.

In this section you will first examine the operation of the BASIC program DPOLY2.BAS. Once this is accomplished I will turn your attention to the statements in the C++ version of the program.

Listing 5.8 shows the statements in the main portion of the program DPOLY2.BAS developed to decipher messages previously enciphered using the program POLY2.BAS. When you examine Listing 5.8, you will note that the only differences between the main portion of the programs are changes to PRINT statements that essentially replace “cipher” with “decipher” and the prefix of two subroutines by the character D to denote major changes to previously developed subroutines without that prefix. Because the only subroutines that were significantly changed were MSGFILE and PCONVERTSTORE, I will focus on these two subroutines.

Listing 5.8. The main portion of the DPOLY2.BAS program.

 REM PROGRAM DPOLY2.BAS DIM PLAINTEXT$(26), PLAIN$(26), CIPHER$(26), CIPHERTEXT$(26), KEY$(26)     CLS     PRINT "DPOLY2.BAS PROGRAM deciphers text previously enciphered                 using two keywords"     PRINT "or keyword phrases and a polyalphabetic substitution process     using"     PRINT "26 cipher alphabets."     PRINT     INPUT "Enter plaintext keyword or keyword phrase in UPPERCASE: ",            TEXT$ GOSUB INITIALIZE           'initialize plaintext alphabet GOSUB KEYWORD                'form keyword based mixed alphabet    FOR I = 0 TO 25: PLAIN$(I) = PLAINTEXT$(I): NEXT I    INPUT "Enter ciphertext keyword or keyword phrase in UPPERCASE:",                 TEXT$ GOSUB INITIALIZE           're-initialize plaintext alphabet GOSUB KEYWORD    FOR I = 0 TO 25: CIPHER$(I) = PLAINTEXT$(I): NEXT I GOSUB PFORMCIPHER           'create 26 cipher alphabets GOSUB DMSGFILE            'assign I/O files, place message on a file GOSUB DPCONVERTSTORE           'convert and store ciphertext on a file GOSUB PRTOUT             'print results STOP 

The DMSGFILE Subroutine

The major differences between the previously developed subroutine MSGFILE and the subroutine DMSGFILE are related to the assignment of the string variables OUTFILE$ and INFILE$. In the subroutine DMSGFILE, these assignments are reversed from their assignment in MSGFILE. Because the contents of the subroutine DMSGFILE were described as part of our description of the program DCIPHER6.BAS and other programs in Chapter 4, you should refer to that chapter for information concerning this subroutine and the program listing.

The DPCONVERTSTORE Subroutine

The statements used to form the subroutine DPCONVERTSTORE are contained in Listing 5.9. Unlike the subroutine PCONVERTSTORE, which converts a plaintext message to ciphertext and stores the message in ciphertext on a disk file, DPCONVERTSTORE performs a reverse process. That is, the subroutine DPCONVERTSTORE converts an enciphered message to plaintext and stores the plaintext message on a disk file.

Listing 5.9. The DPCONVERTSTORE subroutine listing.

 DPCONVERTSTORE:    REM Routine to convert and store ciphertext on a file    OPEN INFILE$ FOR INPUT AS #1    OPEN OUTFILE$ FOR OUTPUT AS #2    C = 0              'first alphabet pointer    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            FOR J = 0 TO 25            IF MID$(TEXT$, I, 1) = MID$(CIPHERTEXT$(C), J + 1,                                   1) THEN GOTO GOTIT            NEXT J GOTIT:       MID$(TEXT$, I, 1) = PLAIN$(J)            C = C + 1            IF C = 26 THEN C = 0   'reset to first alphabet            NEXT I CLEARTXT:               WRITE #2, TEXT$    LOOP DONE1:         CLOSE #2 RETURN 

When you compare the subroutine DPCONVERTSTORE to the subroutine PCONVERTSTORE, you should note that the difference between the two subroutines is limited to the structure of an IF statement in each subroutine and the statement at label GOTIT. In PCONVERTSTORE, the first IF statement is used to match each character in the string variable TEXT$ to a character in the keyword-based alphabet stored in the array PLAIN$ and use the location of the match as a pointer into a cipher alphabet. In the subroutine DPCONVERTSTORE, each character is matched to the cipher alphabet whose value is C at position J+1.

In the subroutine PCONVERTSTORE, a branch to the label GOTIT results in the replacement of a plaintext character (MID$(TEXT$,I,1)) by a ciphertext character (MID$(CIPHERTEXT$(C),J+1,1)) in the cipher alphabet C at position J+1. In the subroutine DPCONVERTSTORE, you should replace each enciphered character with its equivalent plaintext character. Because a match using the previously described IF statement results in the location of the correct alphabet and location within the alphabet, we then set each character in TEXT$ to the array element PLAIN$(J) to extract the appropriate plaintext character. This allows you to convert the ciphertext back into its equivalent plaintext character.

Program Execution

To verify the operation of the program DPOLY2.BAS, execute it using the same plaintext and ciphertext keywords used to encipher your message using the POLY2.BAS program. Figure 5.6 illustrates the execution of the DPOLY2.BAS program using the plaintext keyword MICROSOFT and the ciphertext keyword phrase WINDOWSAREGREAT. The lower portion of this example shows the resulting deciphered message. You will note that this message is the same message that was entered as plaintext when the program POLY2.BAS was executed in Figure 5.5.


Figure 5.6  The execution of the program DPOLY2.BAS.

Other differences between DPOLY2.BAS and POLY2.BAS relate to PRINT statements that were changed to display “deciphered” instead of “enciphered” in the subroutine PRTOUT. Because this was an insignificant change, I did not change the name of the subroutine; however, the PRINT statements in the subroutine were changed when the program was stored on the file DPOLY2.BAS on the companion CD-ROM.


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