📄 128-131.html
字号:
<html><head><TITLE>Learn Encryption Techniques with BASIC and C++:Keyword-Based Monoalphabetic Substitution</TITLE>
<!-- BEGIN HEADER --><META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW"><SCRIPT><!--function displayWindow(url, width, height) { var Win = window.open(url,"displayWindow",'width=' + width +',height=' + height + ',resizable=1,scrollbars=yes');}//--></SCRIPT></HEAD><body bgcolor="ffffff" link="#006666" alink="#006666" vlink="#006666"><P>
<CENTER><B>Learn Encryption Techniques with BASIC and C++</B>
<FONT SIZE="-2">
<BR>
<I>(Publisher: Wordware Publishing, Inc.)</I>
<BR>
Author(s): Gil Held
<BR>
ISBN: 1556225989
<BR>
Publication Date: 10/01/98
</FONT></CENTER>
<P>
<!-- Empty Reference Subhead -->
<!--ISBN=1556225989//-->
<!--TITLE=Learn Encryption Techniques with BASIC and C++//-->
<!--AUTHOR=Gilbert Held//-->
<!--PUBLISHER=Wordware Publishing, Inc.//-->
<!--CHAPTER=3//-->
<!--PAGES=128-131//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="125-128.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="131-134.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<P><FONT SIZE="+1"><B>The DMSGFILE Subroutine</B></FONT></P>
<P>The process of assigning I/O files must be reversed for decipherment. That is, the filename used for the input of the plaintext message in the CIPHER5.BAS program becomes the output file. Similarly, the filename used for the storage of the enciphered message in the CIPHER5.BAS program becomes the input file for decipherment operations. To provide those modifications, you must change the use of the string variables OUTFILE$ and INFILE$ previously used in the subroutine MSGFILE. To change I/O file assignments, simply reverse the use of the previously mentioned string variables.
</P>
<P>Listing 3.6 lists the statements in the new subroutine named DMSGFILE, which contains the reversal of I/O file assignments. If you compare this subroutine to the previously developed subroutine MSGFILE, you should note that the difference between the two is limited to the previously mentioned reversal of I/O filename assignments.</P>
<P><B>Listing 3.6</B> The DMSGFILE subroutine.</P>
<!-- CODE //-->
<PRE>
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 plaintext message,
default=MESSAGE.DAT", OUTFILE$
IF OUTFILE$ = "" THEN OUTFILE$ = "MESSAGE.DAT"
INPUT "Enter filename for enciphered message,
default=CIPHERTX.DAT", INFILE$
IF INFILE$ = "" THEN INFILE$ = "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
</PRE>
<!-- END CODE //-->
<P><FONT SIZE="+1"><B>The DCONVERTSTORE Subroutine</B></FONT></P>
<P>The second subroutine that required modification for deciphering is CONVERTSTORE. As previously discussed, the modified subroutine was renamed DCONVERTSTORE to reflect its modification for decipherment operations.
</P>
<P>Lisitng 3.7 gives the statements contained in the subroutine DCONVERTSTORE. Other than changing two REM statements to reflect the fact that the routine now converts ciphertext to plaintext, the only additional changes relate to search and replacement operations. The IF statement in the nested FOR-NEXT loop now compares each character in the string variable TEXT$ to each character in the CIPHERTEXT$ array instead of the PLAINTEXT$ array. When a match occurs, the character in TEXT$ is replaced by a character in the array PLAINTEXT$ located at the same position in the array where the character in TEXT$ matched the character in the array CIPHERTEXT$. Thus, DCONVERTSTORE simply makes use of the plaintext alphabet stored in the array PLAINTEXT$ and the ciphertext alphabet stored in the array CIPHERTEXT$ in the reverse manner in which they were used in the subroutine CONVERTSTORE.</P>
<P><B>Listing 3.7</B> THE DCONVERTSTORE subroutine.</P>
<!-- CODE //-->
<PRE>
DCONVERTSTORE:
REM Routine to convert and store plaintext 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 ciphertext to plaintext
FOR I = 1 TO MSGLEN
FOR J = 0 TO 25
IF MID$(TEXT$, I, 1) = CIPHERTEXT$(J) THEN GOTO GOTIT
NEXT J
GOTIT: MID$(TEXT$, I, 1) = PLAINTEXT$(J)
NEXT I
CLEARTXT: WRITE #2, TEXT$
LOOP
DONE1: CLOSE #2
RETURN
</PRE>
<!-- END CODE //-->
<P><FONT SIZE="+1"><B>Program Operation</B></FONT></P>
<P>To illustrate the operation of the DCIPHER5.BAS program, you can execute it using the previously enciphered message to the fictional JOHN P. BIDDER as input. Regardless of whether you use the keyboard or specify a file for input of the ciphered messaged, the program produces the same result. Figure 3.3 illustrates the execution of DCIPHER5.BAS in which the keyboard is used to enter the received enciphered message. Because the previously developed subroutine PRTOUT was used to display the resulting deciphered message, that message is displayed in groups of five characters as indicated in the lower portion of Figure 3.3. Although the program converted the ciphertext message back into its plaintext character representation, it did not restore the message to its original format with spaces between words and words rebuilt from two or more five-character groupings when it crossed such groupings. Those functions cannot be performed when the encipherment method does not substitute a character for the space character and results in the human recipient of the deciphered message being responsible for understanding how to correctly read the deciphered message.
</P>
<P><A NAME="Fig3"></A><A HREF="javascript:displayWindow('images/03-03.jpg',488,415 )"><IMG SRC="images/03-03t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/03-03.jpg',488,415)"><FONT COLOR="#000077"><B>Figure 3.3</B></FONT></A> The execution of DCIPHERS.BAS<P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="125-128.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="131-134.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
</body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -