24.

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


The PRTOUT Subroutine

Now that subroutines were developed to place a message on a file and process and convert the message to enciphered text which is stored on another file, I am ready to print the results. To do this I will develop a subroutine named PRTOUT which will process the file containing the enciphered text so that no more than 25 characters or five groups of five characters are displayed on a line. We have limited the printout of enciphered text to 25 characters per line to facilitate manual operations for readers who wish to use the resulting display of the enciphered message in place of the file containing the enciphered text for sending a message.

Listing 2.12 shows the contents of the subroutine PRTOUT. After using an INPUT statement to display the message informing a user to press the Return key, the subroutine clears the display and sets the string variable TEMP$ to a null value. Next, the message “Resulting enciphered message is:” is displayed and the file assigned to the variable OUTFILE$ is opened for input. Note that the enciphered text was previously stored on the file assigned to the variable OUTFILE$; thus, this subroutine will process the enciphered message.

Listing 2.12 The PRTOUT subroutine.

 PRTOUT: REM Subroutine to print results     INPUT "Press Return key to display resulting enciphered message", p$     CLS: TEMP$ = ""     PRINT "Resulting enciphered message is:"     OPEN OUTFILE$ FOR INPUT AS #2     DO UNTIL EOF(2)         INPUT #2, TEXT$         IF MID$(TEXT$, 1, 1) = "/" THEN PRINT RIGHT$(TEXT$,                            LEN(TEXT$) - 1)         IF MID$(TEXT$, 1, 1) = "/" THEN GOTO NOGROUP         IF TEMP$ = "" GOTO BLANK         'blank string         TEMP$ = TEMP$ + TEXT$            'concatenate         TEXT$ = TEMP$         TEMP$ = "" BLANK:     MSGLEN = LEN(TEXT$)         IF MSGLEN >= 25 THEN GOTO BIG         TEMP$ = TEXT$         GOTO NOGROUP               'less than 25 characters BIG:     FOR I = 26 TO MSGLEN          'place remainder of string                                                     'into temp         TEMP$ = TEMP$ + MID$(TEXT$, I, 1)         NEXT I         MSGLEN = 25         GOSUB GROUPBY5:                  'print 25 characters NOGROUP: LOOP         TEXT$ = TEMP$                    'print remainder         MSGLEN = LEN(TEXT$)         IF MSGLEN <= 25 THEN GOSUB GROUPBY5         IF MSGLEN <= 25 THEN RETURN      'done printing      WHILE MSGLEN > 25         TEMP$ = LEFT$(TEXT$, 25)         'get first 25 characters                                                          'in string         TEMPR$ = RIGHT$(TEXT$, MSGLEN - 25)         TEXT$ = TEMP$         MSGLEN = LEN(TEXT$)         GOSUB GROUPBY5         TEXT$ = TEMPR$         MSGLEN = LEN(TEXT$)      WEND         TEXT$ = TEMPR$         MSGLEN = LEN(TEXT$)         GOSUB GROUPBY5 RETURN 

The DO UNTIL loop processes the lines on the enciphered file. First, each line is read into the string variable TEXT$. The first two IF statements check for the presence of a forward slash. If a forward slash occurs in the first character position in a line, the first IF statement causes the line to be printed without the slash. Since a forward slash indicates the line should be treated as cleartext, the second IF statement simply skips the remaining statements in the loop by branching to the label NOGROUP.

The first time PRTOUT is invoked, the string TEMP$ is set to the null string. Thus, the first time through the DO UNTIL loop, the third IF statement causes a branch to the label BLANK to occur. At that label the length of the retrieved line of ciphertext is determined through the use of the LEN function. If the length of the line equals or exceeds 25 characters, a branch to the label BIG occurs. If the length of the line did not exceed 25 characters, there were not enough characters for printing 25 characters on one line. At this point, the characters assigned to the variable TEXT$ are assigned to the variable TEMP$ and a branch to the label NOGROUP occurs to terminate one cycle through the loop.

When the value of TEMP$ is not a null string, the contents of the next line of ciphertext are concatenated, or joined, to the contents of TEMP$. Then, the contents of TEMP$ are assigned to TEXT$ and TEMP$ is reset to a null string. In this manner, you can continue to cycle through the contents of the enciphered file until you either reach the end of the file or the length of the variable TEXT$ equals or exceeds 25 characters.

When the length of TEXT$ equals or exceeds 25 characters, a branch to the label BIG occurs. At that location, the FOR-NEXT loop places the remainder of the contents of TEXT$ that exceed 25 characters into the variable TEMP$. Next, the message length variable MSGLEN is set to 25 and the subroutine GROUPBY5 is called. That subroutine, which was previously discussed and which will be discussed in more detail in the next section in this chapter, actually prints the characters in groups of fives and assigns X’s to fill the last group, if that group contains less than five characters.

When all lines in the file assigned to OUTFILE$ are read, the DO UNTIL loop is exited. At this point, you need to print the remaining enciphered text stored in the variable TEMP$. To do this, assign the contents of TEMP$ to TEXT$ and obtain its length. If the length of TEXT$ is less than or equal to 25 characters, invoke the subroutine GROUPBY5 to print the last 25 or less characters in the enciphered message and then exit the subroutine. If there are more than 25 characters in TEXT$, use a WHILE-WEND loop to print the contents of TEXT$. Assign the first 25 characters in TEXT$ to TEMP$ through the use of the LEFT$ function. Next, the remaining characters in the string TEXT$ are assigned to the variable TEMPR$. After the contents of TEMP$ are assigned to TEXT$, the message length is determined and the subroutine GROUPBY5 is invoked. The contents of TEMPR$ are assigned to TEXT$, the length of TEXT$ is determined, and the subroutine GROUPBY5 is again invoked, this time printing the remaining portion of characters that exceeded 25 in length. When less than 25 characters remain in TEMPR$, the WHILE-WEND loop terminates. At that point the contents of TEMPR$ are assigned to TEXT$, its length is determined, and the subroutine GROUPBY5 is invoked for the last time.


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