-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathmain.cpp
More file actions
209 lines (170 loc) · 4.26 KB
/
main.cpp
File metadata and controls
209 lines (170 loc) · 4.26 KB
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*
Encryption and Decryption Program (Written in C++)
By Sudeep Acharya
http://twitter.com/acsudeep
Encryption and decryption is done by the method of letter shifting.
*/
#include<iostream>
#include<iomanip>
#include<string.h>
#include<dos.h>
using namespace std;
class EncryptionDecryption {
private:
string outputstring;
string inputstring;
string encpass;
string decpass;
int enckey;
int deckey;
int temp;
char tempc;
void doEncryption(); //function to do Encryption
void doDecryption(); //function to do Decryption
public:
EncryptionDecryption(); //initializer Default Constructor
EncryptionDecryption(string); //parameterized constructor
void setInputString(string); //
~EncryptionDecryption();
int checkEncPass(string);
int checkDecPass(string);
void getOutputString(); //get the encrypted string
};
//Use this function to do Encryption
void EncryptionDecryption::doEncryption() {
for(int i=0;i<inputstring.size();i++) {
temp = (int)inputstring[i];
temp = temp + enckey;
tempc = (char)temp;
outputstring += tempc;
}
}
//End of encryption function
//Use this function to do Decryption
void EncryptionDecryption::doDecryption() {
for(int i=0;i<inputstring.size();i++) {
temp = (int)inputstring[i];
temp = temp + deckey;
tempc = (char)temp;
outputstring += tempc;
}
}
//End of decryption function
void EncryptionDecryption::getOutputString() {
outputstring += '\0';
cout << "The Encrypted/Decrypted string is" <<" : "<<outputstring;
outputstring="";
}
//first check the password
int EncryptionDecryption::checkEncPass(string pass) {
//check password before encrypting
if(pass!=encpass)
{
cout << endl << "The password you entered is Incorrect the, Encryption will not proceed.\nTry Again \n";
return 0;
}
else {
doEncryption();
return 1;
}
}
//first check the password
int EncryptionDecryption::checkDecPass(string pass) {
//check password before encrypting
if(pass!=decpass)
{
cout << endl << "The password you entered is Incorrect the, Decryption will not proceed.\nTry Again \n";
return 0;
}
else {
doDecryption();
return 1;
}
}
//Initializer
EncryptionDecryption::EncryptionDecryption() {
inputstring = "";
outputstring = "";
encpass= "111"; //this is the password
decpass= "000";
enckey= 5; //this is the key
deckey= -1 * enckey;
temp= 0;
tempc='\0';
}
EncryptionDecryption::EncryptionDecryption(string e) {
inputstring = e;
}
void EncryptionDecryption::setInputString(string f) {
inputstring=f;
}
EncryptionDecryption::~EncryptionDecryption() {
}
//The program begins here
int main()
{
string inputstring;
string pass;
char ch;
bool quit=false;
EncryptionDecryption O1; //creating object
//Which operation you want to perform
cout << "\n******************************************";
cout << "\nMenu\n";
cout << "1. Encryption\n";
cout << "2. Decryption\n";
cout << "3. Quit\n";
cout << "******************************************";
while(!quit)
{
quit = false;
cout << "\nEnter the choice to perform the task";
cout << "\nEnter 1 or 2 or 3: ";
cin >> ch;
switch(ch)
{
case '1':
cout << "Enter word to Encrypt: ";
cin.ignore();
getline(cin, inputstring);
O1.setInputString(inputstring); //set the encryption text
cout << "Enter the password to proceed Encryption: ";
getline(cin, pass);
if(O1.checkEncPass(pass)==1) { //check encryption password and then do encryption if the password is correct
O1.getOutputString();
}
else {
break;
}
break;
case '2':
cout << "Enter word to Decrypt: ";
cin.ignore();
getline(cin, inputstring);
O1.setInputString(inputstring); //set the decryption string
cout << "Enter the password to proceed Decryption: ";
getline(cin, pass);
if(O1.checkDecPass(pass)==1) { //check decryption password and then do decryption if the password is correct
O1.getOutputString();
}
else {
break;
}
break;
case '3':
cout<< "Exiting the program";
quit=true;
break;
case '\n':
case '\t':
case ' ':
break;
default:
cout<<"Wrong Choice";
quit=false;
break;
}
cout << "\n\n";
}
return 0;
}