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

📄 104-125.html

📁 这个是密码学的经典著作
💻 HTML
📖 第 1 页 / 共 3 页
字号:

  //keep track of each character used, start forming the keyword
  //alphabet
  for (int jx=0; jx<count; jx++){

    //get each character of the input string (integer value)
    int char_value = static_cast<int>(*(input+jx));
    if (used[char_value-SIXTYFIVE]){
       //do nothing - the character was already used
    }
    else {
      //mark as used and add to the keyword alphabet
      used[char_value-SIXTYFIVE] = true;
      *(cipher+index++) = static_cast<char>(char_value);
    }
  }

  //go through the list of characters used - those which weren't
  //used should be added to the keyword alphabet
  for (int kx=0; kx<TWENTYSIX; kx++){

    if (!(used[kx])){
       *(cipher+index++) = static_cast<char>(SIXTYFIVE+kx);
    }
  }

  return;
}//end createAlphabet()


//------------------------------------------------
//Function: display()
//Parameters: name - the name of the file the user wants displayed
//Return Type: None
//Purpose: Echos the resulting output file to the screen.
//------------------------------------------------
void display(char *name)
{
  ifstream infile(name, ios::in);
  char input[SIZE];

  if (!(infile)){
     cerr << "Unable to open input file for display." << endl;
  }
  else {
    while (infile.getline(input, SIZE, '\n')){
     cout << input << endl;
    }
  }
  return;
}//end display()


//------------------------------------------------
//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[])
{
  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)){
    //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(ip, BIGSIZE, '\n')){

     //check to see if the user wants the line to appear in plain text
     if (ip[0] == '/'){
        if (strlen(BUFFER)>0){

        //empty whatever is in the buffer
        groupBUFFER(output, strlen(BUFFER));
        //adjust the buffer
        strcpy(BUFFER, (BUFFER+strlen(BUFFER)));
        //output plaintext
      }

      output << ip << endl;

      }
      else {
        //encipher the line
        char *msg = formCipheredMessage(CTEXT, ip, encoded_msg);
        //print the cipher in groups of five to the ouput file
        printCipherToFile(output, msg);
      }
    }

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

    //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: formatData()
//Parameters: data - the array we want to format
//Return Type: None
//Purpose: Get rid of all spaces in the array.
//------------------------------------------------
void formatData(char data[]){

  for (int mx=0, nx=0; (*(data+nx) != '\0'); nx++){
    if (*(data+nx) == ' '){
       //do nothing - skip over the space in the data
    }
    else {
      *(data+mx++) = *(data+nx);
    }
  }

  //don't forget to add the null terminator
  *(data+mx) = '\0';

  return;
}//end formatData()


//------------------------------------------------
//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 character; 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];
    }
    else {
       //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-SIXTYFIVE];
     }
  }

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


//------------------------------------------------
//Function: getFileNames()
//Parameters: infile_name - the input file
//       outfile_name - the output file we will write the
//       enciphered text to
//Return Type: None
//Purpose: Get file information from the user.
//------------------------------------------------
void getFileNames(char * &infile_name, char * &outfile_name)
{
  char data[SIZE];

  cout << "Enter filename to store/retrieve plaintext message: ";

  cin >> data;

  infile_name = new char[strlen(data) + 1];
  strcpy(infile_name, data);

  cout << "Enter filename to store enciphered message: ";

  cin >> data;

  outfile_name = new char[strlen(data) + 1];
  strcpy(outfile_name, data);

  cout << endl;

  return;
}//end getFileNames()


//------------------------------------------------
//Function: getInputType()
//Parameters: None
//Return Type: int - 0 indicates keyboard input, 1 indicates file
//       input
//Purpose: Determines if the user will be manually entering text to
//be enciphered or if the user wants a file to be enciphered.
//------------------------------------------------
int getInputType(void)
{
  char type;
  bool error = false;
  int value;

  do {
    //prompt user for input from file or keyboard
    cout << "Is file input from keyboard (K, k) or file (F, f): ";
    cin >> type;

    //make type an uppercase letter
    type = static_cast<char>(toupper(static_cast<int>(type)));

    //check for an invalid character
    if((type != 'K') && (type != 'F')){
      cerr << "You have entered an invalid character!" << endl << endl;
      error = true;
    }
    else {
      if (type == 'K')
         value = 0;       //value of 0 represents keyboard input
         else value = 1;    //value of 1 represents file input
      error = false;
    }

  } while (error);

  cout << endl;

  return value;

}//end getInputType()


//------------------------------------------------
//Function: getKeyword()
//Parameters: text - the keyword that the user enters
//Return Type: None
//Purpose: Prompts the user for a keyword and continues until
//a valid keyword has been entered.
//------------------------------------------------
void getKeyword(char * &text)
{
  bool error = false;

  do {

⌨️ 快捷键说明

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