Help me Repair it???Thanks you very much!!
**********************************
#include<iostream>
#include<fstream>
#include<string>
#include<cmath>
using namespace std;
void banner();
int menu();
string encrypt(string, int);
string decrypt(string, int);
int main()
{
/******* Variable Declarations *****************
Purpose: Declare variables for main function
Initialize all variables
***********************************************/
string plaintext = "";
string ciphertext = "";
ifstream infile ("plain.txt");
ofstream outfile ("cipher.txt");
ifstream indata ("cipher.txt");
ofstream outdata ("plain.txt");
int method = 0; //determines if output will be stdout or file
int length = 0;
int key = 0; //shift number [0 -25]
int choice = 0; //program menu option is stored here
banner();//welcome Screen
while (choice != 5)
{
choice = menu(); //Get the user's selection number
switch (choice)
{
/**** Encrypt from Keyboard ***********************
input: Input from the keyboard
************************************************/
case 1:
cout<<"Please select a key size [0-25] ";
cin>> key;
cout<<"Write the message"<<endl;
cin.ignore();//bug fix for getline function
getline(cin,plaintext); //Get text from user
ciphertext = encrypt(plaintext,key); //Submit data to encryption function
cout<<"Encrypting";
for(int k=0; k < 200; k++){} //custom delay method
//cout<<".";
//for(int k=0; k < 200; k++){}
//cout<<".";
//for(int k=0; k < 200; k++){}
cout<<"."<<endl;
system("CLS");//system call to clear screen
cout<<"What output method do you want?"<<endl;;
cout<<"1. Output to monitor"<<endl;
cout<<"2. Output to file"<<endl;
cin >> method;
system("CLS");
if (method == 1)
{
cout<<"The Generated ciphertext: ";
cout<<ciphertext<<endl;
}
else if (method == 2)
{
if (outfile.is_open())
{
outfile << ciphertext;
outfile.close();
}
}
else
cout<<"invalid choice"<<endl;
break;
/**** Encrypt from a File ***********************
input: Input from plain.txt
************************************************/
case 2:
cout<<"Please select a key size [0-25] ";
cin>> key;
if (!infile.is_open())
{ cout << "Error opening file"; exit (1); }
else
getline(infile,plaintext); //Get text from plain.txt
ciphertext = encrypt(plaintext, key); //Submit data to encryption function
cout<<"What output method do you want?"<<endl;
cout<<"1. Output to monitor"<<endl;
cout<<"2. Output to file"<<endl;
cin >> method;
if (method == 1)
{
cout<<ciphertext<<endl;
}
else if (method == 2)
{
if (outfile.is_open())
{
outfile << ciphertext;
outfile.close();
}
}
else
cout<<"invalid choice"<<endl;
break;
/**** Decrypt from the keyboard *****************
input: Input from keyboard
************************************************/
case 3:
cout<<"Please select the key size used to encrypt the message [0-25] ";
cin>> key;
cout<<"Write the message"<<endl;
cin.ignore();
getline(cin,ciphertext);//Get text from user
plaintext = decrypt(ciphertext,key); //Submit data to Decryption function
cout<<"Decrypting";
for(int k=0; k < 200000000; k++){}
cout<<".";
for(int k=0; k < 200000000; k++){}
cout<<".";
for(int k=0; k < 200000000; k++){}
cout<<"."<<endl;
system("CLS");
cout<<"The original message: ";
cout<<plaintext<<endl;
break;
/**** Decrypt from a File ***********************
input: Input from cipher.txt
************************************************/
case 4:
cout<<"Please select a key size [0-25] ";
cin>> key;
if (!indata.is_open())
{ cout << "Error opening file"; exit (1); }
else
getline(indata,ciphertext); //Get text from file
plaintext = decrypt(ciphertext, key); //Submit data to Decryption function
cout<<"What output method do you want?"<<endl;
cout<<"1. Output to monitor"<<endl;
cout<<"2. Output to file"<<endl;
cin >> method;
system("CLS");
if (method == 1)
{
cout<<"The original message: ";
cout<<plaintext<<endl;
}
else if (method == 2)
{
if (outdata.is_open())
{
outdata << plaintext;
outdata.close();
}
}
else
cout<<"invalid choice"<<endl;
break;
/**** Cryptanalysis from the keyboard ***********
input: From the Keyboard
************************************************/
case 5:
cout<<"Write the ciphertext you found"<<endl;
cin.ignore();
getline(cin,ciphertext);
cout<<"What output method do you want?"<<endl;
cout<<"1. Output to monitor"<<endl;
cout<<"2. Output to file"<<endl;
cin >> method;
system("CLS");
if (method == 1)
{
key = 1;
//Brute force the shift key by incrementing to 26
while (key < 26)
{
plaintext = decrypt(ciphertext,key);
cout<<"discovered plaintext: "<<plaintext<<endl;
key++;
}
}
else if (method == 2)
{
if (outdata.is_open())
{
key = 1;
while (key < 26)
{
plaintext = decrypt(ciphertext,key);
outdata << plaintext;
key++;
}
outdata.close();
}
}
else
cout<<"invalid choice"<<endl;
break;
/**** Cryptanalysis from a file ****************
input: cipher.txt in current directory
************************************************/
case 6:
if (!indata.is_open())
{ cout << "Error opening file"; exit (1); }
else
getline(indata,ciphertext);
plaintext = decrypt(ciphertext, key);
cout<<"What output method do you want?"<<endl;
cout<<"1. Output to monitor"<<endl;
cout<<"2. Output to file"<<endl;
cin >> method;
if (method == 1)
{
key = 1;
//Brute force the shift key by incrementing to 26
while (key < 26)
{
plaintext = decrypt(ciphertext,key);
cout<<"discovered plaintext: "<<plaintext<<endl;
key++;
}
}
else if (method == 2)
{
if (outdata.is_open())
{
key = 1;
while (key < 26)
{
plaintext = decrypt(ciphertext,key);
outdata << plaintext;
key++;
}
outdata.close();
}
}
else
cout<<"invalid choice"<<endl;
break;
case 7:
exit (0);
default:
cout<<"invalid choice"<<endl;
}
}
return 0;
}
/************ Encrypt Function *****************
------------------------------------------------
Purpose: Encrypt string using shift key
Input: String with plaintext, integer key
return: Ciphertext in a string
*************************************************/
string encrypt(string msgc, int ekey)
{
int k = 0;
int len = msgc.length();
int tmp[100];
for (k = 0; k < len; k++)
{
atoi(&msgc[k]); //convert char into integer value
tmp[k] = msgc[k] - 97;//subtract ascii value from 97 to get (0 - 25)
tmp[k] = (tmp[k] + ekey) % 26; //encrypt and put into integer array
msgc[k] = tmp[k] + 65;//add ascii value to make uppercase letters
}
return msgc;
}
/************ Decrypt Function *****************
------------------------------------------------
Purpose: Decrypt string using shift key
Input: String with ciphertext, integer key
return: Plaintext in a string
*************************************************/
string decrypt(string ciphmsg, int dkey)
{
int d = 0;
int dlen = ciphmsg.length(); //get length of string
int dtmp[1000];
for (d = 0; d < dlen; d++)
{
atoi(&ciphmsg[d]);//convert char into integer value
dtmp[d] = ciphmsg[d] - 65; //subtract ascii value from 65 to get (0 - 25)
dtmp[d] = dtmp[d] - dkey; //decrypt and put into integer array
if (dtmp[d] < 0)
{
dtmp[d] = abs(dtmp[d]); //obtain absolute value
dtmp[d] = 26 - dtmp[d]; //account for alphabet rollover
ciphmsg[d] = dtmp[d] + 97; //back to lowercase ascii value
}
else
dtmp[d] = dtmp[d] % 26;
ciphmsg[d] = dtmp[d] + 97; //back to lowercase ascii value
}
return ciphmsg;
}
void banner()
{
cout<<" **********************************************"<<endl;
cout<<" * Caesar Cipher Encryption Program*"<<endl;
cout<<" * Written By David J. Tabacco*"<<endl;
cout<<" * September 8, 2004 *"<<endl;
cout<<" **"<<endl;
cout<<" **"<<endl;
cout<<" **"<<endl;
cout<<" **********************************************"<<endl;
for(int k=0; k < 1000000000; k++){}
system("CLS");
}
//this is not an 80's DOS program
int menu()
{
int menuselect;
cout<<" -----------------------------------------------------"<<endl;
cout<<" / cccc A EEEE SSSS A RRRR/ |"<<endl;
cout<<" / cc A A EE SS A A R R/ |"<<endl;
cout<<" / cc AAAAA EEE SS AAAAA RRR/ |"<<endl;
cout<<"/ cccc AAA AAA EEEE SSSS AAA AAA RR RR / |"<<endl;
cout<<"---------------------------------------------------| |"<<endl;
cout<<"|Please Make a Selection | |"<<endl;
cout<<"| | |"<<endl;
cout<<"| | |"<<endl;
cout<<"| Encryption Functions| |"<<endl;
cout<<"| 1: Encrypt the message (Keyboard source) | |"<<endl;
cout<<"| 2: Encrypt the message (File source)| |"<<endl;
cout<<"| 3: Decrypt the message (Keyboard source) | |"<<endl;
cout<<"| 4: Decrypt the message (File source)| |"<<endl;
cout<<"| | |"<<endl;
cout<<"| Cryptanalysis | |"<<endl;
cout<<"| 5. break a message (keyboard source)| |"<<endl;
cout<<"| 6. break a message (file source)| /"<<endl;
cout<<"| | /"<<endl;
cout<<"| Exit| /"<<endl;
cout<<"| 7. Quit | /"<<endl;
cout<<"----------------------------------------------------"<<endl;
cin>> menuselect;
return menuselect;
}
}
**********************************
Attached File(s)
-
CEASAR_cipher1.txt (14.17K)
Number of downloads: 106
This post has been edited by girl_beautiful: 22 June 2007 - 01:56 AM

New Topic/Question
Reply




MultiQuote


|