📄 161-183.html
字号:
<html><head><TITLE>Learn Encryption Techniques with BASIC and C++:Transposition-based Monoalphabetic 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=4//-->
<!--PAGES=161-183//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="152-161.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="184-187.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H4 ALIGN="CENTER"><A NAME="Heading17"></A><FONT COLOR="#000077">The CIPHERTR.CPP Program</FONT></H4>
<P>Listing 4.5 lists the statements in the C++ program CIPHERTR.CPP, which provides a similar capability to CIPHERTR.BAS. That is, this program enciphers a message based upon the use of a simple transposition mixed-sequence alphabet and an alphabet shift key. Although the listing is lengthy and the program is included on the CD-ROM, it is included in its entirety here as doing so provides you with the ability to note the relationship between functions and the order by which they are invoked. Due to the extensive documentation contained within the program listing, it is left for you to examine the listing and the remarks which illustrate how the program operates.
</P>
<P><B>Listing 4.5</B> The CIPHERTR.CPP program listing.</P>
<!-- CODE //-->
<PRE>
/*
ciphertr.cpp
C++ code written by Jonathan Held
using Microsoft Visual C++, version 5.0, on March 23, 1998.
*/
//standard include files
#include<iostream.h>
#include<assert.h>
#include<string.h>
#include<ctype.h>
#include<fstream.h>
//function prototypes
bool checkInput(char * &);
void createCipherStream(char *, char[]);
void createMatrix(char ** &, const char [], const int, const int);
void display(char *);
bool encryptText(char *, char *, const char [], const char[], char []);
void deleteMatrix(char **&, const int, const int);
int findLowestValue(int *&, const int);
void formatData(char []);
char* formCipheredMessage(const char[], const char [], char []);
void getFileNames(char *&, char *&);
int getInputType(void);
int getKeyword(char *&);
bool getMessage(char*, char*, char [], const char[], const char[]);
void getShiftKey(char &);
void groupBUFFER(ofstream, int);
void numericExtraction(char **&, char [], const int, const int);
void printCipherToFile(ofstream, char[]);
void printMatrix(char ** &, const int, const int);
void reviseCipherStream(char **&, char [], const int, const int);
void shiftKeyword(char [], const char);
void simpleExtraction(char **&, char [], const int, const int);
int typeOfExtraction(void);
//constants we will use
const int FIVE = 5, TWENTYFIVE = 25, TWENTYSIX = 26,
TWENTYSEVEN = 27, SIXTYFIVE = 65, NINETY = 90,
NINETYTWO = 92, SIZE = 256, BIGSIZE = 1000;
char BUFFER[BIGSIZE] = {'\0'};
//----------------------------------------------------------------
//Function: main()
//Parameters: None
//Return Type: int - 0 execution is normal, 1 abnormal termination
//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 cipherStream[TWENTYSEVEN] = {'\0'};
char message_to_cipher[SIZE], enciphered_message[SIZE];
char ** cMatrix;
char *keyword, key, *infile, *outfile;
int rows, columns, input_type;
bool success;
//get the keyword we are going to use, determine number of rows
//and columns of our matrix
columns = getKeyword(keyword);
rows = TWENTYSIX/columns;
//integer division requires that we check and see if there is a
//remainder; if so, we need to add one extra row to our matrix
if (TWENTYSIX%columns){
rows++;
}
//create the initial ciphertext stream
createCipherStream(keyword, cipherStream);
//echo to the user what we have
cout << "Plaintext-based alphabet is: " << plaintext << endl
<< "Keyword-based alphabet is: " << cipherStream << endl << endl;
//insert the stream into our matrix
createMatrix(cMatrix, cipherStream, rows, columns);
reviseCipherStream(cMatrix, cipherStream, rows, columns);
getShiftKey(key);
shiftKeyword(cipherStream, key);
getFileNames(infile, outfile);
//query the user as to whether we are deciphering keyboard
//or file input
input_type = getInputType();
//process file input
if(input_type){
success = encryptText(infile, outfile, plaintext, cipherStream,
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,
cipherStream);
}
//report success of operation
if (!success){
cerr << "Error: Invalid filename specified. Goodbye." << endl;
}
else {
cout << "Press return to display resulting enciphered message."
<< endl;
//get the newlines off the current input stream
cin.get();
if(input_type == 1){
cin.get();
}
display(outfile);
}
//delete the dynamically allocated matrix
deleteMatrix(cMatrix, rows, columns);
delete keyword;
return (!success);
}//end main()
//----------------------------------------------------------------
//Function: checkInput()
//Parameters: input - the keyword the user entered
//Return Type: bool - true if the input string contains an error,
// false otherwise
//Purpose: Checks the user's keyword for invalid characters.
//----------------------------------------------------------------
bool checkInput(char * &input)
{
bool error = false;
int count = strlen(input);
for (int ix=0; ix<count; ix++){
int char_value = static_cast<int>(*(input+ix));
//determine if the user did not enter an uppercase character
if ((char_value < SIXTYFIVE) || (char_value > NINETY)){
error = true;
cerr << "You entered an invalid keyword!" << endl << endl;
break;
}
}
return error;
}//end checkInput()
//----------------------------------------------------------------
//Function: createCipherStream()
//Parameters: input - the keyword the user entered
// cipher - the keyword alphabet that will be constructed
//Return Type: None
//Purpose: Creates a preliminary cipher stream that will be used to
//form the cipher matrix.
//----------------------------------------------------------------
void createCipherStream(char *input, char stream[])
{
bool used[TWENTYSIX];
int index = 0,
count = strlen(input);
//no characters are initially used
for(int ix=0; ix<TWENTYSIX; ix++){
used[ix] = false;
}
//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;
*(stream+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])){
*(stream+index++) = static_cast<char>(SIXTYFIVE+kx);
}
}
return;
}//end createCipherStream()
//----------------------------------------------------------------
//Function: createMatrix()
//Parameters: matrix - the matrix we are going to create
// CSTREAM - the initial cipher stream based on the
// user's keyword
// ROWS - the number of rows in the matrix
// COLS - the number of columns in the matrix
//Return Type: None
//Purpose: Creates a numeric key transposed matrix, identical to
//figure 4.4.
//----------------------------------------------------------------
void createMatrix(char ** &matrix, const char CSTREAM[], const int ROWS,
const int COLS)
{
int count = 0;
//dynamically allocate memory for the RxC matrix
//we use assert to ensure that memory was allocated;
//if not, then the program will terminate abnormally
assert(matrix = new char*[ROWS]);
for(int ix=0; ix<ROWS; ix++){
*(matrix + ix) = new char[COLS];
}
//fill in the matrix
for(int jx=0; jx<ROWS; jx++){
for (int kx=0; kx<COLS; kx++){
//we only want to enter a character into the matrix
//twenty-six times - most of the time we allocate
//a matrix larger than what we will need to use; when
//we have more than 26 characters, we simply insert a
//null terminator into the matrix
if(count < TWENTYSIX)
*(*(matrix+jx)+kx) = CSTREAM[count++];
else
*(*(matrix+jx)+kx) = '\0';
}
}
return;
}//end createMatrix()
//----------------------------------------------------------------
//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] == '/'){
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -