64.

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


Modulo 26 Arithmetic

To better understand the routine necessary to decipher the previously enciphered series of messages that used a random number-based enciphering program requires a review of modulo 26 addition and modulo 26 subtraction. Suppose the position values of the first four characters in a plaintext message with respect to their location in the sequential plaintext alphabet are 6, 2, 4, and 18, while a randomly generated number sequence based upon a defined seed was 12, 14, 22, and 17.

The top of Figure 6.1 illustrates the enciphering process performed through the use of modulo 26 addition. Once the enciphered characters reach their destination, the same sequence of random numbers is used to reconstruct the original characters. However, this time modulo 26 subtraction is performed as illustrated in the bottom portion of Figure 6.1. When performing modulo 26 subtraction, if the numerator is smaller than the subtrahend you “borrow” 1, which in effect has a decimal value of 26, prior to performing the required subtraction. Thus, 9-17 becomes (26+9)-17, or 18, when modulo 26 subtraction is performed.


Figure 6.1  Enciphering and deciphering using modulo 26 addition and subtraction.

Now that you have an appreciation of the modulo subtraction process, let’s focus our attention upon a small program developed to decipher a previously enciphered one-line message based upon a specified seed for the BASIC random number generator.

The DRANDOM2.BAS Program

Listing 6.7 contains the statements in the program DRANDOM2.BAS and three examples of its execution. In keeping with the file naming conventions previously established, I labeled the program DRANDOM2.BAS to indicate that it deciphers a message previously enciphered using the program RANDOM2.BAS.

Listing 6.7 The DRANDOM2.BAS program listing and its repeated execution using different random seed numbers and different ciphertext messages to re-create a common plaintext message.

 REM Program DRANDOM2.BAS DIM KEY$(80), PLAINTEXT$(26) CLS    PRINT "Program DRANDOM2.BAS to demonstrate use of random numbers                 and"    PRINT "random number seed in deciphering operations" GOSUB INITIALIZE START: PRINT    INPUT "Enter a random seed number, 0 to terminate ", Y    IF Y = 0 THEN STOP    RANDOMIZE (Y)    INPUT "Enter a one line message in UPPERCASE: ", TEXT$    FOR I = 1 TO LEN(TEXT$)    KEY$(I) = MID$(TEXT$, I, 1)    NEXT I REM Decipher    PRINT "Deciphered message is               : ";    FOR I = 1 TO LEN(TEXT$)    FOR J = 0 TO 25    IF PLAINTEXT$(J) = KEY$(I) GOTO GOTIT    NEXT J GOTIT: X = INT(RND * 100)    IF X > 25 THEN GOTO GOTIT    IF J < X THEN J = J + 26    Z = J - X    PRINT PLAINTEXT$(Z);    NEXT I    GOTO START INITIALIZE:    RESTORE    REM Initialize plaintext values    FOR I = 0 TO 25    READ PLAINTEXT$(I)    NEXT I    DATA "A","B","C","D","E","F","G","H","I","J","K","L","M","N"    DATA "O","P","Q","R","S","T","U","V","W","X","Y","Z" RETURN Program DRANDOM2.BAS to demonstrate use of random numbers and random number seed in deciphering operations Enter a random seed number, 0 to terminate 5 Enter a one line message in UPPERCASE: CPKWBXVSORN Deciphered message is          : FIREFREDNOW Enter a random seed number, 0 to terminate 8 Enter a one line message in UPPERCASE: RDMXNQVCJXE Deciphered message is          : FIREFREDNOW Enter a random seed number, 0 to terminate 51 Enter a one line message in UPPERCASE: ZXDDMCBNHGZ Deciphered message is          : FIREFREDNOW Enter a random seed number, 0 to terminate 0 

After entering the random number generator’s seed number and the one-line enciphered message, the decipher routine matches each character in the message against the plaintext alphabet stored in the array PLAINTEXT$. Once a match occurs, you have located the correct position value or location of the ciphertext character in the plaintext character in the plaintext alphabet—a value assigned to the variable J. After the value of J is obtained, a branch to the label GOTIT occurs and a random number between 0 and 25 is extracted. Because you must perform modulo 26 subtraction to reconstruct the plaintext character, compare the value of J to the value of x and increase J by 26 if the value of J is less than x. Then we subtract the value of x from the value of J, in effect performing modulo 26 subtraction. As indicated in the lower portion of Listing 6.7, this process enables you to reconstruct the plaintext message.

A comparison of the execution of RANDOM2.BAS and DRANDOM2.BAS contained in the lower portions of Listings 6.4 and 6.7 verifies the correctness of the deciphering process. That is, plaintext messages enciphered using a defined random number seed value in Listing 6.4 are entered as ciphertext messages in Listing 6.7 with the same seed number. This results in the correct decipherment of each message into its original plaintext contents.


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