53.

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

Listing 5.4 illustrates the statements contained in the subroutine PFORMCIPHER. As noted from the REM statement at the beginning of this subroutine, its function is to form 26 ciphertext alphabets using the first keyword character as a basis for generating each alphabet.

Listing 5.4 The PFORMCIPHER subroutine.

 PFORMCIPHER:     REM routine to form 26 CIPHERTEXT alphabets based upon the first                  keyword character     FOR ZI = 0 TO 25     GOSUB INITIALIZE     FOR J = 0 TO 25     IF CIPHER$(ZI) = PLAINTEXT$(J) THEN GOTO GOTYA     NEXT J GOTYA:  J = J + 1     FOR K = 0 TO 25     CIPHERTEXT$(ZI) = CIPHERTEXT$(ZI) + PLAINTEXT$((K + J) MOD 26)     NEXT K     NEXT ZI     LPRINT TAB(5); RETURN 

The subroutine PFORMCIPHER contains three FOR-NEXT loops. The outer loop uses the index ZI which varies from 0 to 25 and is used to develop 26 ciphertext alphabets. After the first FOR statement, invoke the subroutine INITIALIZE to set the plaintext alphabet in its sequence into the array PLAINTEXT$. The FOR-NEXT loop using the index J then compares the character in the appropriate location of the string array CIPHER$ to each character in PLAINTEXT$ until a match occurs. When a match occurs, the character in the string array CIPHER$, which represents the first character in the ciphertext alphabet ZI, is matched to position J in the plaintext alphabet and a branch to the label GOTYA occurs. Because you want to shift the matched character to the last column, increment J by 1 prior to using the FOR-NEXT loop with K as the index to form the ciphertext alphabet. The execution of the subroutine PFORMCIPHER produces the 26x26-character matrix previously illustrated in Figure 5.4, as CIPHERTEXT$(ZI) varies from 0 to 25 when WINDOWSAREFUN is used as the ciphertext keyword phrase.

The PCONVERTSTORE Subroutine

The subroutine PCONVERTSTORE is similar to the subroutine CONVERTSTORE, although it is modified to sequentially pick a ciphertext character from each cipher alphabet in sequence until the last alphabet is reached. The ciphertext character extraction process begins anew with the first cipher alphabet.

Listing 5.5 contains the statements in the subroutine PCONVERTSTORE. The statement that sets the variable C to a value of 0 also sets the pointer to the first cipher alphabet. The next modification to CONVERTSTORE is the replacement of the string array PLAINTEXT$ by the string array PLAIN$ in the first IF statement in the subroutine. This statement mates a character from the plaintext stored in the string variable TEXT$ to a character in the string variable PLAIN$ which contains the keyword-based mixed plaintext alphabet. When a match occurs, J points to the location in the keyword-based mixed alphabet that serves as a pointer to each cipher alphabet for the extraction to a cipher character. However, when you branch to the label GOTIT, you must increment J by 1 because you incremented J by that amount in the subroutine PFORMCIPHER.

Listing 5.5 The PCONVERTSTORE subroutine.

 PCONVERTSTORE:    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) = PLAIN$(J) THEN GOTO GOTIT                NEXT J GOTIT:           MID$(TEXT$, I, 1) = MID$(CIPHERTEXT$(C), J + 1, 1)                C = C + 1                IF C = 26 THEN C = 0     'reset to first alphabet                NEXT I CLEARTXT:          WRITE #2, TEXT$    LOOP DONE1:         CLOSE #2 RETURN 

Once a match occurs and you branch to the label GOTIT, the character in the string TEXT$ is replaced by a character from the cipher alphabet whose index value is C at position J+1 through the use of the MID$ statement. C is then incremented by 1 to point to the next cipher alphabet. However, if C equals 26 it is reset to a value of 0 to point back to the first cipher alphabet.

Program Execution

Figure 5.5 illustrates an example of the execution of POLY2.BAS using MICROSOFT as the plaintext keyword and WINDOWSAREGREAT as the ciphertext keyword phrase. I purposely changed the ciphertext keyword from WINDOWSAREFUN to WINDOWSAREGREAT to illustrate one deficiency of keyword-based polyalphabetic substitution systems—common keywords or keyword phrases. (The POLY2.BAS program enciphers text based on the use of two keywords or keyword phrases using a polyalphabetic substitution process using 26 cipher alphabets.)


Figure 5.5  The execution of POLY2.BAS

At this point it may be useful to manually encipher a portion of the cleartext message YOU CONFUSED… to develop the enciphered message VNIGT KHMKR EUEIX… shown in the lower portion of Figure 5.5. Using the keyword-based mixed plaintext alphabet at the top of Figure 5.4, locate Y and extract the character in the Y column from the first cipher alphabet, obtaining the character V. Next, locate the character O in the keyword-based mixed plaintext alphabet and extract the character in the plaintext O column from the second cipher alphabet to obtain the character N. Similarly, U in the plaintext message results in I being extracted from the third cipher alphabet, and so on.

In Figure 5.4, the ciphertext keyword phrase WINDOWSAREFUN was used, while the ciphertext keyword phrase WINDOWSAREGREAT was used when the program POLY2.BAS was executed. Because both keyword phrases have the first 10 characters in common, of which 9 are used to form cipher alphabets due to a duplicate W, the first 9 cipher alphabets are the same for both keyword phrases. This means that the use of a polyalphabetic substitution system will be more secure if you avoid industry-specific keyword or keyword phrases as well as other terms someone may be able to correctly guess based upon your or other employees’ work habits.


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