⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 084-088.html

📁 这个是密码学的经典著作
💻 HTML
字号:
<html><head><TITLE>Learn Encryption Techniques with BASIC and C++:Monoalphabetic Substitution Concepts</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=2//-->
<!--PAGES=084-088//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->

<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="081-084.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="088-094.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H4 ALIGN="CENTER"><A NAME="Heading26"></A><FONT COLOR="#000077">The DECIPHER Subroutine</FONT></H4>
<P>The actual deciphering process is performed by the subroutine DECIPHER whose contents are listed in Listing 2.17. In examining the statements in the subroutine DECIPHER, readers should recognize its close resemblance to the subroutine CONVERTSTORE in the CIPHER4.BAS program. The only difference between the two subroutines is the search mechanisms bounded by the nested FOR-NEXT loops. In the DECIPHER subroutine, each character in the enciphered message is compared against the ciphertext alphabet. When a match occurs, the position of the character in the ciphertext alphabet is used as a pointer to extract a character from the plaintext alphabet. Also note that the first character of each line of data retrieved from the ciphertext file is examined to determine if a forward slash or backslash character is in the first position of the line. Similar to the encipherment process, the decipherment process uses those characters to bypass the decipherment process (/) or to terminate the decipherment process (\).
</P>
<P><B>Listing 2.17</B> The DECIPHER subroutine.</P>
<!-- CODE //-->
<PRE>
DECIPHER:
   REM Routine to decipher 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 //-->
<H4 ALIGN="CENTER"><A NAME="Heading27"></A><FONT COLOR="#000077">The DPRTOUT Subroutine</FONT></H4>
<P>The last subroutine required for decipherment is appropriately labeled DPRTOUT to reflect the fact that it prints the results of the decipherment process. Unlike the subroutine PRTOUT which limited the number of characters to 25 per line and required extensive logic to operate on strings and invoke the GROUPBY5 subroutine, DPRTOUT is very simple. DPRTOUT simply reads each line in the file containing the deciphered message, eliminates any forward slash character used as a line prefix, and prints each line as is. Listing 2.18 lists the contents of that subroutine.
</P>
<P><B>Listing 2.18</B> The DPRTOUT subroutine.</P>
<!-- CODE //-->
<PRE>
DPRTOUT: REM Subroutine to print results
   INPUT "Press Return key to display resulting enciphered
message", p$
   CLS
   PRINT "Resulting deciphered 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) &lt;&gt; "/" THEN PRINT TEXT$
   LOOP
RETURN
</PRE>
<!-- END CODE //-->
<P>To illustrate the composition of the resulting decipherment program, Listing 2.19 lists the main portion of the program DCIPHER4.BAS. You should note that two of the five subroutines, INITIALIZE and FORMCIPHER, are exactly the same as those subroutines used in CIPHER4.BAS. The three remaining subroutines, DMSGFILE, DECIPHER, and DPRTOUT, are modified versions of the subroutines MSGFILE, CONVERTSTORE, and PRTOUT. The entire decipherment program which deciphers a message enciphered using a simple monoalphabetic substitution process is stored on the file DCIPHER4.BAS on the CD-ROM under the BASIC directory. The executable program in that directory is named DCIPHER4.EXE.
</P>
<P><B>Listing 2.19</B> The main portion of the DCIPHER4.BAS program.</P>
<!-- CODE //-->
<PRE>
REM PROGRAM DCIPHER4.BAS
DIM PLAINTEXT$(26), CIPHERTEXT$(26)
CLS
GOSUB INITIALIZE
   PRINT "DCIPHER4.BAS PROGRAM deciphers a message using a simple"
   PRINT "monoalphabetic substitution process."
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         'create cipher alphabet
GOSUB DMSGFILE         'assign I/O files, place message on a file
GOSUB DECIPHER         'convert enciphered message to plaintext
GOSUB DPRTOUT          'print results
STOP
</PRE>
<!-- END CODE //-->
<P><FONT SIZE="+1"><B>Program Execution</B></FONT></P>
<P>To illustrate the operation of the DCIPHER4.BAS program, you can execute the program with input from the keyboard and from a file. To facilitate a comparison of the deciphered message to the original plaintext message, you can use the enciphered message contained at the bottom of Figure 2.6 as input to DCIPHER4.BAS.
</P>
<P>Figure 2.8 illustrates the operation of DCIPHER4.BAS in which keyboard input was selected. In this example, you must use the keyboard to enter the ciphertext message in the same manner as it was displayed as a result of the execution of CIPHER4.BAS. That is, you must prefix each cleartext line with a forward slash, enter each line using the five-character groups contained on the line, and terminate the enciphered message with a backslash character on a separate line.</P>
<P><A NAME="Fig8"></A><A HREF="javascript:displayWindow('images/02-08.jpg',448,493 )"><IMG SRC="images/02-08t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/02-08.jpg',448,493)"><FONT COLOR="#000077"><B>Figure 2.8</B></FONT></A>&nbsp;&nbsp;The execution of DCIPHER4.BAS using keyboard input.</P>
<P>The execution of DCIPHER4.BAS using keyboard input will display up to 25 characters per line because input was limited to five groups of five characters per line. In comparison, the use of file input when DCIPHER4.BAS is executed deciphers each full line contained on the file whose name was assigned to the variable OUTFILE$ (Figure 2.9). The use of file input may provide a more natural conversion to plaintext since words do not have to be split among two lines if they do not fit into a five-character group terminating a line.
</P>
<P><A NAME="Fig9"></A><A HREF="javascript:displayWindow('images/02-09.jpg',491,269 )"><IMG SRC="images/02-09t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/02-09.jpg',491,269)"><FONT COLOR="#000077"><B>Figure 2.9</B></FONT></A>&nbsp;&nbsp;The execution of DCIPHER4.BAS using file input.<P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="081-084.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="088-094.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>


</body></html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -