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

📄 187-211.html

📁 这个是密码学的经典著作
💻 HTML
📖 第 1 页 / 共 3 页
字号:
<html><head><TITLE>Learn Encryption Techniques with BASIC and C++:Transposition-based Monoalphabetic 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=4//-->
<!--PAGES=187-211//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->

<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="184-187.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="211-215.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H4 ALIGN="CENTER"><A NAME="Heading19"></A><FONT COLOR="#000077">The CIPHER6.CPP Program</FONT></H4>
<P>Continuing our presentation of C&#43;&#43; versions of each BASIC language version, Listing 4.7 contains the program listing of CIPHER6.CPP. As you review the program listing, you will note that an extensive effort was made to make the program as &#147;crash-free&#148; as possible, as well as to incorporate the functionality of the BASIC language version of the program. When you examine the program listing, you may wish to focus your attention upon the functions interval, which creates the cipher stream based on interval extraction, and numericExtraction, which creates the cipher stream used to encrypt files by pulling off the columns from the matrix in alphabetical order based on the character in the top row and inserting them into the cipher array. Although many of the previously developed C&#43;&#43; functions are included once again in this program listing, the listing is presented in its entirety to facilitate reference to the relationship between functions as well as the manner by which they operate.
</P>
<P><B>Listing 4.7</B> The CIPHER6.CPP program listing.</P>
<!-- CODE //-->
<PRE>
/*
cipher6.cpp
C&#43;&#43; code written by Jonathan Held using Microsoft Visual C&#43;&#43;,
version 5.0, on March 24, 1998.
*/

//standard include files
#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;

//function prototypes
bool checkInput(char * &#38;);
void createCipherStream(char *, char[]);
void createMatrix(char ** &#38;, const char [], const int, const int);
void display(char *);
bool encryptText(char *, char *, const char [], const char[], char []);
void deleteMatrix(char **&#38;, const int, const int);
int findLowestValue(int *&#38;, const int);
void formatData(char []);
char* formCipheredMessage(const char[], const char [], char []);
void getFileNames(char *&#38;, char *&#38;);
int getInputType(void);
int getKeyword(char *&#38;);
bool getMessage(char*, char*, char [], const char[], const char[]);
void getShiftKey(char &#38;);
void groupBUFFER(ofstream, int);
void interval(char[]);
void numericExtraction(char **&#38;, char [], const int, const int);
void printCipherToFile(ofstream, char[]);
void printMatrix(char ** &#38;, const int, const int);
void reviseCipherStream(char **&#38;, char [], const int, const int,
                           const int);
void shiftKeyword(char [], const char);
void simpleExtraction(char **&#38;, char [], const int, const int);
int welcome(void);

//constants we will use
const int ONE = 1, TWO = 2, THREE = 3, FIVE = 5, TWENTYFIVE = 25,
      TWENTYSIX = 26, TWENTYSEVEN = 27, SIXTYFIVE = 65,
      NINETY = 90, NINETYTWO = 92, SIZE = 256, DUMMY_VAL = 999,
      BIGSIZE = 1000;
char BUFFER[BIGSIZE] = &#123;'\0'&#125;;
//----------------------------------------------------------------
//Function: main()
//Parameters: None
//Return Type: int - 0 execution is normal, 1 abnormal termination
//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 cipherStream[TWENTYSEVEN] = &#123;'\0'&#125;;
 char message_to_cipher[SIZE], enciphered_message[SIZE];

 char ** cMatrix;
 char *keyword, key, *infile, *outfile;
 int rows, columns, input_type, encipher_type;
 bool success;


 encipher_type = welcome();

 if(encipher_type == ONE)&#123;
    getKeyword(keyword);
    createCipherStream(keyword, cipherStream);
    cout &lt;&lt; "Keyword-based alphabet is: "
         &lt;&lt; cipherStream &lt;&lt; endl &lt;&lt; endl;
    interval(cipherStream);
 &#125;
 else &#123;
   //get the keyword we are going to use, determine number of rows
   //and columns of our matrix
   columns = getKeyword(keyword);
   rows = TWENTYSIX/columns;

   //integer division requires that we check and see if there is a
   //remainder; if so, we need to add one extra row to our matrix
   if(TWENTYSIX%columns)&#123;
     rows&#43;&#43;;
   &#125;

   //create the initial ciphertext stream
   createCipherStream(keyword, cipherStream);

   //echo to the user what we have
   cout &lt;&lt; "Plaintext-based alphabet is: " &lt;&lt; plaintext &lt;&lt; endl
        &lt;&lt; "Keyword-based alphabet is:   " &lt;&lt; cipherStream &lt;&lt; endl
                       &lt;&lt; endl;

   //insert the stream into our matrix
   createMatrix(cMatrix, cipherStream, rows, columns);

   reviseCipherStream(cMatrix, cipherStream, rows, columns,
                   encipher_type);

 &#125;

 getShiftKey(key);
 shiftKeyword(cipherStream, key);

 getFileNames(infile, outfile);

 input_type = getInputType();

 //process file input
 if(input_type)&#123;
   success = encryptText(infile, outfile, plaintext, cipherStream,
                 enciphered_message);
 &#125;
 else &#123;
   cout &lt;&lt; "Use a \'/\' to leave a line in plaintext." &lt;&lt; endl
     &lt;&lt; "Use a \'\\' to indicate end of message input. " &lt;&lt; endl;
   success = getMessage(infile, outfile, message_to_cipher, plaintext,
    cipherStream);
 &#125;

 //report success of operation
 if(!success)&#123;
   cerr &lt;&lt; "Error: Invalid filename specified. Goodbye." &lt;&lt; endl;
 &#125;
 else &#123;
   cout &lt;&lt; "Press return to display resulting enciphered message."
                    &lt;&lt; endl;
   //get the newlines off the current input stream
   cin.get();
   if(input_type == ONE)&#123;
     cin.get();
   &#125;
   display(outfile);
 &#125;

 //delete dynamically allocated memory accordingly
 if(encipher_type != ONE)
   deleteMatrix(cMatrix, rows, columns);

 delete [] infile;
 delete [] outfile;
 delete [] keyword;

 return (!success);
&#125;//end main()


//----------------------------------------------------------------
//Function: checkInput()
//Parameters: input - the keyword 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; SIXTYFIVE) || (char_value &gt; NINETY))&#123;
     error = true;
     cerr &lt;&lt; "You entered an invalid keyword!" &lt;&lt; endl &lt;&lt; endl;
     break;
   &#125;
 &#125;

 if(count == 0)&#123;
   cerr &lt;&lt; "You entered an invalid keyword!" &lt;&lt; endl &lt;&lt; endl;
   error = true;
 &#125;

 return error;
&#125;//end checkInput()


//----------------------------------------------------------------
//Function: createCipherStream()
//Parameters: input - the keyword the user entered
//      cipher - the keyword alphabet that will be constructed
//Return Type: None
//Purpose: Creates a preliminary cipher stream that will be used to
//form the cipher matrix.
//----------------------------------------------------------------
void createCipherStream(char *input, char stream[])
&#123;
 bool used[TWENTYSIX];
 int index = 0,
   count = strlen(input);

 //no characters are initially used
 for(int ix=0; ix&lt;TWENTYSIX; ix&#43;&#43;)&#123;
   used[ix] = false;
 &#125;

 //keep track of each character used, start forming the keyword
 //alphabet
 for(int jx=0; jx&lt;count; jx&#43;&#43;)&#123;

   //get each character of the input string (integer value)
   int char_value = static_cast&lt;int&gt;(*(input&#43;jx));

   if(used[char_value-SIXTYFIVE])&#123;
     //do nothing - the character was already used
   &#125;
   else &#123;
     //mark as used and add to the keyword alphabet
     used[char_value-SIXTYFIVE] = true;
     *(stream&#43;index&#43;&#43;) = static_cast&lt;char&gt;(char_value);
   &#125;
 &#125;

 //go through the list of characters used - those which weren't
 //used should be added to the keyword alphabet
 for(int kx=0; kx&lt;TWENTYSIX; kx&#43;&#43;)&#123;

   if(!(used[kx]))&#123;
     *(stream&#43;index&#43;&#43;) = static_cast&lt;char&gt;(SIXTYFIVE&#43;kx);
   &#125;
 &#125;

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


//----------------------------------------------------------------
//Function: createMatrix()
//Parameters: matrix - the matrix we are going to create
//      CSTREAM - the initial cipher stream based on the
//          user's keyword
//      ROWS - the number of rows in the matrix
//      COLS - the number of columns in the matrix
//Return Type: None
//Purpose: Creates a numeric key transposed matrix, identical to
//figure 4.4.
//----------------------------------------------------------------
void createMatrix(char ** &#38;matrix, const char CSTREAM[], const int ROWS,
                    const int COLS)
&#123;
 int count = 0;

 //dynamically allocate memory for the RxC matrix
 //we use assert to ensure that memory was allocated;
 //if not, then the program will terminate abnormally
  assert(matrix = new char*[ROWS]);
  for(int ix=0; ix&lt;ROWS; ix&#43;&#43;)&#123;
    *(matrix &#43; ix) = new char[COLS];
  &#125;

  //fill in the matrix
  for(int jx=0; jx&lt;ROWS; jx&#43;&#43;)&#123;
    for (int kx=0; kx&lt;COLS; kx&#43;&#43;)&#123;
    //we only want to enter a character into the matrix
    //twenty-six times - most of the time we allocate
    //a matrix larger than what we will need to use; when
    //we have more than 26 characters, we simply insert a
    //null terminator into the matrix
      if(count &lt; TWENTYSIX)
        *(*(matrix&#43;jx)&#43;kx) = CSTREAM[count&#43;&#43;];
            else
        *(*(matrix&#43;jx)&#43;kx) = '\0';
    &#125;
 &#125;

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


//----------------------------------------------------------------
//Function: encryptText()
//Parameters:  inp_file - the name of the input plaintext file
//       outp_file - the name of the output ciphertext file
//       PTEXT[] - the plaintext alphabet
//       CTEXT[] - the ciphertext alphabet
//       encoded_msg[] - the message to be encoded
//Return Type: bool, indicating success of operation
//Purpose: Used to encrypt file input.  Takes each line of the input
//file, encrypts it, and saves the result to the specified output
//file.
//----------------------------------------------------------------
bool encryptText(char * inp_file, char * outp_file, const char PTEXT[],
          const char CTEXT[], char encoded_msg[])
&#123;
 bool success = false;
 char ip[SIZE];

 //declare file stream objects
 ifstream input(inp_file, ios::in);
 ofstream output(outp_file, ios::app);

 if((!input) || (!output))&#123;
   //do nothing - I/O error; user will be notified upon
   //procedure's return to main()
 &#125;
 else &#123;

   success = true;

   //print plaintext and ciphertext alphabets to the
   //output file
   output &lt;&lt; "PLAINTEXT:  " &lt;&lt; PTEXT &lt;&lt; endl;
   output &lt;&lt; "CIPHERTEXT: " &lt;&lt; CTEXT &lt;&lt; endl &lt;&lt; endl;

   while (input.getline(ip, BIGSIZE, '\n'))&#123;

⌨️ 快捷键说明

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