286-291.html

来自「这个是密码学的经典著作」· HTML 代码 · 共 293 行

HTML
293
字号
<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=286-291//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->

<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="283-286.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="291-294.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<P><B>Listing 6.6</B> The RANDOM2.CPP program listing and its repeated execution using different random seed numbers.</P>
<!-- CODE //-->
<PRE>
/*
random2.cpp
C&#43;&#43; code written by Jonathan Held on April 28, 1998,
using Microsoft Visual C&#43;&#43; version 5.0
*/

#include &lt;iostream.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;string.h&gt;
#include &lt;assert.h&gt;

//function prototypes
bool checkInput(char *&#38;);
char* encipher(char *&#38;);
void formatData(char []);
void getMessage(char *&#38;);
unsigned int getSeed(void);
void gotit(const char [], char *&#38;, const int, int);

const int BUFFER_SIZE = 256;

//----------------------------------------------------------------
//Function: main()
//Parameters: None
//Return Type: int - 0 if program terminated normally
//Purpose: driver that makes calls to various procedures defined
//in random1.cpp
//----------------------------------------------------------------
int main()&#123;
   unsigned int seed;
   char *message, *result;

  while (seed = getSeed()) &#123;
   srand(seed);
   getMessage(message);
   cout &lt;&lt; "\nYou entered: " &lt;&lt; message &lt;&lt; endl;
   result = encipher(message);
   cout &lt;&lt; "Enciphered message is " &lt;&lt; result &lt;&lt; endl &lt;&lt; endl;
   delete message;
 &#125;

 return 0;
&#125;//end main()

//----------------------------------------------------------------
//Function: checkInput()
//Parameters: input - the message the user entered
//Return Type: bool - true if the input string contains an error,
//       false otherwise
//Purpose: Checks the user's keyword for invalid characters.
//----------------------------------------------------------------
bool checkInput(char * &#38;input)
&#123;
 bool error = false;
 int count = strlen(input);

 for (int ix=0; ix&lt;count; ix&#43;&#43;)&#123;

  int char_value = static_cast&lt;int&gt;(*(input&#43;ix));
  //determine if the user did not enter an uppercase character
  if ((char_value &lt; 65) || (char_value &gt; 90))&#123;
      error = true;
      cerr &lt;&lt; "\aYou entered an invalid message!" &lt;&lt; endl &lt;&lt; endl;
       break;
   &#125;
&#125;

if (count == 0)&#123;
  cerr &lt;&lt; "\aYou entered an invalid message!" &lt;&lt; endl &lt;&lt; endl;
  error = true;
&#125;
 return error;
&#125;//end checkInput()


//----------------------------------------------------------------
//Function: encipher()
//Parameters: inp - the user's input that will be enciphered
//Return Type: char * - a pointer to the encrypted string
//Purpose: returns a pointer to the encrypted text
//----------------------------------------------------------------
char* encipher(char *&#38;inp)&#123;

 //yet another, and easier way to represent the plaintext
 //array
 const char plaintext[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
 int index = 0;

 char *encrypted = new char[strlen(inp)&#43;1];
 assert(encrypted);

 for (int ix=0; ix &lt; strlen(inp); ix&#43;&#43;)&#123;
   for (int jx=0; jx &lt;26; jx&#43;&#43;)&#123;
    if (plaintext[jx] == inp[ix])&#123;
          gotit(plaintext, encrypted, jx, index&#43;&#43;);
          break;
      &#125;
   &#125;
&#125;

//don't forget to insert the null terminator
encrypted[strlen(inp)] = '\0';

 return encrypted;
&#125;//end encipher();


//----------------------------------------------------------------
//Function: formatData()
//Parameters: data - the array we want to format
//Return Type: None
//Purpose: Get rid of all spaces in the array.
//----------------------------------------------------------------
void formatData(char data[])&#123;
  for (int mx=0, nx=0; (*(data&#43;nx) != '\0'); nx&#43;&#43;)&#123;
   if (*(data&#43;nx) == ' ')&#123;
      //do nothing - skip over the space in the data
   &#125;
   else &#123;
     *(data&#43;mx&#43;&#43;) = *(data&#43;nx);
   &#125;
 &#125;

 //don't forget to add the null terminator
 *(data&#43;mx) = '\0';

 return;
&#125;//end formatData()


//----------------------------------------------------------------
//Function: getMessage()
//Parameters: input - user's one-line message
//Return Type: None
//Purpose: Gets a one-line message from the user.
//----------------------------------------------------------------
void getMessage(char *&#38;input)&#123;

  char buffer[BUFFER_SIZE] = &#123;'\0'&#125;;
  bool error = false;

  do&#123;
   cout &lt;&lt; "Enter a one-line message (no spaces) in UPPERCASE: ";
   cin.getline(buffer, BUFFER_SIZE, '\n');
   assert(input = new char[strlen(buffer) &#43; 1]);
   strcpy(input, buffer);
   formatData(input);
   error = checkInput(input);

   if (error)
      delete input;

 &#125; while (error);

 return;
&#125;//getMessage()
//----------------------------------------------------------------
//Function: getSeed()
//Parameters: none
//Return Type: unsigned int - the seed the client entered
//Purpose: gets the seed for the random number generator.  If the
//user enters a negative number, it causes the seed to underflow to
//a very high non-negative number.  If the user enters an
//alphanumeric sequence, e.g., "A123", then seed_Number is set to
//zero.
//----------------------------------------------------------------
unsigned getSeed()&#123;

 unsigned int seed_Number;

 cout &lt;&lt; "Enter seed, 0 (or non-digit) to terminate: ";
 cin &gt;&gt; seed_Number;
 cin.ignore();

 return seed_Number;
&#125;//end getSeed()


//----------------------------------------------------------------
//Function: gotit()
//Parameters: p_text - the plaintext array, i.e., characters "A-Z"
//      enc_msg - a pointer to the message that will be encoded
//      loc - the location of each encoded character in the
//      plaintext array
//Return Type: None
//Purpose: encodes the user's input using random numbers
//----------------------------------------------------------------
void gotit(const char p_text[], char *&#38;enc_msg, int loc, int index)&#123;

 int random_num, z;

 do &#123;
  random_num = rand() % 100;
 &#125; while (random_num &gt; 25);

 z = (loc &#43; random_num) % 26;
 enc_msg[index] = p_text[z];

 return;
&#125;//end gotit()
//end file random2.cpp
A:\c&gt;RANDOM2
Enter seed, 0 (or non-digit) to terminate: 5
Enter a one-line message (no spaces) in UPPERCASE: FIREFREDNOW

You entered: FIREFREDNOW
Enciphered message is TDYLWCANCKC

Enter seed, 0 (or non-digit) to terminate: 8
Enter a one-line message (no spaces) in UPPERCASE: FIREFREDNOW

You entered: FIREFREDNOW
Enciphered message is QLEGOKYPGRV

Enter seed, 0 (or non-digit) to terminate: 51
Enter a one-line message (no spaces) in UPPERCASE: FIREFREDNOW

You entered: FIREFREDNOW
Enciphered message is KIYOSXAWQTW

Enter seed, 0 (or non-digit) to terminate:
</PRE>
<!-- END CODE //-->
<P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="283-286.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="291-294.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>


</body></html>

⌨️ 快捷键说明

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