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

📄 161-183.html

📁 这个是密码学的经典著作
💻 HTML
📖 第 1 页 / 共 3 页
字号:
     formCipheredMessage(CTEXT,msg_to_cipher,enciphered_msg);
     printCipherToFile(cipherFile,enciphered_msg);
  }
  }

 //empty the rest of the buffer
 groupBUFFER(cipherFile, strlen(BUFFER));
 }

 //close the files
 textFile.close();
 cipherFile.close();

 //notify user where plaintext and ciphertext files are
 cout << "\nPlaintext file is: " << input << endl;
 cout << "Encrypted file is: " << output << endl << endl;

 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: 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)
{
 int extract_type = typeOfExtraction();

 if(!(extract_type)){
   simpleExtraction(MATRIX, cipher, R, C);
 }
 else {
   numericExtraction(MATRIX, cipher, R, C);
 }

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

 if(!(extract_type))
   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: typeOfExtraction()
//Parameters:  None
//Return Type: int - 0 denotes SIMPLE extraction, 1 NUMERIC
//Purpose: Determines what type of extraction the user wants to
//perform.
//----------------------------------------------------------------
int typeOfExtraction()
{
 char type[SIZE];
 int ret_value;

 do {
  cout << "Enter transposition matrix method - SIMPLE or NUMERIC: ";
  cin >> type;
 } while (strcmp(type, "SIMPLE") && strcmp(type, "NUMERIC"));

 if(!strcmp(type, "SIMPLE"))
   ret_value = 0;
 else
   ret_value = 1;

 return ret_value;
}//end typeOfExtraction
//end file ciphertr.cpp
</PRE>
<!-- END CODE //-->
<P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="152-161.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="184-187.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>


</body></html>

⌨️ 快捷键说明

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