📄 068-081.html
字号:
<html><head><TITLE>Learn Encryption Techniques with BASIC and C++:Monoalphabetic Substitution Concepts</TITLE>
<!-- BEGIN HEADER --><META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW"><SCRIPT><!--function displayWindow(url, width, height) { var Win = window.open(url,"displayWindow",'width=' + width +',height=' + height + ',resizable=1,scrollbars=yes');}//--></SCRIPT></HEAD><body bgcolor="ffffff" link="#006666" alink="#006666" vlink="#006666"><P>
<CENTER><B>Learn Encryption Techniques with BASIC and C++</B>
<FONT SIZE="-2">
<BR>
<I>(Publisher: Wordware Publishing, Inc.)</I>
<BR>
Author(s): Gil Held
<BR>
ISBN: 1556225989
<BR>
Publication Date: 10/01/98
</FONT></CENTER>
<P>
<!-- Empty Reference Subhead -->
<!--ISBN=1556225989//-->
<!--TITLE=Learn Encryption Techniques with BASIC and C++//-->
<!--AUTHOR=Gilbert Held//-->
<!--PUBLISHER=Wordware Publishing, Inc.//-->
<!--CHAPTER=2//-->
<!--PAGES=068-081//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<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>
<P><BR></P>
<P><FONT SIZE="+1"><B>Program Execution</B></FONT></P>
<P>To illustrate the use of CIPHER4.BAS, execute the program just developed. The top portion of Figure 2.6 illustrates the execution of the program which displays program information and enables the user to enter his or her message. In this example, the first two lines of the message were entered with the forward slash as a prefix character to ensure those lines remained in cleartext. After the two-line heading, the next four lines required encipherment and were entered without a forward slash character used as a prefix. Finally, the backslash character was entered by itself on a separate line to indicate the termination of the message.
</P>
<P><A NAME="Fig6"></A><A HREF="javascript:displayWindow('images/02-06.jpg',421,418 )"><IMG SRC="images/02-06t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/02-06.jpg',421,418)"><FONT COLOR="#000077"><B>Figure 2.6</B></FONT></A> The execution of CIPHER4.BAS</P>
<P>The lower portion of Figure 2.6 illustrates the resulting enciphered message printed in groups of five characters. Note that the first two lines of heading information are simply printed without alteration, while the body of the message is both enciphered and printed in groups of five characters. Also note that the last group contained three characters and required the addition of two X’s to complete the group of five. You can easily confirm this visually by noting that the message ended with the word STOP. Because a simple alphabetic shift was used for enciphering using the character B as the shift key, each character of plaintext is shifted up two positions in the alphabet. Thus, STOP becomes UVQR. Because VQR are the last three characters in the message, two X’s were added to complete the group.
</P>
<P><FONT SIZE="+1"><B>Program Default Files</B></FONT></P>
<P>To understand the contents of the default files created by CIPHER4.BAS, let us examine their contents after the program is executed. Figure 2.7 lists the contents of MESSAGE.DAT and CIPHERTX.DAT based upon the message illustrated in Figure 2.6 being entered when CIPHER4.BAS was executed. As indicated in Figure 2.7, the heading is placed on a file as is, while the body of the message is placed on each file with spaces between words being removed.
</P>
<P><A NAME="Fig7"></A><A HREF="javascript:displayWindow('images/02-07.jpg',446,304 )"><IMG SRC="images/02-07t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/02-07.jpg',446,304)"><FONT COLOR="#000077"><B>Figure 2.7</B></FONT></A> The CIPHER4.BAS file contents.</P>
<H4 ALIGN="CENTER"><A NAME="Heading23"></A><FONT COLOR="#000077">The CIPHER4.CPP Program</FONT></H4>
<P>The C++ version of CIPHER4.BAS contains a number of programming improvements which facilitate the use of the program. The program listing of CIPHER4.CPP is contained in Listing 2.15.
</P>
<P><B>Listing 2.15</B> The CIPHER4.CPP program listing.</P>
<!-- CODE //-->
<PRE>
/*
Cipher4.cpp
C++ Code written by Jonathan Held, January 23, 1998, using
Microsoft Visual C++, version 5.0.
486 total lines of code.
*/
//standard include files
#include <iostream.h> //standard i/o operations
#include <string.h> //used to find the length of a string
#include <ctype.h> //for character handling
#include <fstream.h> //file stream processing
#include <stdlib.h> //standard library functions
//function prototypes - see function headers for more information
void getFileNames(char* &, char* &);
int getInputType(void);
void getShiftKey(char &);
void createCipher(const char [], char[], const char);
bool getMessage(char*, char*, char [], const char[],
const char[]); char* formCipheredMessage(const char[],
const char [], char []); void printResults(const char[],
const char[], const char[], const char [],
const int);
void printCipherToFile(ofstream, const char[]);
bool encryptText(char *, char *, const char [],
const char[], char[]);
//-----------------------------------------------------
//Function: main()
//Parameters: argc - the number of command line arguments passed to
// main
//Return Type: int - 0 execution is normal, 1 abnormal termination
//Purpose: Runs the main part of the program.
//-----------------------------------------------------
int main(int argc, char *argv[]){
//initialize plaintext
char plaintext[26] = {'A','B','C','D','E','F','G','H','I',
'J','K','L','M','N','O','P','Q','R',
'S','T','U', 'V','W','X','Y','Z'};
//other variables we will use
char ciphertext[26], message_to_cipher[256],
enciphered_message[256], key;
char *infile, *outfile;
int input_type;
bool success = false;
//function calls
//this code allows the user to run the program from the
//dos prompt with command line arguments, i.e. the user
//can run the program at the dos prompt by typing
//cipher <input filename> <output filename> <key>
if (argc >= 4){
infile = new char[strlen(argv[1])+1];
strcpy(infile,argv[1]);
outfile = new char[strlen(argv[2])+1];
strcpy(outfile,argv[2]);
key = toupper(*argv[3]);
int key_value = static_cast<int>(key);
if ((key_value < 65) || (key_value > 90)){
//print error message
cerr << "Error: Invalid key used. Goodbye." << endl;
//force the program to terminate due to error
exit(EXIT_FAILURE);
}
input_type = 1;
}
//user tried to run from the dos prompt but made a mistake
else if (argc >=2 && argc <4){
cout << "Usage: cipher10 <inputfile> <outputfile> <key>";
//force the program to terminate due to error
exit(EXIT_FAILURE);
}
else {
//user wants to manually enter information
//get file information
getFileNames(infile, outfile);
input_type = getInputType();
//get the uppercase key
getShiftKey(key);
}
//create the cipher key
createCipher(plaintext, ciphertext, key);
//process file input
if (input_type){
success = encryptText(infile, outfile, plaintext, ciphertext,
enciphered_message);
}
else {
cout << "Use a \'\\' to leave a line in plaintext." << endl
<< "Use a \'\\' to indicate end of message input. " << endl;
success = getMessage(infile, outfile, message_to_cipher, plaintext,
ciphertext);
}
//report success of operation
if (!success){
cerr << "Error: Invalid filename specified. Goodbye." << endl;
}
//delete dynamically allocated memory
delete [] infile;
delete [] outfile;
return (EXIT_SUCCESS);
}//end main()
//-----------------------------------------------------
//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 buffer[256];
int length;
cout << "Enter filename to store/retrieve plaintext message: ";
cin >> buffer;
length = strlen(buffer);
infile_name = new char[length + 1];
strcpy(infile_name, buffer);
cout << "\nEnter filename to store enciphered message: ";
cin >> buffer;
length = strlen(buffer);
outfile_name = new char[length + 1];
strcpy(outfile_name, buffer);
cout << endl;
return;
}//end getFileNames()
//-----------------------------------------------------
//Function: getInputType()
//Parameters: None
//Return Type: int - 0 indicated 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: 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 buffer[256];
char nextChar;
//declare file stream objects
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -