286-291.html
来自「这个是密码学的经典著作」· HTML 代码 · 共 293 行
HTML
293 行
<html><head><TITLE>Learn Encryption Techniques with BASIC and C++:Using Random Numbers</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=6//-->
<!--PAGES=286-291//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="283-286.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="291-294.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<P><B>Listing 6.6</B> The RANDOM2.CPP program listing and its repeated execution using different random seed numbers.</P>
<!-- CODE //-->
<PRE>
/*
random2.cpp
C++ code written by Jonathan Held on April 28, 1998,
using Microsoft Visual C++ version 5.0
*/
#include <iostream.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
//function prototypes
bool checkInput(char *&);
char* encipher(char *&);
void formatData(char []);
void getMessage(char *&);
unsigned int getSeed(void);
void gotit(const char [], char *&, const int, int);
const int BUFFER_SIZE = 256;
//----------------------------------------------------------------
//Function: main()
//Parameters: None
//Return Type: int - 0 if program terminated normally
//Purpose: driver that makes calls to various procedures defined
//in random1.cpp
//----------------------------------------------------------------
int main(){
unsigned int seed;
char *message, *result;
while (seed = getSeed()) {
srand(seed);
getMessage(message);
cout << "\nYou entered: " << message << endl;
result = encipher(message);
cout << "Enciphered message is " << result << endl << endl;
delete message;
}
return 0;
}//end main()
//----------------------------------------------------------------
//Function: checkInput()
//Parameters: input - the message 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 < 65) || (char_value > 90)){
error = true;
cerr << "\aYou entered an invalid message!" << endl << endl;
break;
}
}
if (count == 0){
cerr << "\aYou entered an invalid message!" << endl << endl;
error = true;
}
return error;
}//end checkInput()
//----------------------------------------------------------------
//Function: encipher()
//Parameters: inp - the user's input that will be enciphered
//Return Type: char * - a pointer to the encrypted string
//Purpose: returns a pointer to the encrypted text
//----------------------------------------------------------------
char* encipher(char *&inp){
//yet another, and easier way to represent the plaintext
//array
const char plaintext[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int index = 0;
char *encrypted = new char[strlen(inp)+1];
assert(encrypted);
for (int ix=0; ix < strlen(inp); ix++){
for (int jx=0; jx <26; jx++){
if (plaintext[jx] == inp[ix]){
gotit(plaintext, encrypted, jx, index++);
break;
}
}
}
//don't forget to insert the null terminator
encrypted[strlen(inp)] = '\0';
return encrypted;
}//end encipher();
//----------------------------------------------------------------
//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: getMessage()
//Parameters: input - user's one-line message
//Return Type: None
//Purpose: Gets a one-line message from the user.
//----------------------------------------------------------------
void getMessage(char *&input){
char buffer[BUFFER_SIZE] = {'\0'};
bool error = false;
do{
cout << "Enter a one-line message (no spaces) in UPPERCASE: ";
cin.getline(buffer, BUFFER_SIZE, '\n');
assert(input = new char[strlen(buffer) + 1]);
strcpy(input, buffer);
formatData(input);
error = checkInput(input);
if (error)
delete input;
} while (error);
return;
}//getMessage()
//----------------------------------------------------------------
//Function: getSeed()
//Parameters: none
//Return Type: unsigned int - the seed the client entered
//Purpose: gets the seed for the random number generator. If the
//user enters a negative number, it causes the seed to underflow to
//a very high non-negative number. If the user enters an
//alphanumeric sequence, e.g., "A123", then seed_Number is set to
//zero.
//----------------------------------------------------------------
unsigned getSeed(){
unsigned int seed_Number;
cout << "Enter seed, 0 (or non-digit) to terminate: ";
cin >> seed_Number;
cin.ignore();
return seed_Number;
}//end getSeed()
//----------------------------------------------------------------
//Function: gotit()
//Parameters: p_text - the plaintext array, i.e., characters "A-Z"
// enc_msg - a pointer to the message that will be encoded
// loc - the location of each encoded character in the
// plaintext array
//Return Type: None
//Purpose: encodes the user's input using random numbers
//----------------------------------------------------------------
void gotit(const char p_text[], char *&enc_msg, int loc, int index){
int random_num, z;
do {
random_num = rand() % 100;
} while (random_num > 25);
z = (loc + random_num) % 26;
enc_msg[index] = p_text[z];
return;
}//end gotit()
//end file random2.cpp
A:\c>RANDOM2
Enter seed, 0 (or non-digit) to terminate: 5
Enter a one-line message (no spaces) in UPPERCASE: FIREFREDNOW
You entered: FIREFREDNOW
Enciphered message is TDYLWCANCKC
Enter seed, 0 (or non-digit) to terminate: 8
Enter a one-line message (no spaces) in UPPERCASE: FIREFREDNOW
You entered: FIREFREDNOW
Enciphered message is QLEGOKYPGRV
Enter seed, 0 (or non-digit) to terminate: 51
Enter a one-line message (no spaces) in UPPERCASE: FIREFREDNOW
You entered: FIREFREDNOW
Enciphered message is KIYOSXAWQTW
Enter seed, 0 (or non-digit) to terminate:
</PRE>
<!-- END CODE //-->
<P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="283-286.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="291-294.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
</body></html>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?