📄 035-039.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=035-039//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="032-035.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="039-042.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<P><B>Listing 2.4</B> The CIPHER1.CPP program listing.</P>
<!-- CODE //-->
<PRE>
/* PROGRAM :CIPHER1.CPP
All C++ code written by Jonathan Held, January 20, 1998,
using Microsoft's Visual C++ version 5.1.
*/
//standard include files
#include <iostream.h>
//function prototypes
void check(char &);
void findKey(char, const char [], int &);
void formCipher(const char[], char[], int);
void printResults(const char[], const char[]);
//main program
int main(){
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'};
char CIPHERTEXT[26], key;
int key_location;
cout << "Enter UPPERCASE Alphabetic Shift Key: ";
cin >> key;
check(key);
findKey(key, PLAINTEXT, key_location);
formCipher(PLAINTEXT, CIPHERTEXT, key_location);
printResults(PLAINTEXT, CIPHERTEXT);
return (0);
}//end main()
//-----------------------------------------------------
//Function: check()
//Parameters: key - alphabetic key entered by user
//Return type: none
//Purpose: Ensure key is a valid alphabetic character. If it is
//a lowercase letter, this procedure will automatically convert it
//to an uppercase one. This procedure also handles all error
//checking!
//-----------------------------------------------------
void check(char &key){
int key_value;
bool error = false;
do {
//only change came in this line, where I removed the call
//to the function toupper()
key_value = static_cast<int>(key);
if ((key_value < 65) || (key_value >90)){
cerr << "\n\aPlease enter an UPPERCASE Alphabetic Shift Key
(A-Z): ";
cin >> key;
}
else {
error = true;
}
} while (!(error));
key = static_cast<char>(key_value);
cout << "\nKey entered was: " << key << endl;
return;
}//end check()
//-----------------------------------------------------
//Function: findkey()
//Parameters: key - alphabetic key entered by user
// PLAINTEXT - the plaintext alphabet
// key_location - the location of the key in the
// PLAINTEXT alphabet
//Return type: none
//Purpose: Find the key in the PLAINTEXT alphabet. This position
//will be used when we form the cipher.
//-----------------------------------------------------
void findKey(char key, const char PLAINTEXT[],
int &key_location){
for (int ix=0; ix<26; ix++){
if (key == PLAINTEXT[ix]){
key_location = ix + 1;
break;
}
}
return;
}//end findKey()
//-----------------------------------------------------
//Function: formcipher()
//Parameters: PLAINTEXT - the plaintext alphabet
// CIPHERTEXT - the cipher alphabet we will create
// loc - the key location in the plaintext
//Return type: none
//Purpose: Create the ciphertext using modulo arithmetic.
//-----------------------------------------------------
void formCipher(const char PLAINTEXT[], char CIPHERTEXT[],
int loc){
for (int ix=0; ix<26; ix++){
CIPHERTEXT[ix] = PLAINTEXT[(ix + loc) % 26];
}
return;
}//end formCipher()
//-----------------------------------------------------
//Function: printresults()
//Parameters: PLAINTEXT - the plaintext alphabet
// CIPHERTEXT - the ciphertext
//Return type: none
//Purpose: Print the plaintext and corresponding ciphertext based
//on the key the user selected.
//-----------------------------------------------------
void printResults(const char PLAINTEXT[],
const char CIPHERTEXT[]){
cout << "PLAINTEXT: ";
for (int ix=0; ix<26; ix++){
cout << PLAINTEXT[ix] << " ";
}
cout << "\nCIPHERTEXT: ";
for (int jx=0; jx<26; jx++){
cout << CIPHERTEXT[jx] << " ";
}
cout << endl;
return;
}//end printResults()
</PRE>
<!-- END CODE //-->
<P>In examining the C++ program listing for CIPHER1.CPP, note that four functions were used—check, findKey, formCipher, and printResults. Each function is documented within the program listing to include a description of their parameter(s), return type, and purpose. The execution of CIPHER1.CPP will produce the same results as the execution of CIPHER1.BAS, which was previously illustrated in Listing 2.3. The executable version of CIPHER1.CPP is named CIPHER1.EXE and is located under the C directory on the CD-ROM included with this book.
</P><P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="032-035.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="039-042.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
</body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -