27.

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


You can execute the program from the DOS prompt by typing:

       CIPHER4 <input file><output file><K> 

Here the input and output files can use any extension; however, for consistency it is suggested that the extension .DAT be used. K represents the shift key you want to use, similar to our previous examples. To practice using CIPHER4.CPP, create a text file using a word processor or line editor. For example:

      THIS IS A TEST RUN OF THE PROGRAM      /THIS LINE WILL BE IN CLEARTEXT      THIS LINE WILL NOT 

The C++ version of the program does not require the ending of file input with a backslash (\) character. Once you save your file as ASCII text, at the DOS prompt enter the previously referenced command line. Then, you can list the contents of the file OUTPUT.DAT by typing TYPE OUTPUT.DAT or by the use of a word processor. Due to the extensive documentation contained in the program we will not duplicate its contents by examining its modules.

Although simple monoalphabetic substitution produces ciphertext that offers little protection to a message, it provides you with a foundation for creating a program with many subroutines. Prior to moving on to more sophisticated methods of encipherment, I’ll conclude this chapter by automating the decipherment process. Although you could create several new subroutines and incorporate them into our encipherment program through appropriate logic to form one program that both enciphers and deciphers messages, it may be more useful to illustrate the decipherment process by creating a separate program. That program will be named DCIPHER4.BAS to correspond to CIPHER4.BAS, because they both use the same simple monoalphabetic substitution process to encipher and decipher messages.

The DCIPHER4.BAS Program

The process of deciphering a message is the inverse of the encipherment process. For a simple monoalphabetic substitution process, you would first initialize the plaintext and ciphertext alphabets using the previously developed INITIALIZE and FORMCIPHER subroutines. Once the two alphabets are constructed, you would use a new subroutine to decipher the ciphertext characters by comparing each ciphertext character in the message to the ciphertext alphabet. When a match occurs, use the position of the character in the ciphertext alphabet as a pointer to extract a corresponding character from the plaintext alphabet. In addition to creating this new subroutine which is named DECIPHER, you must modify the previously created MSGFILE and PRTOUT subroutines. As you will recall, MSGFILE is used to assign I/O files and accept keyboard or file input, while PRTOUT is used to print the enciphered message. To reflect the new functions performed by those subroutines their names will be changed to DMSGFILE and DPRTOUT, respectively.

The DMSGFILE Subroutine

Listing 2.16 contains the statements in the subroutine DMSGFILE which assigns I/O files and accepts the entry of an enciphered message from either the keyboard or from a file. Because enciphered text was grouped into five-character groups with spaces between groups, the DMSGFILE subroutine is similar to the MSGFILE subroutine in that it removes spaces between groups, while MSGFILE removed spaces between words. Also note that the variable INFILE$ is used to store the name of the file containing the enciphered message, while the variable OUTFILE$ is used to store the resulting plaintext deciphered message. Thus, DMSGFILE assigns files opposite to the manner in which I/O files are assigned by the subroutine MSGFILE.

Listing 2.16 The DMSGFILE subroutine.

 DMSGFILE:    REM Routine to assign I/O files and accept keyboard or file input    REM and remove spaces between words        INPUT "Enter filename to store enciphered message,                         default=CIPHERTX.DAT", INFILE$        IF INFILE$ = "" THEN INFILE$ = "CIPHERTX.DAT"        INPUT "Enter filename to store deciphered message,                                  default=MESSAGE.DAT", OUTFILE$        IF OUTFILE$ = "" THEN OUTFILE$ = "MESSAGE.DAT"        INPUT "Select keyboard (k) or file (f) ciphertext message                         input: ", IN$        IF IN$ = "F" OR IN$ = "f" THEN RETURN        OPEN INFILE$ FOR OUTPUT AS #1    REM Routine to place enciphered message on a file removing spaces between groups        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 

Similar to the manner in which MSGFILE was created, DMSGFILE uses the forward slash and backslash characters to indicate to the program that it should bypass conversion (/) or the end of the message is reached (\). Thus, the second part of DMSGFILE, with the exception of wording in PRINT statements, functions in the same manner as the subroutine MSGFILE used in the CIPHER4.BAS program.


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