1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
|
#include <iostream>
#include <cstring>
#include <fstream>
// read and write from or to files || ofstream = write on files ifstream = read from files
using namespace std;
void encrypt();
void decrypt();
void savefunction();
char save;
string plaintext, key, encrypt2(string plaintext,string key), decrypt2(string plaintext,string key);;
int choice, word_pos, key_pos;
int main(){
do {
cout<<"\n\n*****ENIGMA*****\n";
cout<<"[1] Encrypt \n[2] Decrypt \n[3] Exit \n";
cout<<"Enter choice: ";
cin>>choice;
switch (choice){
case 1: //encryption
encrypt();
break;
case 2: //decryption
decrypt();
break;
case 3: //exit
break;
}
} while (choice!=3);
return 0;
system("pause");
}
encrypt2 (plaintext, key){
string encrypted = "";//wala pay sulod na result
for(int x = 0, o = 0; x < plaintext.length() ; x++){ //mag loop cya kung unsa katas.a ang word pakapin pod ang key
if(plaintext[x]==' '){ // pag space ang makita
x++;//x++ mag skip og read sa letter kay space man unsa.on man nato kung space paasa kaau ning space
encrypted += " ";//pag space as usual butangan niya og space e aron mag bulag cla space lang jud
}
word_pos = get_location(plaintext[x]); //charot kuhaon nya nag posistion sa letter "word[x]"
key_pos = get_location(key[o]); // kuhaon pud nya ang position sa key kung pila ang idungag
encrypted += get_letter(word_pos + key_pos) ; //kani naline kay kuha.on nya ang letter tapos i sumpay sa encrypted na variable
/* then is sumpay lang to nya kuntahay kay 'b' ang letter then ang key kay 'c'
since ang position sa 'b' kay 1 then ang posistion sa 'c' kay 2 (Note dli na i count gkan pag 'a' so mag start ang 1 sa b)
then i-add lang nimo para ma encrypt cya 1 + 2 = 3 since ang 3 kay 'd' mao lang to .
*/
o++;//kani next letter sa key napod daw ang basahun nya
if(o > key.length()-1)//pag manubra na kuntahay ang length sa love kay 4 man d back to zero napod (note since ge array man pag consider ang string d minus 1 ta aron d mulapas)
o = 0;//reset nya ang o parabakik tag sugod
}
return encrypted;//tapos na done bitches i return nato nya ng ecnryoted na word
}
void encrypt(){
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
char yes;string temp;//char yes para sa yes or no then kanang string temp temporary lang pang hold sa first word
cout<<"\n\n*****ENCRYPTION*****\n\n";
cout<<"Enter plain text to encrypt: ";
cin>>temp;//pang hold lang ni
getline(cin, plaintext);
//cin.ignore();
plaintext += temp;//dili na ka ka type
cout<<"Enter encryption key: ";
getline(cin, key);
//cin.ignore();
encrypt2(plaintext, key);
cout<<"Encrypted cipher text: "<<encrypt2(plaintext,key)<<endl; //call nya ang function with the parameters na
cout<<"Save cipher text to file? (y/n): ";
cin>>save;
savefunction();
}
decrypt2(plaintext,key){
string decrypted = "";//parehas ra didtos pikas
for(int x = 0, o = 0; x < plaintext.length() ; x++){//parehas ra didtos pikas
/*if(plaintext[x]==' '){ // pag space ang makita
x++;//x++ mag skip og read sa letter kay space man unsa.on man nato kung space paasa kaau ning space
decrypted += " ";//pag space as usual butangan niya og space e aron mag bulag cla space lang jud
}*/
word_pos = get_location(plaintext[x]);//parehas ra didtos pikas
key_pos = get_location(key[o]);//parehas ra didtos pikas
decrypted += get_letter(word_pos - key_pos+26);//parehas ra didto sa pikas plus 26 lang para sa negative vaules minus lang para atras ba balikan ang dapat balikan
o++;//kani next letter sa key napod daw ang basahun nya
if(o > key.length()-1)//parehas ra didtos pikas
o = 0;//parehas ra didtos pikas
}
return decrypted;//parehas ra didtos pikas
}
void decrypt(){
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXxXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
string temp;//temp string
cout<<"\n\n*****DECRYPTION*****\n\n";
cout<<"Enter plain text to decrypt: ";//same ra sa taas
cin>>temp;//pang hold lang ni
getline(cin, plaintext);//kay pag geline
//cin.ignore();
plaintext += temp;//dili na ka ka type
cout<<"Enter decryption key: ";
getline(cin,key);//same ra sa taas
//cin.ignore();
string decrypt2(plaintext,key);
cout<<"Encrypted cipher text: "<<decrypt2(plaintext,key)<<endl;//call nya ang function
}
void savefunction(){
if (save=='Y' || save=='y'){
ofstream file;
string filename;
cout<<"Enter file name: ";
getline(cin, filename); // get user file name
filename += ".txt"; // add .txt after file name
file.open(filename.c_str(), std::ios_base::app);
// file<< --> place the cypher text here to the text file
file.close();
cout<<filename<<" saved successfully!";
}
else if (save=='N' || save=='n'){
cout<<"Cipher text not saved. \n";
}
}
|