Ive passed, Pre-Algebra, Honors Algebra I, and Honors Algebra II with an A average. But the mathematics are only a small part of the confusion. The diagrams in the book arent very concise and it doesnt show mathematically the algorithm for most of them. And even if i do figure out how it works implementing it is a whole other story. Anyway i decided to give One-Time Pads a try. Heres my code, i need someone to look it over for me, dont worry its organized (perhaps too organized and commented) I cant get it to work properly. If the letter O gets passed through it when it is decryhpted it becomes a 5. A few other letters do this to me but i cannot decipher (thats a pun) what the problem is. Have a look please. Also ill have a look at the free one form the internet perhaps its more concise. Thanks again!
Code:
#include <iostream>
#include <fstream>
#include <windows.h>
using namespace std;
int main()
{
//infinite loop
while(1)
{
//clear the screen
system("cls");
//title
cout<<"++++++++++++++++++++\n";
cout<<"+ ONE-TIME PAD +\n";
cout<<"+ V. 1.0 +\n";
cout<<"++++++++++++++++++++\n";
cout<<"\n\n";
//menu
cout<<"[1] Encrypt\n";
cout<<"[2] Decrypt\n";
cout<<"[3] Help Info\n";
cout<<"[4] Exit\n\n";
//choose
int mchoice;
cout<<"Choice: ";
cin>>mchoice;
//evaulate
if(mchoice==1)
{
//encrypt
//clear input buffer
cin.ignore();
//init vars
char ptext[56];
char key[56];
char ctext[56];
int gchoice;
//begin process
//get plaintext
cout<<"Plain Text: ";
cin.getline(ptext, 56);
//set ints accordingly
int j = strlen(ptext);
int i = strlen(ptext);
cout<<"\nGenerate Key?(1=Yes, 2=No): ";
cin>>gchoice;
//evaluate
time_t seconds;
time(&seconds);
srand((unsigned int) seconds);
if(gchoice==1)
{
//generate random key
cin.ignore();
for(int k=0; k<j; k++)
{
key[k]=char((rand()%26)+65);
}
cout<<key;
cin.ignore();
}
if(gchoice==2)
{
cin.ignore();
//enter your own key
cout<<"\nKEY: ";
cin.getline(key, 56);
}
//encrypt
for(int l=0; l<i; l++)
{
ctext[l]=((ptext[l]+key[l])%26)+65;
}
cout<<ctext;
cin.ignore();
continue;
}
if(mchoice==2)
{
//decrypt
//clear input buffer
cin.ignore();
//define all Decryption vars
char Dptext[56];
char Dkey[56];
char Dctext[56];
//gather info.
cout<<"Cipher Text: ";
cin.getline(Dctext, 56);
cout<<"Key: ";
cin.getline(Dkey, 56);
//check values
if(strlen(Dctext)==strlen(Dkey))
{
//begin decrypt
}else{
cout<<"Incorrect Alignment of Key/Cipher.";
cin.ignore();
continue;
}
//decrypt
for(int z=0; z<(strlen(Dkey)); z++)
{
Dptext[z]=(((Dctext[z]-Dkey[z])%26)+65);
}
cout<<"\n\nDONE-----\n\n";
cout<<Dptext;
cin.ignore();
continue;
}
if(mchoice==3)
{
//help info
cin.ignore();
ifstream hin("help.txt");
char hh;
while(hin.get(hh))
{
cout<<hh;
}
cin.ignore();
continue;
}
if(mchoice==4)
{
//exit
cout<<"Exitting...";
break;
}
else{
//invalid response
cout<<"\n\nINCORRECT RESPONSE\n\n\a";
cin.ignore();
cin.ignore();
continue;
}
}
return 0;
}
PS-Its not finished yet, im going to implement saving, loading etc. thats why thre fstream and windows headers are included but not implemented... yet. Thanks again!