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

📄 068-081.html

📁 这个是密码学的经典著作
💻 HTML
📖 第 1 页 / 共 2 页
字号:
  ifstream input(inp_file, ios::in);
  ofstream output(outp_file, ios::app);

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

 success = true;

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

 while (input.getline(buffer, 256, '\n')){

  //look at the next character
  nextChar = input.peek();

  //if we've grabbed 256 characters, and the very next character
  //is the newline delimeter, we want to ignore it
  if (nextChar == '\n')
     input.ignore();

  //check to see if the user wants the line to appear in plain text
  if (buffer[0] == '/'){
     output << buffer << endl;
  }
  else {
    //encipher the line
    char *msg = formCipheredMessage(CTEXT, buffer, encoded_msg);
    //print the cipher in groups of five to the ouput file
    printCipherToFile(output, msg);
   } }

   //notify user where plaintext and ciphertext files are
     cout << "Plaintext file is: " << inp_file << endl;
     cout << "Encrypted file is: " << outp_file << endl << endl;

  }

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

  //return success of the operation
  return success;
}//end encryptText()

//-----------------------------------------------------
//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 < 65) || (key_value > 90)){
      cerr << "\nYou must enter a letter from A to Z!" << endl << endl;
    }
    else {
     error = false;
    }
  } while (error);

  cout << endl;

  return;
}//end getShiftKey()

//-----------------------------------------------------
//Function: createCipher()
//Parameters: PTEXT - the plaintext alphabet
//      ctext - the cipher alphabet we are going to create
//      user_key - the key the user entered
//Return Type: None
//Purpose: Create the cipher stream we will use later to encode the
//user's message.
//-----------------------------------------------------
void createCipher(const char PTEXT[], char ctext[], const char user_key){

 int location;

  //find the location of the key in the plaintext
  for (int ix=0; ix<26; ix++){
   if (user_key == PTEXT[ix]){
      //location is one more than ix
      location = ix + 1;
      break;
    }
  }

  //create the cipher text
  for (int jx=0; jx<26; jx++){
    ctext[jx] = PTEXT[(jx + location) % 26];
 }

  return;
}//end createCipher();

//-----------------------------------------------------
//Function: getMessage()
//Parameters:  input - the name of the input plaintext file
//       output the name of the output ciphertext file
//       msg_to_cipher - the essage to be encoded
//       PTEXT[] - the plaintext alphabet
//       CTEXT[] - the ciphertext alphabet
//Return Type: bool, indicating success of operation
//Purpose: Allow the user to manually input text from the keyboard.
//Save the text in plaintext to the input file; encrypt the text
//and save it to the specified output file for later retrieval.
//-----------------------------------------------------
bool getMessage(char* input, char* output,
char msg_to_cipher[], const char
                  PTEXT[], const
char CTEXT[]){

  bool go_on = true, success = false;

  ofstream textFile(input, ios::app);
  ofstream cipherFile(output, ios::app);

  if ((!textFile) || (!cipherFile)){
     //do nothing - error will be noted to user later
  }
  else {

   success = true;

   textFile << "PLAINTEXT:  " << PTEXT << endl;
   textFile << "CIPHERTEXT: " << CTEXT << endl << endl;

   //get the newline character off of the input stream
     cin.get();

   cout << "Enter the message in UPPERCASE characters: " << endl;

   while (go_on) {

    //get the entire line, up to 256 characters
    cin.getline(msg_to_cipher, 256, '\n');

   //case user doesn't want the text to be encrypted
   if (msg_to_cipher[0] == '/'){
      textFile << msg_to_cipher << endl;
      cipherFile << msg_to_cipher << endl;
   }

    //case user is done entering text (ASCII value is 92
    //for the newline delimeter
    else if (static_cast<int>(msg_to_cipher[0]) == 92){
      go_on = false;
   }

    //encrypt the text
      else {
       textFile << msg_to_cipher << endl;

       char enciphered_msg[256];
       formCipheredMessage(CTEXT,msg_to_cipher,enciphered_msg);
       printCipherToFile(cipherFile,enciphered_msg);
    }
  }
}

  //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: formCipheredMessage()
//Parameters:  CTEXT - the cipher alphabet we will use for substitution
//      MESSAGETOCIPHER - the user's message
//      enc_message - the enciphered message to be determined
//Return Type: char* - a pointer to the encoded information.
//Purpose: Encipher the user's message.
//-----------------------------------------------------
char* formCipheredMessage(const char CTEXT[], const char MESSAGETOCIPHER[],
             char enc_message[]){

  int length = strlen(MESSAGETOCIPHER)+1;

  int encode_value;

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

     //test to see if we have an alphabetic charac倀er; if not,
     //simply copy it to our encrypted message - this preserves
     //characters such as ', ! etc...
     if (!isalpha(static_cast<int>(MESSAGETOCIPHER[ix]))){
        enc_message[ix] = MESSAGETOCIPHER[ix];
     }
     els{
        //valid character - the easy way to calculate the ciphered
        //character is based on the plain text's ascii character value;
        //since it has to be a capital letter, it must be in the range
        //from 65 to 90, with A represented by 65, Z by 90.  By simply
        //subtracting 65 from the encode_value (the integer representation
        //of the plaintext character), we now know what cipher character
        //to use.

        encode_value = toupper(static_cast<int>(MESSAGETOCIPHER[ix]));
        enc_message[ix] = CTEXT[encode_value-65];
     }
  }

  //return a reference to the encoded message
  return enc_message;
}//end formCipheredMessage()

//-----------------------------------------------------
//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-character blocks in the
//specified output file.
//-----------------------------------------------------
void printCipherToFile(ofstream op, const char MSG[]){

  int len = strlen(MSG) + 1;

  //keep track of number of characters
  int counter = 0;

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

    if (*(MSG+kx) != ' '){

    //test to see if we need to print a space
    if ((counter != 0) && (counter%5 == 0)) {
       op << " ";
       }
    //if not, we have a character to print
    op << *(MSG+kx);

    //increment the count of characters
    counter++;
   }
  }

  //force a carriage return in the output file
  op << endl;

  return;
}//end printCipherToFile()
//end file cipher4.cpp
</PRE>
<!-- END CODE //-->
<P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="063-068.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="081-084.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>


</body></html>

⌨️ 快捷键说明

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