📄 280-283.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=280-283//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="278-280.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="283-286.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H4 ALIGN="CENTER"><A NAME="Heading9"></A><FONT COLOR="#000077">The RANDOM2.BAS Program</FONT></H4>
<P>Listing 6.4 contains the program listing and sample execution of the program RANDOM2.BAS. This simple program was developed to illustrate how you can use random numbers to encipher messages. In this example, our plaintext alphabet is restricted to the 26 uppercase letters. This alphabet is initialized by the INITIALIZE subroutine. Next, the program uses a user-entered number to seed the random number generator and accepts a one-line message read into the string variable TEXT$. The characters in that message are then placed into the string array KEY$.
</P>
<P><B>Listing 6.4</B> The RANDOM2.BAS program listing and its repeated execution using different random seed numbers and the same plaintext message.</P>
<!-- CODE //-->
<PRE>
REM Program RANDOM2.BAS
REM Sample program to demonstrate use of random numbers for enciphering
DIM KEY$(80), PLAINTEXT$(26)
CLS
PRINT "Program RANDOM2.BAS to demonstrate use of random numbers and"
PRINT "random number seed in enciphering operations"
GOSUB INITIALIZE
START: PRINT
INPUT "Enter a random seed number, 0 to terminate ", Y
IF Y = 0 THEN STOP
RANDOMIZE (Y)
INPUT "Enter a one line message in UPPERCASE: ", TEXT$
FOR I = 1 TO LEN(TEXT$)
KEY$(I) = MID$(TEXT$, I, 1)
NEXT I
REM Encipher
PRINT "Enciphered message is : ";
FOR I = 1 TO LEN(TEXT$)
FOR J = 0 TO 25
IF PLAINTEXT$(J) = KEY$(I) GOTO GOTIT 'locate character position
NEXT J ' in plaintext array
GOTIT: X = INT(RND * 100) 'get random number
IF X > 25 THEN GOTO GOTIT ' less than 25
Z = (J + X) MOD 26 'use MOD 26 addition to add
PRINT PLAINTEXT$(Z); 'display enciphered character
NEXT I
GOTO START
INITIALIZE:
RESTORE
REM Initialize plaintext values
FOR I = 0 TO 25
READ PLAINTEXT$(I)
NEXT I
DATA "A","B","C","D","E","F","G","H","I","J","K","L","M","N"
DATA "O","P","Q","R","S","T","U","V","W","X","Y","Z"
RETURN
Program RANDOM2.BAS to demonstrate use of random numbers and
random number seed in enciphering operations
Enter a random seed number, 0 to terminate 5
Enter a one-line message in UPPERCASE: FIREFREDNOW
Enciphered message is: CPKWBXVSORN
Enter a random seed number, 0 to terminate 8
Enter a one-line message in UPPERCASE: FIREFREDNOW
Enciphered message is: RDMXNQVCJXE
Enter a random seed number, 0 to terminate 51
Enter a one-line message in UPPERCASE: FIREFREDNOW
Enciphered message is: ZXDDMCBNHGZ
Enter a random seed number, 0 to terminate 0
</PRE>
<!-- END CODE //-->
<P>The routine labeled ENCIPHER matches each character in the message that was placed in the string array KEY$ to a plaintext character. When a match is found, a branch to the label GOTIT occurs, and a two-digit random number is obtained. Next, the program discards the random number if it exceeded 25 and obtains a new random number, forcing <I>x</I> to be between 0 and 25.</P>
<P>Once an acceptable value of <I>x</I> is obtained, the index value of J, which represents the plaintext character location in the alphabet, is added via modulo 26 addition to the random number. By restricting the largest random number to 25 and the plaintext character position to 26, the largest number produced by adding the two becomes 51. Performing modulo 26 addition prevents a resulting number occurring from different additions, such as would be the case if a random number added to the index resulted in a value of 52 or more. For example, 1 mod 26 is 1, but so is 53 mod 26 and 79 mod 26. By limiting the maximum value of the sum of the index and random number to 51, you ensure you can correctly decipher a message that was enciphered using the technique previously described.</P>
<H4 ALIGN="CENTER"><A NAME="Heading10"></A><FONT COLOR="#000077">The RANDOM1.CPP Program</FONT></H4>
<P>Since the purpose of this book is to acquaint you with both BASIC and C++ encryption and decryption coding methods, let’s turn our attention to two C++ random number programs. The first program, RANDOM1.CPP, prompts you for a seed and uses that seed to generate a sequence of random numbers between 1 and 100, only displaying those numbers that are less than 25.
</P>
<P>The top portion of Listing 6.5 contains the statement in the program RANDOM1.CPP, while the lower portion shows three examples of its execution. Although this program was written to attempt to duplicate the program RANDOM1.BAS, the lack of a C++ RANDOMIZE statement resulted in the development of the function getSeed which is used to return a user-entered seed value. That value is used with the C++ function SRAND to generate 75 random numbers, with only those numbers whose values do not exceed 25 being displayed.</P>
<P>If you compare the random numbers shown at the bottom of Listing 6.5 to those produced by the execution of the BASIC version of the program, you will note different results even though the same seed values were used. These differences result from the fact that most programming languages use a different algorithm to generate random numbers.</P><P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="278-280.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="283-286.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
</body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -