📄 266-272.html
字号:
<html><head><TITLE>Learn Encryption Techniques with BASIC and C++:Polyalphabetic Substitution</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=5//-->
<!--PAGES=266-272//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="262-266.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="../ch06/273-276.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H4 ALIGN="LEFT"><A NAME="Heading21"></A><FONT COLOR="#000077">The DPOLY2.CPP Program</FONT></H4>
<P>In concluding this chapter, dual coverage of BASIC and C++ programs will be continued by turning your attention to the C++ version of DPOLY2. That program, which has the filename DPOLY2.CPP, is contained on the CD-ROM in the C directory.
</P>
<P>Listing 5.10 contains a listing of the statements in the header and the main function of DPOLY2.CPP. In examining the listing shown in Listing 5.10 you will note that many functions used in the program POLY2.CPP are reused, such as checkInput, createStream, and createTable. Thus, in this section I will focus your attention on the key difference between the two programs. That difference is primarily in the inclusion of the functions decipher and getFileToDecipher. Listing 5.11 lists the statements in those two functions. Since each function is explicitly documented through the use of comments, I will leave it to you to review the statements for each function.</P>
<P><B>Listing 5.10</B> Header and function main statements in DPOLY2.CPP.</P>
<!-- CODE //-->
<PRE>
/*DPOLY2.CPP
C++ code written by Jonathan Held
Using Microsoft’s Visual C++ Version 5.0
*/
//standard includes
#include<iostream.h>
#include<assert.h>
#include<string.h>
#include<ctype.h>
#include<fstream.h>
#include<stdlib.h>
//constants we will use
const int FIVE = 5, TWENTYFIVE = 25, TWENTYSIX = 26,
TWENTYSEVEN = 27, SIXTYFIVE = 65, NINETY = 90,
NINETYTWO = 92, SIZE = 256, BIGSIZE = 1000;
//function prototypes
bool checkInput(char * &);
void createStream(char *, char []);
void createTable(char [][TWENTYSEVEN], const char [], const char []);
void decipher(ifstream, ofstream, const char[], char[]);
void display(char *);
void formatData(char []);
void getFileNames(char *&, char *&);
void getFileToDecipher(const char[], char[][TWENTYSEVEN]);
int getInputType(void);
int getKeyword(char *&);
bool getMessage(char*, char*, char [], const char[], const
char[][TWENTYSEVEN]);
//----------------------------------------------------------------
//Function: main()
//Parameters: None
//Return Type: int - 0 means program terminated normally
//Purpose: Runs the main part of the program.
//----------------------------------------------------------------
int main()
{
char plaintext[TWENTYSEVEN] = {'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','\0'};
char *ptext_keyword, *ctext_keyword, *infile, *outfile;
//p_stream is used to help form the alphabetic-shifted cipher
//alphabets
char p_stream[TWENTYSIX] = {'\0'};
//c_stream represented the top row of the polyalphabetic ciphertext
//matrix
char c_stream[TWENTYSIX] = {'\0'};
//table is the actual polyalphabetic ciphertext matrix
char table[TWENTYSIX][TWENTYSEVEN];
char enc_msg[SIZE];
int type_of_input;
bool success = false;
cout << "Enter plaintext keyword or keyword phrase in UPPERCASE: ";
getKeyword(ptext_keyword);
cout << "Enter ciphertext keyword or keyword phrase in UPPERCASE: ";
getKeyword(ctext_keyword);
createStream(ptext_keyword, p_stream);
createStream(ctext_keyword, c_stream);
createTable(table, plaintext, c_stream);
type_of_input = getInputType();
if (type_of_input){
//decipher accordingly
getFileToDecipher(p_stream, table);
}
else {
getFileNames(infile, outfile);
getMessage(infile, outfile, enc_msg, p_stream, table);
cout << "Press return to display the deciphered text." << endl;
display(outfile);
}
return 0;
}
</PRE>
<!-- END CODE //-->
<P><B>Listing 5.11</B> Statements in the decipher and getFileToDecipher functions from the program DPOLY2.CPP.</P>
<!-- CODE //-->
<PRE>
//----------------------------------------------------------------
//Function: decipher()
//Parameters: in - the input file we are deciphering
// out - the output file we are writing the deciphered
// text to
// PSTREAM - the plaintext alphabet (used to decipher)
// TBL - the polyalphabetic substitution table
//Return Type: None
//Purpose: Decipher the input file and write the contents to the
//output file specified by the user.
//----------------------------------------------------------------
void decipher(ifstream in, ofstream out, const char PSTREAM[], const
char TBL[][TWENTYSEVEN])
{
char enc_file_data[SIZE];
static int which_cipher_alphabet = 0;
//continue this process until we get to the end of the file
while (in.getline(enc_file_data, SIZE, '\n')){
if (enc_file_data[0] == '/'){
out << enc_file_data << endl;
}
else {
//format the data - i.e., get rid of all spaces
formatData(enc_file_data);
//dump data to file
for (int ix=0; ix<strlen(enc_file_data); ix++){
//used to keep track of what plaintext character
//we are going to use
int jx;
for (jx=0; jx<TWENTYSIX; jx++){
//find where the encrypted data is in the
//ciphertext - this location corresponds to
//the plaintext character location
if (enc_file_data[ix] == TBL[which_cipher_alphabet%TWENTYSIX][jx])
break;
}
//conditionals for grouping by five and inserting
//new lines
if (!(ix%TWENTYFIVE))
out << endl;
if ((ix!=0) && (!(ix%FIVE))){
out << " " << PSTREAM[jx];
}
else {
out << PSTREAM[jx];
}
which_cipher_alphabet++;
}
}
}
return;
}//end decipher()
//----------------------------------------------------------------
///----------------------------------------------------------------
//Function: getFileToDecipher()
//Parameters: PSTREAM - the plaintext stream at the top of the
// polyalphabetic substitution table
// TBL - the polyalphabetic table
//Return Type: None
//Purpose: Prompt the user for the name of the encrypted file
//and the file the user wants to store the decrypted text to.
//----------------------------------------------------------------
void getFileToDecipher(const char PSTREAM[], char TBL[][TWENTYSEVEN])
{
char fileinput[SIZE], fileoutput[SIZE];
cout << "Enter name of file to decipher: ";
cin >> fileinput;
ifstream input(fileinput, ios::in);
if (!(input)){
cerr << "Input file not available. Exiting program." << endl;
exit(EXIT_FAILURE);
}
cout << "Enter name of file for output: ";
cin >> fileoutput;
ofstream output(fileoutput, ios::out);
if (!(output)){
cerr << "Output file not created. Exiting program." << endl;
exit(EXIT_FAILURE);
}
decipher(input, output, PSTREAM, TBL);
//don't forget to close the files
input.close();
output.close();
cout << "\nDeciphered text is in " << fileoutput << endl;
cout << "\nPress return to display deciphered text." << endl;
cin.get();
cin.get();
display(fileoutput);
return;
}//end getFileToDecipher()
//----------------------------------------------------------------
</PRE>
<!-- END CODE //-->
<P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="262-266.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="../ch06/273-276.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
</body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -