-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
78 lines (48 loc) · 1.32 KB
/
main.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
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int decrypt(int a, int b, int input);
int encrypt(int a, int b, int input);
int main(){
string filename;
char letter;
cout << "Please enter a file to open: ";
cin >> filename;
ifstream readFile;
readFile.open(filename);
if (!readFile.is_open()){
cout << "Error! " << filename << "could not be opened!\n";
exit(0);
}
ofstream writeFile;
char selection;
cout << "Encrypt (e) or decrypt (d)?\n";
cin >> selection;
if (selection == 'e'){
writeFile.open("EC_"+ filename);
while (readFile.get(letter)){
if (letter < 32 || letter > 127) writeFile << letter;
else writeFile << char(encrypt(7,24,(letter - 32)) + 32);
}
writeFile.close();
}
else {
writeFile.open("DC_"+ filename);
while (readFile.get(letter)){
if (letter < 32 || letter > 127) writeFile << letter;
else writeFile << char(decrypt(68,-24,(letter - 32)) + 32);
}
writeFile.close();
}
}
int decrypt(int a, int b, int input){
int c = (a * (input + b));
while (c < 0){
c += 95;
}
return c % 95;
}
int encrypt(int a, int b, int input){
return ((a * input) + b) % 95;
}