23.

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


Processing Multiple-Line Messages

Since you want the ability to read a message containing multiple lines of text, let’s modify our previously constructed program by the addition of several subroutines. One subroutine will permit a message to be input from the keyboard or a previously created file. To provide you with the ability to enter text more naturally, this subroutine will also permit you to enter words with spaces between words, since the program will become responsible for their removal. Other subroutines will convert the plaintext message into ciphertext and position the enciphered text into groups of five characters for printing, terminating the last group of characters with an appropriate number of X’s to fill any terminating group that has less than five characters.

The MSGFILE Subroutine

Listing 2.10 shows the contents of the subroutine labeled MSGFILE. This subroutine performs three major functions. First, it permits the user to assign filenames for the file used to store a plaintext and resulting ciphertext message or to select the default filenames MESSAGE.DAT for storing the plaintext message and CIPHERTX.DAT for storing the enciphered message. The second function performed by MSGFILE is to remove spaces between words. The third function performed by this subroutine is to store in a file the resulting plaintext message in which any spaces between words were removed.

Listing 2.10 The MSGFILE subroutine.

 MSGFILE:    REM Routine to assign I/O files and accept keyboard or file input    REM and remove spaces between words        INPUT "Enter filename to store plaintext message,                         default=MESSAGE.DAT", INFILE$        IF INFILE$ = "" THEN INFILE$ = "MESSAGE.DAT"        INPUT "Enter filename to store enciphered message,                          default=CIPHERTX.DAT", OUTFILE$        IF OUTFILE$ = "" THEN OUTFILE$ = "CIPHERTX.DAT"        INPUT "Select keyboard (k) or file (f) message input: ", IN$        IF IN$ = "F" OR IN$ = "f" THEN RETURN        OPEN INFILE$ FOR OUTPUT AS #1    REM Routine to place message on a file removing spaces between words        PRINT "Enter your message - place a / at the beginning of                          each line"        PRINT "that should remain in plaintext and a \ on a                          separate line"        PRINT "to indicate the end of the enciphered message"        PRINT AGN:     LINE INPUT TEXT$          IF MID$(TEXT$, 1, 1) = "/" THEN GOTO XT          NTEXT$ = ""          FOR I = 1 TO LEN(TEXT$)          NTEXT$ = NTEXT$ + LTRIM$(MID$(TEXT$, I, 1))          NEXT I          WRITE #1, NTEXT$          IF MID$(TEXT$, 1, 1) = "\" GOTO DONE          GOTO AGN XT:       WRITE #1, TEXT$          GOTO AGN DONE:      CLOSE #1 RETURN 

The first portion of the subroutine MSGFILE prompts the user to enter filenames for storing a plaintext message and its resulting enciphered message. If the user simply presses the Return key, the default filenames MESSAGE.DAT and CIPHERTX.DAT are used. Otherwise, the subroutine prompts the user to enter the appropriate plaintext and ciphertext filenames.

The second major routine in MSGFILE performs the actual placement of keyboard-entered plaintext on a file and removes spaces between words. To facilitate the transmission of enciphered messages, this portion of the subroutine permits users to prefix each heading message line with a forward slash (/) character if they do not want that message line to be enciphered. This allows a header portion of a message to be ignored during the encipherment process. The transmission of the enciphered text with a cleartext header facilitates its distribution to the appropriate recipient. To indicate the end of the message, a backslash (\) character must be entered by itself on a separate line.

The FOR-NEXT loop in the second half of the subroutine MSGFILE uses the LTRIM$ statement to remove spaces between words. This FOR-NEXT loop is invoked only when the beginning of a line does not contain a forward slash character. Since LTRIM$ removes leading spaces in a string, its use in the FOR-NEXT loop places a line of plaintext in which all spaces have been removed into the string variable NTEXT$.

The CONVERTSTORE Subroutine

The following subroutine reads the contents of the previously stored messages a line at a time, converts each message into its enciphered text, and stores the enciphered text on a line-by-line basis in the file whose name is assigned to the string variable OUTFILE$. Listing 2.11 shows the statements in the subroutine CONVERTSTORE which perform the previously mentioned functions.

Listing 2.11 The CONVERTSTORE subroutine.

 CONVERTSTORE:    REM Routine to convert and store ciphertext on a file    OPEN INFILE$ FOR INPUT AS #1    OPEN OUTFILE$ FOR OUTPUT AS #2    DO UNTIL EOF(1)        INPUT #1, TEXT$        MSGLEN = LEN(TEXT$)        IF MID$(TEXT$, 1, 1) = "/" THEN GOTO CLEARTXT        IF MID$(TEXT$, 1, 1) = "\" THEN GOTO DONE1        REM Convert plaintext to ciphertext           FOR I = 1 TO MSGLEN           FOR J = 0 TO 25           IF MID$(TEXT$, I, 1) = PLAINTEXT$(J) THEN GOTO GOTIT           NEXT J GOTIT:      MID$(TEXT$, I, 1) = CIPHERTEXT$(J)           NEXT I CLEARTXT:     WRITE #2, TEXT$    LOOP DONE1:    CLOSE #2 RETURN 

After opening the file whose name was assigned to the string variable INFILE$ for input and the file whose name was assigned to OUTFILE$ for output, the subroutine uses a DO UNTIL loop to read each message line from the file containing the plaintext message. Within the DO UNTIL loop, each line is processed based upon the first character in the line.

If the first character in a line is a forward slash, a branch to the label CLEARTXT occurs and the conversion of plaintext to enciphered text is bypassed. If the first character in a line is a backslash, the end of the previously stored message has been reached and a branch to the label DONE1 occurs. This terminates the loop, closes the previously opened file #2, and causes an exit from the subroutine to occur.

If the first character in each line is neither a forward slash nor a backslash character, the subroutine converts each plaintext character in the line into its equivalent ciphertext character. This is accomplished through the use of a pair of nested FOR-NEXT loops. The outer loop cycles through each of the characters in a line retrieved from the file containing the plaintext. The inner loop compares on a character-by-character basis the plaintext character to each character in the string array PLAINTEXT$. When a match occurs, a branch to the label GOTIT occurs. At that label, the plaintext character is replaced by the ciphertext character whose position in the array CIPHERTEXT$ is the same as the position of the plaintext character in the array PLAINTEXT$.


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