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

📄 187-211.html

📁 这个是密码学的经典著作
💻 HTML
📖 第 1 页 / 共 3 页
字号:
 return success;
}//end getMessage()


//----------------------------------------------------------------
//Function: getShiftKey()
//Parameters:  key_desired - uppercase key entered by the user
//Return Type: None
//Purpose: Get the key the user enters; error checking performed
//until user enters a valid value.
//----------------------------------------------------------------
void getShiftKey(char &key_desired)
{
 bool error = true;

 do {
  //prompt user to enter an uppercase shift key
  cout << "Enter UPPERCASE Alphabetic Shift Key (CTRL-C to quit): ";
  cin >> key_desired;

  int key_value = static_cast<int>(key_desired);

  //do some error checking
  if((key_value < SIXTYFIVE) || (key_value > NINETY)){
    cerr << "\nYou must enter a letter from A to Z!" << endl << endl;
  }
  else {
    cout << endl;
    error = false;
   }
  } while (error);

  return;
}//end getShiftKey()


//----------------------------------------------------------------
//Function: groupBUFFER()
//Parameters: out - the output stream we are writing to
//      num - the number of characters we want to output
//Return Type: None
//Purpose: Output the buffer in groups of five characters at a
//time.
//----------------------------------------------------------------
void groupBUFFER(ofstream out, int num)
{

 for(int kx=0;kx<num;kx++){

   if((kx!=0) && (kx%TWENTYFIVE==0)){
     out << endl;
   }

   if((kx!=0) && (kx%FIVE == 0) && (kx%TWENTYFIVE!=0)){
      out << " " << *(BUFFER+kx);
   }
   else {
        out << *(BUFFER+kx);
   }
 }

 out << endl;

 return;
}//end groupBUFFER()


//----------------------------------------------------------------
//Function: interval()
//Parameters:  ctext - the cipher stream we are modifying
//Return Type: None
//Purpose: Create the cipher stream based on interval extraction.
//----------------------------------------------------------------
void interval(char ctext[])
{
  int interval, pos;
  char temp[TWENTYSEVEN];

  cout << "Enter the interval as a numeric between 1 and 25: ";
  cin >> interval;

  while ((interval < ONE) || (interval > TWENTYFIVE)){
   cerr << "\aInterval must be between 1 and 25." << endl;
   cerr << "Enter interval: ";
   cin >> interval;
  }

  for(int ix=0; ix<TWENTYSIX; ix++)
    temp[ix] = ctext[ix];

  for(int jx=0; jx<TWENTYSIX; jx++){
    pos = (((jx+1)*interval)-1)%TWENTYSIX;
    ctext[jx] = temp[pos];
  }

  cout << "\nInterval-based alphabet is " << ctext << endl << endl;

  return;
}//end interval()


//----------------------------------------------------------------
//Function: numericExtraction()
//Parameters: MATRIX - the matrix we want to form the cipher from
//      cipher - the cipher stream we will create
//      R - the number of rows in the matrix
//      C - the number of columns in the matrix
//Return Type: None
//Purpose: Creates the cipher stream we will use 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.
//----------------------------------------------------------------
void numericExtraction(char **& MATRIX, char cipher[], const int R,
                           const int C)
{
  int counter = 0;

  //place ASCII values of first row's characters into a table;
  //we will use these values to determine the order in which
  //we pull columns out of the matrix
  int *top_row_ASCII_values = new int[C];
  assert(top_row_ASCII_values);

  //no cast to integer type required since this is already taken
  //care of for us
  for (int ix=0; ix<C; ix++){
    *(top_row_ASCII_values + ix) = *((*MATRIX)+ix);
  }

  for(int jx=0; jx<C; jx++){

    //find out what column we want
    int col = findLowestValue(top_row_ASCII_values, C);

    //put contents of the column into the cipher stream, but
    //only do this when we have a character!
    for(int kx=0; kx<R; kx++){
       if((*(*(MATRIX+kx)+col)) != '\0'){
         cipher[counter++] = (*(*(MATRIX+kx)+col));
       }
     }
  }

  //destroy dynamically allocated memory
  delete [] top_row_ASCII_values;

  return;
}//end numericExtraction()


//----------------------------------------------------------------
//Function: printCipherToFile()
//Parameters: op - the output file we are writing to
//            msg - the cipher text we are displaying
//Return Type: None
//Purpose: Group the cipher in 5-block characters in the
//specified output file.
//----------------------------------------------------------------
void printCipherToFile(ofstream op, char msg[]){

  formatData(msg);

  //check to see if there are more than 25 characters
  //in the buffer; if so, print out as many groups of
  //25 as possible
  if(strlen(BUFFER) >= TWENTYFIVE){

     int numchars = (strlen(BUFFER)/TWENTYFIVE)*TWENTYFIVE;
     //print the contents of the buffer to the output stream
     groupBUFFER(op, numchars);
     //shift whatever is left in the buffer
     strcpy(BUFFER, (BUFFER+numchars));
     //append data to the buffer
     strcat(BUFFER, msg);
  }

  //if buffer contents are less than 25, simply append the new
  //data to the buffer
  else if ((strlen(BUFFER) >= 0) && (strlen(BUFFER) < TWENTYFIVE)){
    strcat(BUFFER, msg);
  }

  return;
}//end printCipherToFile()


//----------------------------------------------------------------
//Function: printMatrix()
//Parameters: the_matrix - the matrix we want to display
//      ROWS - the number of rows in the matrix
//      COLS - the number of columns in the matrix
//Return Type: None
//Purpose: Displays the matrix.
//----------------------------------------------------------------
void printMatrix(char ** &the_matrix, const int ROWS, const int COLUMNS)
{
  cout << "\nThe matrix is:" << endl << endl;

  for(int ix=0; ix<ROWS; ix++){

    for(int kx=0; kx<COLUMNS; kx++){
      cout << *(*(the_matrix+ix)+kx) << " ";
    }

    cout << endl;
  }

  return;
}//end printMatrix()


//----------------------------------------------------------------
//Function: reviseCipherStream()
//Parameters: MATRIX - the numeric keyed transposed matrix
//      cipher - the cipher stream that was originally created
//           when the user entered a keyword
//      R - the number of rows in the matrix
//      C - the number of columns in the matrix
//Return Type: None
//Purpose: Overwrites the cipher stream initially created with the
//one we get by taking columns out of the matrix.
//----------------------------------------------------------------
void reviseCipherStream(char ** &MATRIX, char cipher[], const int R,
                const int C, const int enc_type)
{
  if(enc_type == TWO){
     simpleExtraction(MATRIX, cipher, R, C);
  }
  else {
    numericExtraction(MATRIX, cipher, R, C);
  }

  //print the resulting matrix
  printMatrix(MATRIX, R, C);

  if(enc_type == TWO)
    cout << "\nSIMPLE Transposition alphabet is: " << cipher << endl
              << endl;
  else
    cout << "\nNUMERIC Transposition alphabet is: " << cipher << endl
                      << endl;
  return;
}//end reviseCipherStream()


//----------------------------------------------------------------
//Function: shiftKeyword()
//Parameters: ctext - the cipher alphabet we are going to shift
//      user_key - the key the user entered
//Return Type: None
//Purpose: Shift the keyword or keyword phrase we will use later to
//encode the user's message.
//----------------------------------------------------------------
void shiftKeyword(char ctext[], const char USER_KEY)
{
  int location;
  char temp[TWENTYSIX] = {'\0'};

  //find the location of the key in the plaintext
  for(int ix=0; ix<TWENTYSIX; ix++){
     if(USER_KEY == ctext[ix]){
       location = ix;
       break;
     }
  }

  if (location == TWENTYFIVE){
          //do nothing
  }
  else {

     //put into temp all the characters up to and including the shift key
     //location now indicated how many characters, so we must increment by
     //one since the array uses a zero-based index
     strncpy(temp, ctext, location+1);
     //shift all remaining characters in the ciphertext
     strcpy(ctext, (ctext+location+1));
     //concatenate temp back to ctext
     strcat(ctext, temp);
  }

  cout << "Shifted keyword-mixed alphabet is: " << ctext << endl << endl;

  return;
}//end shiftKeyword();


//----------------------------------------------------------------
//Function: simpleExtraction()
//Parameters: MATRIX - the matrix we want to form the cipher from
//      cipher - the cipher stream we will create
//      R - the number of rows in the matrix
//      C - the number of columns in the matrix
//Return Type: None
//Purpose: Creates the cipher stream we will use to encrypt files
//by pulling off the columns from left to right and inserting them
//into the cipher array.
//----------------------------------------------------------------
void simpleExtraction(char **& MATRIX, char cipher[], const int R,
                           const int C)
{
  int counter = 0;

  for (int ix=0; ix<C; ix++){
     for (int kx=0; kx<R; kx++){
        if ((*(*(MATRIX+kx)+ix)) != '\0'){
           cipher[counter++] = (*(*(MATRIX+kx)+ix));
           }
     }
  }

  return;
}//end simpleExtraction()


//----------------------------------------------------------------
//Function: welcome()
//Parameters:  None
//Return Type: int - method the cipher stream will be created;
//1 is interval, 2 is simple, 3 is numeric
//Purpose: Allow the user to select which method he/she wants
//to use to create the cipher stream.
//----------------------------------------------------------------
int welcome()
{
  int type;

  cout << "Program cipher6.cpp enciphers a message using one" << endl
     << "of the following techniques to form a cipher alphabet:"
                        << endl << endl
        << "(1) Interval Extraction Sequence" << endl
        << "(2) Simple Transposition Sequence" << endl
        << "(3) Numeric Transposition Sequence" << endl << endl
        << "Enter method to form enciphered message 1, 2 or 3: ";

  cin >> type;

  while ((type <ONE) || (type > THREE)){
   cerr << "\aYou must enter a number between 1 and 3!" << endl;
   cerr << "Choice: ";
   cin >> type;
  }

  //get the newline off the stream
  cin.get();
  cout << endl;

  return type;
}//end welcome()
//end file cipher6.cpp
</PRE>
<!-- END CODE //-->
<P><BR></P>
<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>


</body></html>

⌨️ 快捷键说明

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