-
Notifications
You must be signed in to change notification settings - Fork 0
/
encrypt.cpp
executable file
·164 lines (145 loc) · 3.19 KB
/
encrypt.cpp
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
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
using namespace std;
int menu();
void executeChoice(int choice,vector<string>& o,string file);
void convert(string& s);
void printEncryptRot(vector<string>& o, string file);
void printDecryptRot(vector<string>& o, string file);
string getFile(); //Takes in the file name from the user
vector<string> getInfo(string file);//Places orignal text into a vector
void rotEncrypt(int key,vector<string>& o);//Encrypts using rot method
void rotDecrypt(int key,vector<string>& o);//Decrypts using rot method
int main()
{
vector<string> original;
vector<string> encRot;
vector<string> encCrypt;
string file = getFile(); //File the user wants to be encrypted or decrypted
original = getInfo(file);//Original.txt
int choice = menu();
executeChoice(choice,original,file);
return 0;
}
int menu()
{
int choice;
cout<<"Enter 0 for rot encryption"<<endl
<<"Enter 1 for rot decryption"<<endl
<<"Your choice: ";
cin>>choice;
return choice;
}
void executeChoice(int choice,vector<string>& o,string file)
{
int key;
string crypto;
switch(choice)
{
case 0:
cout<<"Please enter a key for your rotation: ";
cin>>key;
rotEncrypt(key,o);
printEncryptRot(o, file);
cout<<"Your file has been encrypted"<<endl;
break;
case 1:
cout<<"Please enter a key for your decryption: ";
cin>>key;
rotDecrypt(key,o);
printDecryptRot(o,file);
cout<<"Your file has been decrypted"<<endl;
break;
}
}
string getFile()
{
string file;
cout<<"Enter the file to be encrypted (Format: file.txt): ";
cin>>file;
return file;
}
vector<string> getInfo(string file)
{
string s;
vector<string> o;
ifstream fin;
fin.open(file, ios::in);
if(!fin)
cout<<"There was an error opening the file"<<endl
<<"Make sure the file is in the same folder as the program"<<endl;
fin>>s;
while(fin)
{
convert(s);
o.push_back(s);
fin>>s;
}
fin.close();
return o;
}
void convert(string& s)
{
for(int i = 0; i < s.size();i++)
s[i] = tolower(s[i]);
}
void printDecryptRot(vector<string>& o, string file)
{
ofstream fout;
fout.open(file);
for(int i = 0;i < o.size();i++)
{
fout<<o[i]<<" ";
}
fout.close();
}
void rotEncrypt(int key,vector<string>& o)
{
string s;
int newchar;
for(int i = 0;i < o.size(); i++)
{
s = o[i];
for(int j = 0;j < s.size();j++)
{
if(s[j] == '?' || s[j] == '.' || s[j] == ',' || s[j] == '!' || s[j] == ';' || s[j] == ':')
break;
s[j] += key;
if(s[j] > 'z')
s[j] -= 26;
}
o[i] = s;
}
}
void rotDecrypt(int key,vector<string>& o)
{
string s;
int newchar;
char x;
for(int i = 0;i < o.size();i++)
{
s = o[i];
for(int j = 0;j < s.size();j++)
{
x = s[j];
if(s[j] == '?' || s[j] == '.' || s[j] == ',' || s[j] == '!' || s[j] == ';' || s[j] == ':')
break;
s[j] -= key;
if(s[j] < 'a')
s[j] += 26;
}
o[i] = s;
}
}
void printEncryptRot(vector<string>& o, string file)
{
ofstream fout;
fout.open(file);
for(int i = 0;i < o.size();i++)
{
fout<<o[i]<<" ";
}
fout.close();
}