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

📄 104-125.html

📁 这个是密码学的经典著作
💻 HTML
📖 第 1 页 / 共 3 页
字号:
    char buffer[SIZE];
    cout << "Enter keyword or keyword phrase in UPPERCASE" << endl
      << "do not use spaces or non-alphabetic characters): ";

    cin.getline(buffer, SIZE, '\n');
    text = new char[strlen(buffer) + 1];
    strcpy(text, buffer);

    error = checkInput(text);

  } while (error);

  return;
}//end getKeyword()


//------------------------------------------------
//Function: getMessage()
//Parameters:  input - the name of the input plaintext file
//       output the name of the output ciphertext file
//       msg_to_cipher - the message 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 or lowercase characters. "
                  << endl
         << "Non-alphabetic characters may be entered but are ignored."
                  << endl
         << "Use a / at the beginning of each line that should remain"
                  << endl
         << "in plaintext and a \\ on a separate line to indicate the"
                  << endl
         << "end of an enciphered message." << endl << endl;

    while (go_on) {

     //get the entire line, up to 256 characters
     cin.getline(msg_to_cipher, SIZE, '\n');
 
     //case user doesn't want the text to be encrypted
     if(msg_to_cipher[0] == '/'){

       if(strlen(BUFFER)>0){
         //empty whatever is in the buffer
         groupBUFFER(cipherFile, strlen(BUFFER));
         //adjust the buffer
         strcpy(BUFFER, (BUFFER+strlen(BUFFER)));
       }

       //output plaintext
       textFile << msg_to_cipher << endl;
       cipherFile << msg_to_cipher << endl;
    }

     //case user is done entering text
     else if (static_cast<int>(msg_to_cipher[0]) == NINETYTWO){
       go_on = false;
    }

     //encrypt the text
     else {
       textFile << msg_to_cipher << endl;
       char enciphered_msg[BIGSIZE];
       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 {
      error = false;
    }
  } while (error);

  cout << endl;

  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: 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: 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);
  }
  return;
}//end shiftKeyword();


//------------------------------------------------
//Function: welcome()
//Parameters: None
//Return Type: None
//Purpose: Prints the program's greeting message.
//------------------------------------------------
void welcome(void)
{
  cout << "CIPHER5.EXE PROGRAM enciphers text based upon the use of a"
                 << endl
       << "keyword or keyword phrase and an alphabetic shift key using a"
                 << endl
       << "monoalphabetic substitution process." << endl << endl;
  return;
}//end welcome()
//end file cipher5.cpp
</PRE>
<!-- END CODE //-->
<P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="100-104.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="125-128.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>


</body></html>

⌨️ 快捷键说明

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