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

📄 266-272.html

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

<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="262-266.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="../ch06/273-276.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H4 ALIGN="LEFT"><A NAME="Heading21"></A><FONT COLOR="#000077">The DPOLY2.CPP Program</FONT></H4>
<P>In concluding this chapter, dual coverage of BASIC and C&#43;&#43; programs will be continued by turning your attention to the C&#43;&#43; version of DPOLY2. That program, which has the filename DPOLY2.CPP, is contained on the CD-ROM in the C directory.
</P>
<P>Listing 5.10 contains a listing of the statements in the header and the main function of DPOLY2.CPP. In examining the listing shown in Listing 5.10 you will note that many functions used in the program POLY2.CPP are reused, such as checkInput, createStream, and createTable. Thus, in this section I will focus your attention on the key difference between the two programs. That difference is primarily in the inclusion of the functions decipher and getFileToDecipher. Listing 5.11 lists the statements in those two functions. Since each function is explicitly documented through the use of comments, I will leave it to you to review the statements for each function.</P>
<P><B>Listing 5.10</B> Header and function main statements in DPOLY2.CPP.</P>
<!-- CODE //-->
<PRE>
/*DPOLY2.CPP
C&#43;&#43; code written by Jonathan Held
Using Microsoft&#146;s Visual C&#43;&#43; Version 5.0
*/

//standard includes
#include&lt;iostream.h&gt;
#include&lt;assert.h&gt;
#include&lt;string.h&gt;
#include&lt;ctype.h&gt;
#include&lt;fstream.h&gt;
#include&lt;stdlib.h&gt;

//constants we will use
const int FIVE = 5, TWENTYFIVE = 25, TWENTYSIX = 26,
      TWENTYSEVEN = 27, SIXTYFIVE = 65, NINETY = 90,
      NINETYTWO = 92, SIZE = 256, BIGSIZE = 1000;

//function prototypes
bool checkInput(char * &#38;);
void createStream(char *, char []);
void createTable(char [][TWENTYSEVEN], const char [], const char []);
void decipher(ifstream, ofstream, const char[], char[]);
void display(char *);
void formatData(char []);
void getFileNames(char *&#38;, char *&#38;);
void getFileToDecipher(const char[], char[][TWENTYSEVEN]);
int getInputType(void);
int getKeyword(char *&#38;);
bool getMessage(char*, char*, char [], const char[], const
                  char[][TWENTYSEVEN]);


//----------------------------------------------------------------
//Function: main()
//Parameters: None
//Return Type: int - 0 means program terminated normally
//Purpose: Runs the main part of the program.
//----------------------------------------------------------------
int main()
&#123;

  char plaintext[TWENTYSEVEN] = &#123;'A','B','C','D','E','F','G','H',
                       'I','J','K','L','M','N','O','P',
                       'Q','R','S','T','U','V','W','X',
                       'Y','Z','\0'&#125;;
 
 char *ptext_keyword, *ctext_keyword, *infile, *outfile;

  //p_stream is used to help form the alphabetic-shifted cipher
  //alphabets
  char p_stream[TWENTYSIX] = &#123;'\0'&#125;;

  //c_stream represented the top row of the polyalphabetic ciphertext
  //matrix
  char c_stream[TWENTYSIX] = &#123;'\0'&#125;;

  //table is the actual polyalphabetic ciphertext matrix
  char table[TWENTYSIX][TWENTYSEVEN];

  char enc_msg[SIZE];

  int type_of_input;
  bool success = false;

  cout &lt;&lt; "Enter plaintext keyword or keyword phrase in UPPERCASE: ";
  getKeyword(ptext_keyword);
  cout &lt;&lt; "Enter ciphertext keyword or keyword phrase in UPPERCASE: ";
  getKeyword(ctext_keyword);
  createStream(ptext_keyword, p_stream);
  createStream(ctext_keyword, c_stream);
  createTable(table, plaintext, c_stream);
  type_of_input = getInputType();

  if (type_of_input)&#123;
     //decipher accordingly
     getFileToDecipher(p_stream, table);
  &#125;
  else &#123;
     getFileNames(infile, outfile);
     getMessage(infile, outfile, enc_msg, p_stream, table);
     cout &lt;&lt; "Press return to display the deciphered text." &lt;&lt; endl;
     display(outfile);
  &#125;

  return 0;
&#125;
</PRE>
<!-- END CODE //-->
<P><B>Listing 5.11</B> Statements in the decipher and getFileToDecipher functions from the program DPOLY2.CPP.</P>
<!-- CODE //-->
<PRE>
//----------------------------------------------------------------
//Function: decipher()
//Parameters: in - the input file we are deciphering
//      out - the output file we are writing the deciphered
//                  text to
//      PSTREAM - the plaintext alphabet (used to decipher)
//      TBL - the polyalphabetic substitution table
//Return Type: None
//Purpose: Decipher the input file and write the contents to the
//output file specified by the user.
//----------------------------------------------------------------
void decipher(ifstream in, ofstream out, const char PSTREAM[], const
           char TBL[][TWENTYSEVEN])
&#123;
  char enc_file_data[SIZE];
  static int which_cipher_alphabet = 0;

  //continue this process until we get to the end of the file
  while (in.getline(enc_file_data, SIZE, '\n'))&#123;

     if (enc_file_data[0] == '/')&#123;
        out &lt;&lt; enc_file_data &lt;&lt; endl;
     &#125;
     else &#123;
        //format the data - i.e., get rid of all spaces
               formatData(enc_file_data);

        //dump data to file
        for (int ix=0; ix&lt;strlen(enc_file_data); ix&#43;&#43;)&#123;

       //used to keep track of what plaintext character
         //we are going to use
         int jx;
         for (jx=0; jx&lt;TWENTYSIX; jx&#43;&#43;)&#123;
         //find where the encrypted data is in the
         //ciphertext - this location corresponds to
         //the plaintext character location
         if (enc_file_data[ix] == TBL[which_cipher_alphabet%TWENTYSIX][jx])
            break;
    &#125;

        //conditionals for grouping by five and inserting
          //new lines
          if (!(ix%TWENTYFIVE))
             out &lt;&lt; endl;

          if ((ix!=0) &#38;&#38; (!(ix%FIVE)))&#123;
             out &lt;&lt; " " &lt;&lt; PSTREAM[jx];
          &#125;
          else &#123;
            out &lt;&lt; PSTREAM[jx];
          &#125;

            which_cipher_alphabet&#43;&#43;;
        &#125;
     &#125;
  &#125;

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


//----------------------------------------------------------------
///----------------------------------------------------------------
//Function: getFileToDecipher()
//Parameters: PSTREAM - the plaintext stream at the top of the
//      polyalphabetic substitution table
//      TBL - the polyalphabetic table
//Return Type: None
//Purpose: Prompt the user for the name of the encrypted file
//and the file the user wants to store the decrypted text to.
//----------------------------------------------------------------
void getFileToDecipher(const char PSTREAM[], char TBL[][TWENTYSEVEN])
&#123;
  char fileinput[SIZE], fileoutput[SIZE];

  cout &lt;&lt; "Enter name of file to decipher: ";
  cin &gt;&gt; fileinput;

  ifstream input(fileinput, ios::in);

  if (!(input))&#123;
     cerr &lt;&lt; "Input file not available. Exiting program." &lt;&lt; endl;
     exit(EXIT_FAILURE);
  &#125;

  cout &lt;&lt; "Enter name of file for output: ";
  cin &gt;&gt; fileoutput;
  ofstream output(fileoutput, ios::out);

  if (!(output))&#123;
     cerr &lt;&lt; "Output file not created.  Exiting program." &lt;&lt; endl;
     exit(EXIT_FAILURE);
  &#125;

  decipher(input, output, PSTREAM, TBL);

  //don't forget to close the files
  input.close();
  output.close();

  cout &lt;&lt; "\nDeciphered text is in " &lt;&lt; fileoutput &lt;&lt; endl;
  cout &lt;&lt; "\nPress return to display deciphered text." &lt;&lt; endl;
  cin.get();
  cin.get();

  display(fileoutput);

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


//----------------------------------------------------------------
</PRE>
<!-- END CODE //-->
<P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="262-266.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="../ch06/273-276.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>


</body></html>

⌨️ 快捷键说明

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