📄 331-335.html
字号:
<html><head><TITLE>Learn Encryption Techniques with BASIC and C++:Using Random Numbers</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=6//-->
<!--PAGES=331-335//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="319-331.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="335-345.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H4 ALIGN="CENTER"><A NAME="Heading26"></A><FONT COLOR="#000077">The DRANDOM3.BAS Program</FONT></H4>
<P>To use consistent naming conventions, I labeled the programs that decipher messages enciphered using RANDOM3.BAS and RANDOM3.CPP as DRANDOM3.BAS and DRANDOM3.CPP. Listing 6.19 contains the statements in the main part of that program that are very similar to RANDOM3.BAS. In fact, the only change in addition to changes in the words in strings located in PRINT statements is the substitution of the subroutines DMSGFILE for MSGFILE and RDCONVERTSTORE for RCONVERTSTORE.
</P>
<P><B>Listing 6.19</B> Statements in the main portion of the DRANDOM3.BAS program.</P>
<!-- CODE //-->
<PRE>
REM Program DRANDOM3.BAS
CLS
DIM PLAINTEXT$(25)
PRINT "DRANDOM3.BAS - A program which deciphers messages"
PRINT "using the built-in BASIC random number generator"
PRINT "that were enciphered using the RANDOM3.BAS program"
PRINT
RTN:
INPUT "Enter your secret code (6 characters maximum): "; CODE$
IF LEN(CODE$) > 6 THEN GOTO RTN
GOSUB INITIALIZE 'initialize plaintext values
GOSUB SETUP 'obtain random seed and position in seed
GOSUB DMSGFILE 'assign I/O files, place message on a file
GOSUB RDCONVERTSTORE 'convert and store plaintext on a file
GOSUB PRTOUT 'print results
STOP
</PRE>
<!-- END CODE //-->
<P>The subroutine DMSGFILE essentially adjusts I/O file references to the opposite of the subroutine MSGFILE. The subroutine RDCONVERTSTORE is very similar to the subroutine RCONVERTSTORE, but converts ciphertext to plaintext instead of plaintext to ciphertext.
</P>
<H4 ALIGN="CENTER"><A NAME="Heading27"></A><FONT COLOR="#000077">The RDCONVERTSTORE Subroutine</FONT></H4>
<P>Listing 6.20 lists the statements in the subroutine RDCONVERTSTORE. In this subroutine a random number is extracted and smoothed to a value between 0 and 25 in the same manner as performed by the subroutine RCONVERTSTORE. Next, each character in the string variable TEXT$ is matched to a character in the string array PLAINTEXT$, with the position of the match assigned to the variable J.
</P>
<P><B>Listing 6.20</B> Statements in the RDCONVERTSTORE subroutine.</P>
<!-- CODE //-->
<PRE>
RDCONVERTSTORE:
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
X = INT(RND * 100) 'get 2 digit integer
X = INT(X / 3.85) 'smooth to 0 to 25
FOR J = 0 TO 25
IF MID$(TEXT$, I, 1) = PLAINTEXT$(J) THEN GOTO
GOTIT
NEXT J
GOTIT: IF J < X THEN J = J + 26
MID$(TEXT$, I, 1) = PLAINTEXT$((J - X) MOD 26)
NEXT I
CLEARTXT: WRITE #2, TEXT$
LOOP
DONE1: CLOSE #2
RETURN
</PRE>
<!-- END CODE //-->
<P>To perform modulo 26 subtraction, the value of J is compared to the value of X. If J is less than X, J is incremented by 26. The value of X is then subtracted from J via modulo 26 arithmetic and the resulting value is used as an index into the string array PLAINTEXT$. The character at the index position represents the plaintext character and it is used to replace the ciphertext character in the variable TEXT$.
</P>
<P>Figure 6.6 illustrates the operation of the program DRANDOM3.BAS. In this example the user simply enters a “secret” code and selects the default filenames and file input as the mechanism for reading the previously created enciphered message. The resulting deciphered message in the lower portion of Figure 6.6 is displayed in groups of five characters since the subroutine PRTOUT was used in the program.</P>
<P><A NAME="Fig6"></A><A HREF="javascript:displayWindow('images/06-06.jpg',463,304 )"><IMG SRC="images/06-06t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/06-06.jpg',463,304)"><FONT COLOR="#000077"><B>Figure 6.6</B></FONT></A> Execution of the DRANDOM3.BAS program.</P>
<P>Readers may be curious as to the effect of entering a different “secret” code during the execution of the program DRANDOM3.BAS. To illustrate the effect, I used the code 9lives instead of 9LIVES. What may appear to be a small code change is a significant change to the mechanism developed for positioning to a place into the BASIC random number generator.
</P>
<P>Figure 6.7 illustrates the effect of using the program DRANDOM3.BAS with an invalid secret code. If you compare the decipherment illustrated in Figure 6.6 to the decipherment illustrated in Figure 6.7, you will note that the use of an incorrect code results in a meaningless deciphered message. Thus, it is as important to keep your secret code secure as it is not to lose your code.</P>
<P><A NAME="Fig7"></A><A HREF="javascript:displayWindow('images/06-07.jpg',463,307 )"><IMG SRC="images/06-07t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/06-07.jpg',463,307)"><FONT COLOR="#000077"><B>Figure 6.7</B></FONT></A> Using DRANDOM3.BAS with an incorrect code.<P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="319-331.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="335-345.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
</body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -