📄 161-183.html
字号:
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: deleteMatrix()
//Parameters: matrix - the matrix we are going to destroy
// R - the number of rows in the matrix
// C - the number of columns in the matrix
//Return Type: None
//Purpose: Destroys the dynamically allocated matrix!
//----------------------------------------------------------------
void deleteMatrix(char **&matrix, const int R, const int C)
{
for(int ix=0; ix<R; ix++)
delete [] *(matrix+ix);
delete [] matrix;
return;
}//end deleteMatrix()
//----------------------------------------------------------------
//Function: display()
//Parameters: name - the name of the file the user wants displayed
//Return Type: None
//Purpose: Echoes 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: findLowestValue()
//Parameters: values - the ASCII values of all characters in the
// top row of the matrix
// COLS - the number of columns in the matrix
//Return Type: int - the column we want.
//Purpose: Determines what column we are going to extract from
//the matrix and put into the cipher stream.
//----------------------------------------------------------------
int findLowestValue(int *&values, const int COLS)
{
int loc=0, lowest=999;
for(int ix = 0; ix < COLS; ix++){
if(*(values+ix) != 999){
if(*(values+ix) < lowest){
lowest = *(values+ix);
loc = ix;
}
}
}
*(values+loc) = 999;
return loc;
}//end findLowestValue()
//----------------------------------------------------------------
//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: 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: 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: getKeyword()
//Parameters: text - the keyword that the user enters
//Return Type: int - the length of the keyword
//Purpose: Prompts the user for a keyword and continues until
//a valid keyword has been entered. Returns the length of the
//keyword.
//----------------------------------------------------------------
int getKeyword(char * &text)
{
bool error = false;
char buffer[SIZE];
do {
cout << "Enter keyword or keyword phrase in UPPERCASE" << endl
<< "do not use spaces or non-alphabetic characters): ";
cin.getline(buffer, SIZE, '\n');
assert(text = new char[strlen(buffer) + 1]);
strcpy(text, buffer);
error = checkInput(text);
//delete text if there was an error
if(error){
delete [] text;
}
} while (error);
cout << endl;
return strlen(buffer);
}//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 characters: " << 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];
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -