17.

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


Decipherment

Upon receipt of the enciphered message, you must know the relationship between the plaintext and ciphertext to perform our decipherment operation. Although you can use the alphabet relationship previously illustrated in Figure 2.1, that relationship is easier to use for encipherment than decipherment. For decipherment operations, you may wish to reverse the alphabet relationship shown in Figure 2.1, placing the ciphertext alphabet at the top. Then you would look up each letter from the enciphered message in the ciphertext alphabet and replace it with its plaintext alphabet equivalent. This produces the message MEETM EINST LOUIS, which you can rewrite to reflect the correct spacing between words, restoring the original form of the message.

Similar to the coding in the programs SHIFT.BAS and SHIFT.CPP, we can easily test any enciphered message to determine if a simple monoalphabetic substitution process was used. Because only 26 plaintext-ciphertext relationships exist, you can simply displace each character in the enciphered message by a uniform amount and vary that amount 25 times to discover the contents of the plaintext. Thus, a simple monoalphabetic substitution process does not offer a significant level of protection. However, the simple monoalphabetic substitution process represents an excellent starting point for a discussion of more sophisticated substitution processes as well as the development of a series of subroutines and programs that can be used to automate encipherment and decipherment operations. In the remainder of this chapter, I’ll create a series of subroutines and programs that will form the basis for coding examples constructed in the rest of this book.

Automating Operations

The process of converting plaintext to ciphertext and the reverse process can be automated to facilitate monoalphabetic operations. As previously explained in Chapter 1, I’ll use QuickBASIC and C++ to develop the subroutines and programs presented in this chapter and the remainder of this book.

Using an Alphabetic Shift Key

Listing 2.3 contains the program CIPHER1.BAS which can be used to create a ciphertext alphabet based upon the use of a defined alphabetic shift key. In this program, the string array PLAINTEXT$ is used to store the standard sequence of uppercase letters contained in the English alphabet, while the string array CIPHERTEXT$ is used to store the resulting cipher alphabet shifted according to an alphabetic shift key. Note that the subroutine INITIALIZE is used to initialize the plaintext values into the array PLAINTEXT$. In fact, in this and subsequent programs presented in this book, I’ll use common subroutines, when possible, to facilitate the construction of specific programs or program segments developed to perform predefined operations. This will provide you with a “construction set” of routines that can be used to tailor the development of programs to your specific requirements.

Listing 2.3 The CIPHER1.BAS program listing.

 REM PROGRAM CIPHER1.BAS DIM PLAINTEXT$(25), CIPHERTEXT$(25) GOSUB INITIALIZE 1       INPUT "Enter UPPERCASE Alphabetic Shift Key: ", K$        FOR I = 0 TO 25        IF K$ = PLAINTEXT$(I) GOTO 2        NEXT I        PRINT "You must enter a letter from A to Z"        GOTO 1 2       REM Position I represents shift key letter GOSUB FORMCIPHER GOSUB PRTOUT STOP INITIALIZE:      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 FORMCIPHER:      REM Routine to form CIPHERTEXT alphabet based upon defined shift key      J = I + 1      FOR K = 0 TO 25      CIPHERTEXT$(K) = PLAINTEXT$((K + J) MOD 26)      NEXT K RETURN PRTOUT:      REM Print results      FOR I = 0 TO 25      PRINT CIPHERTEXT$(I);      NEXT I      PRINT RETURN END 

Returning to Listing 2.3, after the PLAINTEXT$ array is initialized, the INPUT statement displays the message “Enter UPPERCASE Alphabetic Shift Key” which is read into the string labeled K$. The value of K$ is compared against the alphabet to ensure that an uppercase letter was entered. If this is not the case, an error message is displayed and the program branches back to the statement labeled 1, causing the message “Enter UPPERCASE Alphabetic Shift Key” to be redisplayed. Assuming an uppercase letter is entered, the subroutine FORMCIPHER is invoked. In this subroutine, the position in the plaintext alphabet in which the shift key character equaled a plaintext character plus one is saved by assigning I+1 to the variable J. The reason I+1 is assigned to J is because the alphabet needs to be rotated through the position of the alphabetic shift key. For example, assume C was entered as the alphabetic shift key. Its position in the array PLAINTEXT$ is 2 since the index starts at 0. To shift the alphabet so it starts at the letter D you must add 1 to the position of the shift character.

The FOR-NEXT statement in the subroutine FORMCIPHER assigns 26 characters to the array CIPHERTEXT$ by adding the index K in the loop (which varies in value from 0 to 25) to the value of J, using the mod 26 operator. Thus, when K is 0 and the shift key is C, (K+J) mod 26 is 3, which results in D being assigned to CIPHERTEXT$(0). Similarly, when the shift key is C and K has a value of 25, (K+J) mod 26 has a value of 2, which results in the assignment of C to CIPHERTEXT$(25).

Once the FORMCIPHER subroutine is completed, the main part of the program invokes the subroutine PRTOUT. This subroutine prints the new alphabet based upon the entered alphabetic shift key.

Figure 2.3 illustrates several examples of the execution of CIPHER1.BAS. Note that the use of Z as the alphabetic shift key results in a full rotation of the alphabet.


Figure 2.3  CIPHER1.BAS execution examples.

The C++ version of the CIPHER1.BAS program can be located on the CD-ROM under the C directory. That program has the filename CIPHER1.CPP and is contained in Listing 2.4.


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