📄 246-262.html
字号:
}//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: PSTREAM - the top row of the polyalphabetic cipher matrix
// TBL - the actual polyalphabetic cipher matrix
// MESSAGETOCIPHER - the message we want to encipher
// enc_message - the enciphered message
//Return Type: char* - a pointer to the encoded information.
//Purpose: Encipher the user's message.
//----------------------------------------------------------------
char* formCipheredMessage(const char PSTREAM[], const char TBL[]
[TWENTYSEVEN],
const char MESSAGETOCIPHER[], char enc_message[])
{
int length = strlen(MESSAGETOCIPHER)+1;
//location identifies where in the CSTREAM character the plaintext
//character is
int location = 0;
//use this variable to keep track of which cipher alphabet
//we are using - making it a static ensures that its value
//is preserved when we make subsequent function calls
static int which_cipher_alphabet = 0;
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 {
//find the location of the character we want to
//encipher in the CSTREAM
for (int jx=0; jx<TWENTYSIX; jx++){
if (MESSAGETOCIPHER[ix] == PSTREAM[jx]){
location = jx;
break;
}
}
enc_message[ix] = TBL[which_cipher_alphabet%TWENTYSIX][location];
//go to the next cipher alphabet
which_cipher_alphabet++;
}
}
//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: 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 {
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
// PSTREAM - the top row of the polyalphabetic ciphertext
// matrix; used to index into the actual table
// TBL - the polyalphabetic ciphertext matrix
//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, const char PSTREAM[],
const char TBL[][TWENTYSEVEN], char msg_to_cipher[])
{
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;
//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(PSTREAM, TBL, 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: 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: writeMatrixToFile()
//Parameters: CSTREAM - the top row of the polyalphabetic cipher matrix
// TBL - the actual matrix
//Return Type: bool - indicates success of the operation
//Purpose: Prints the matrix to a file "table.dat" for future
//reference.
//----------------------------------------------------------------
bool writeMatrixToFile(const char PSTREAM[], const char TBL[][TWENTYSEVEN])
{
ofstream output("table.dat", ios::out);
bool success = false;
if (output){
success = true;
output << PSTREAM << endl << endl;
for (int ix=0; ix<TWENTYSIX; ix++)
output << TBL[ix] << endl;
}
cout << "Polyalphabetic matrix was written to file table.dat"
<< endl << endl;
output.close();
return success;
}//end writeMatrixToFile()
//end file poly2.cpp
</PRE>
<!-- END CODE //-->
<P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="240-245.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="262-266.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
</body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -