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


An Alternative Random Process

A second process readers can consider for generating random numbers for use in enciphering operations is based upon the work of Gilbert Vernam (see the shaded box near the beginning of this chapter). Using Vernam’s method, which he employed to develop a long random key from two relatively short random keys, as a foundation, you can perform a similar operation through the use of two random seed numbers.

To develop this alternative process, assume that you retain the use of a six-character “secret” code word. You can use the ASCII values of the code word as a mechanism to select two random number seeds. For each seed selected, you can position yourselves into the random number sequence and extract 1,000 random numbers from the first sequence and 999 from the second. You can then develop a routine I will call ROTOR that operates upon the two random number sequences similar to Mr. Vernam’s dual paper tape reader.


When Gilbert Vernam developed the first automatic enciphering system, random characters were literally pulled from a hat. This system was time-consuming, and it limited the number of random characters on the tape to 1,000. Vernam realized that looping the tape repeated the random character sequence used for encipherment operations every 1,000 characters. To overcome this weakness, Vernam designed a special tape reader that read two tapes consisting of randomly selected characters. The contents of each tape were added to one another using modulo addition. When a cycle of the shorter tape was completed, the longer tape was repositioned by one character position and the sequence of modulo addition by character position was repeated. The longer tape contained 1,000 randomly selected characters, and the shorter tape contained 999 randomly selected characters, which resulted in the use of two tapes extending the random character key sequence to 999,000 before the sequence repeated.

By extending the random cipher key from 1,000 to 999,000 characters, the security of Vernam’s automated enciphering system was significantly enhanced. This extension permitted hundreds to thousands of messages to be enciphered before a new pair of tapes was necessary to prevent the key from repeating.


The ROTOR.BAS Program

Listing 6.21 contains the statements in the program ROTOR.BAS which is used to illustrate how you can create your own random number sequence generating routing that can be considerably different from that contained in BASIC but is based upon the use of the BASIC random number generator.

Listing 6.21 The ROTOR.BAS program listing.

REM PROGRAM ROTOR.BAS
REM This program constructs a new random number sequence of numbers that
REM range in value from 0 to 99 based upon the extraction of random numbers
REM from two seeds whose location are based on the composition of a
REM six-position code word. This sequence repeats after 999,000 characters
REM with the same secret code.
CLS
DIM R1(1000), R2(999)
RTN:
      INPUT “Enter your secret code (6 characters maximum): “; CODE$
      IF LEN(CODE$) > 6 THEN GOTO RTN
GOSUB SETUP
      P1 = 0
      P2 = 1
      FOR K=1 TO 500
GOSUB ROTOR
      NEXT K
STOP
SETUP:
      REM Routine to extract two random numbers sequences based on
      REM The composition of the secret code
      FOR I = 1 TO LEN(CODE$)
      CODE(I) = ASC(MID$(CODE$, I, 1)
      NEXT I
      SEED1 = CODE(1)*CODE(3)
      X1 = CODE(5)
      DO UNTIL X1<= 4
                   X1 = X1 / 2
      LOOP
      SEED1 = SEED * X1
      SEED2 = CODE(2) * CODE(4)
      X2 = CODE(6)
      DO UNTIL X2 <= 4
                    X2 = X2 / 2
      LOOP
      SEED2 = SEED2 * X2
      RANDOMIZE SEED1
      MAX = CODE(1) * CODE(3) * CODE(5)
      FOR I = 1 TO MAX STEP CODE(2)       'position into sequence
            DUMMY = RND
      NEXT I
      FOR I = 1 TO CODE(1) * 500 STEP CODE(3) 'go further into sequence
            DUMMY = RND
      NEXT I
      FOR I = 1 TO 1000                        'obtain first sequence
            R1(i) = RND         'of random numbers
       NEXT I
       RANDOMIZE SEED2
       MAX = CODE(2) * CODE(4) * CODE(6)
       FOR I = 1 TO MAX STEP CODE(4)    'position into next sequence
             DUMMY = RND
       NEXT I
       FOR I = 1 TO CODE(4) * 8307 STEP CODE(2) 'go further into
                                                               sequence
             DUMMY = RND
        NEXT I
        FOR I = 1 TO 999                        'get second sequence
              R2(I) = RND
        NEXT I
RETURN
ROTOR:
         R = INT(((R1(P1) + R2(P2)) * 100) MOD 99)
         PRINT USING “## “; R;
         P1 = P1+ 1
         IF P1 = 1000 THEN P2 = P2 + 1
         IF P1 = 1000 THEN P1 = 0
RETURN

The two arrays in the program, R1 and R2, function similar to Mr. Vernam’s two punched paper tapes. That is, they contain sequences of 1,000 and 999 random numbers, respectively. You can perform a modulo addition operation on the numbers in the two sequences as well as rotate the relationship between the numbers in each sequence to generate a new random number sequence.

After you enter your “secret” code, it is assigned to the string variable CODE$, similar to the previously developed RANDOM3.BAS program. The subroutine SETUP is then invoked. This subroutine obtains a position in a random number sequence similar to the previously developed routine of the same name. However, this routine uses the ASCII value of the “secret” code to obtain two different seed numbers as well as the location for the extraction of random numbers from each seed.

The first seed, specified by the variable SEED1, has its value determined by first multiplying the ASCII value of the first and third characters in the “secret” code. This produces a maximum value of 127*127, or 16,129. Because the number of seeds supported by BASIC is considerably higher, you then extract the ASCII value of the fifth character in the “secret” code and continuously divide it by 2 until it is less than or equal to 4. This results in the extension of the seed to a maximum value of 64,516. Next, the value of a second seed is computed. First, the ASCII values of the second and fourth characters in the “secret” code are multiplied by one another. Then divide the ASCII value of the sixth character in the code by 2 until it is less than or equal to 4, and use that value as a multiplier of the product of the ASCII value of the second and fourth characters. Once the two seeds are computed, you are ready to select each seed and position yourself into the random number sequence in each seed prior to extracting the random numbers and placing them into the R1 and R2 arrays.


Previous Table of Contents Next