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


Because the subroutine SETUP provides a mechanism to select a random seed and positions you into a seed, in an earlier version of this book I purposely skipped a discussion of the code used in that subroutine (discussing the mechanism used to obtain the seed and position within the seed is similar to giving a burglar the keys to the store). The earlier version offered a reward of $1,000 to the first person who could decode an encrypted file. Although the prize was won, it required approximately a year of effort on the part of many persons until a satisfactory solution was found. Because no prize is now offered, the entire source file is included on the CD-ROM to permit you to experiment in creating different length keys.

The code used in the subroutine KEYWORD is contained in Listing 7.3. This subroutine is most interesting as it uses the “secret” code to develop a 30-character keyword. Note that the expansion of the 6-character “secret” code to a 30-character keyword substitutes the string “9” for the characters “\”, “/”, and the double quote whose ASCII value is 34. To preclude any extended ASCII characters occurring in the keyword, any character in the expansion process that has an ASCII value above 127 is set to the string “Q.” The remaining modules in the subroutine are similar to modules I developed in the original KEYWORD subroutine. The main difference between the remainder of the subroutines concerns the length of the array PLAINTEXT$, which now contains 93 characters and whose element value now ranges from 0 to 92.

Listing 7.3 The KEYWORD subroutine.

KEYWORD:
   REM Place entered keyword into KEY$ array 1 character per position
   REM but mix word thru expansion to 30 characters
     MSGLEN = 30
     J = 1
     FOR I = 1 TO 6
AGN2:  IF J >= 31 THEN GOTO NULLIT
     KEY$(J) = MID$(CODE$, I, 1)
     FOR J = J + 1 TO J + 4
     KEY$(J) = CHR$(ASC(MID$(CODE$, I, 1)) + (J - 1))
     IF KEY$(J) = "\" OR KEY$(J) = "/" OR KEY$(J) = CHR$(34) THEN
KEY$(J) = "9"
     IF ASC(KEY$(J)) > 127 THEN KEY$(J) = "Q"
     NEXT J
INCIT: NEXT I
   REM ELIMINATE DUPLICATE LETTERS, REPLACE WITH CARRIAGE RETURNS
NULLIT: K = 2
     FOR I = 1 TO MSGLEN
     FOR J = K TO MSGLEN
     IF KEY$(I) <> KEY$(J) GOTO NOTDUP
     KEY$(J) = CHR$(13)
NOTDUP:  NEXT J
     K = K + 1
     NEXT I
   REM REMOVE CARRIAGE RETURNS IN STRING
     X$ = ""
     FOR I = 1 TO MSGLEN
     IF KEY$(I) = CHR$(13) THEN GOTO ASKP
     X$ = X$ + KEY$(I)
ASKP:  NEXT I
   REM PLACE REVISED KEYWORD WITH NO DUPLICATE LETTERS BACK IN KEY$
     FOR I = 1 TO LEN(X$)
     KEY$(I) = MID$(X$, I, 1)
     NEXT I
   REM COMPARE KEY$ & PLAINTEXT$ ARRAYS, SET PLAINTEXT$ ELEMENT TO CR WHEN
MATCHED
     FOR J = 1 TO LEN(X$)
     FOR K = 0 TO 92
     IF KEY$(J) = PLAINTEXT$(K) THEN PLAINTEXT$(K) = CHR$(13)
     NEXT K
     NEXT J
   REM CREATE ONE STRING
     FOR I = 0 TO 92
     IF PLAINTEXT$(I) = CHR$(13) THEN GOTO SKIP
     X$ = X$ + PLAINTEXT$(I)
SKIP:  NEXT I
   REM PLACE SEQUENCE BACK INTO PLAINTEXT$ ARRAY
     FOR I = 0 TO 92
     PLAINTEXT$(I) = MID$(X$, I + 1, 1)
     NEXT I
RETURN

Listing 7.4 contains the code from the modified subroutine PFORMCIPHER. In examining this subroutine, note that it forms a two-dimensional ciphertext alphabet. The index JJ varies from 0 to 35 and forms 36 cipher alphabets that are based upon the keyword exploded from the “secret” code. By now you probably recognize that ENCIPHER.BAS uses a combination of random numbers and a rotating ciphertext alphabet to perform encipherment. Exactly how this occurs is illustrated by the code in the modified subroutine RCONVERTSTORE.

Listing 7.4 The modified PFORMCIPHER subroutine.

PFORMCIPHER:
    REM routine to form 35 CIPHERTEXT alphabets based upon the
                 characters in the code
    FOR JJ = 0 TO 35
    FOR KK = 0 TO 92
    CIPHERTEXT$(JJ, KK) = PLAINTEXT$((KK + JJ) MOD 92)
    NEXT KK
    NEXT JJ
RETURN

Listing 7.5 contains the code in the modified subroutine RCONVERTSTORE. In examining the code in the subroutine RCONVERTSTORE, note that after extracting a two-digit random number using X=INT(RND*100), I divide the result by 1.087 and take the integer portion of the result. This smooths the random number to a value between 0 and 92 which corresponds to the 93 elements in the array PLAINTEXT$. Then I compare each character in each line of the plaintext to a character in the array PLAINTEXT$. When a match occurs, I add the index value of the match position in PLAINTEXT$ to the smoothed value of X using mod 92 addition. The result, stored in the variable Z, is used to extract a character from one of the ciphertext alphabets contained in the string array CIPHERTEXT$. Thus, ENCIPHER.BAS uses random numbers to extract characters from a two-dimensional alphabet to obtain enciphered text. The routines used for enciphering illustrate how you can combine two or more techniques to develop your own enciphering program if you wish to do so.


Previous Table of Contents Next