19.

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


Converting to Ciphertext

We can build upon the programs CIPHER1.BAS and CIPHER1.CPP to expand their capability so that they can convert plaintext to ciphertext. Listing 2.5 contains the program listing for this new program which we named CIPHER2.BAS. This elementary program enciphers a one-line message based upon an alphabetic shift key.

Listing 2.5 The CIPHER2.BAS program listing.

 REM PROGRAM CIPHER2.BAS     PRINT "This program enciphers a one-line message based upon an alphabetic shift key" DIM PLAINTEXT$(26), CIPHERTEXT$(26) 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     PRINT "Enter your message in UPPERCASE:"     INPUT TEXT$     MSGLEN = LEN(TEXT$) GOSUB MSGENCIPHER 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 MSGENCIPHER:     REM Convert plaintext to ciphertext     FOR I = 1 TO MSGLEN     FOR J = 0 TO 25     IF MID$(TEXT$, I, 1) = PLAINTEXT$(J) THEN GOTO 5     NEXT J 5   MID$(TEXT$, I, 1) = CIPHERTEXT$(J)     NEXT I RETURN PRTOUT:     REM Print results     PRINT "Resulting enciphered message is:"     PRINT TEXT$ RETURN END 

In examining Listing 2.5, let us focus our attention upon the new additions, specifically the lines from PRINT “Enter your message in UPPERCASE” to STOP, and the subroutine MSGENCIPHER.

The statements following the referenced PRINT statement simply accept a one-line message and store it in the string variable TEXT$, obtain the length of the message through the use of the LEN statement and store the length of the message in the variable MSGLEN, invoke the subroutine MSGENCIPHER, and output or print the results obtained from an encipherment of the message by invoking the subroutine PRTOUT. Thus, the key to encipherment once the ciphertext is developed through the use of an alphabetic shift key is the subroutine MSGENCIPHER, which we will now discuss.

The MSGENCIPHER Subroutine

The subroutine MSGENCIPHER contains a nested pair of FOR-NEXT loops in which I is varied in the outer loop from 1 to the last character of the message denoted by MSGLEN. In the inner loop, J is varied from 0 to 25 and is used to match each plaintext character in the message to its equivalent PLAINTEXT array character position through the use of the MID function. For example, when I has a value of 1, MID$(TEXT$, I, 1) extracts the first character from the string TEXT$ which, when used in the IF statement, is compared to the value of the string variable PLAINTEXT$(J). As J is varied from 0 to 25, a value is reached in which the extracted character from TEXT$ equals a character in the PLAINTEXT$ string array and a branch to the statement label 5 occurs. At that location, the character in the string TEXT$ is replaced by the character in the CIPHERTEXT$ array located at position J. Thus, each character in the plaintext message will be replaced by a character from the shifted alphabet whose position coincides with the unshifted alphabet plaintext character.

Figure 2.4 illustrates the execution of the program CIPHER2.BAS using B as the alphabetic shift key to form the cipher alphabet. Note that at this point in time you have to eliminate spaces between words as there is no space character in our plaintext or ciphertext alphabet. Later in this book, I’ll develop a routine which will remove spaces between words, enabling a user to enter a plaintext message in a more natural manner.


Figure 2.4  A CIPHER2.BAS execution example.


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