-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSource.cpp
More file actions
105 lines (93 loc) · 2.12 KB
/
Source.cpp
File metadata and controls
105 lines (93 loc) · 2.12 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
//Encryption-Decryption Sample Library
// [32, 126] Available Range
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
const int MAX = 126;
const int MIN = 32;
const int RANGE = MAX - MIN; // Range = MaxValue - MinValue
const std::string KEY = "abc";
//Each key outputs a different encrypted message
std::string EncryptFile(std::string const& f);
//
void NewEncryptedValues(std::vector <char> & u, std::vector <int> const& v);
//
void ShowNewCode(std::vector <char> const& u);
//
void DecryptVigenereFile(std::string const & f, std::string & d);
//
int main() {
std::string fileName, text, plaintext;
std::cin >> fileName;
text = EncryptFile(fileName);
DecryptVigenereFile(text, plaintext);
std::cout << plaintext << '\n';
system("pause");
return 0;
}
void DecryptVigenereFile(std::string const & f, std::string & d) {
std::vector <int> v;
int keySize = KEY.size();
d = f;
for (int i = 0; i < f.size(); ++i) {
v.push_back(KEY[i % keySize] % RANGE);
}
for (int i = 0; i < f.size(); ++i) {
d[i] = d[i] - v[i];
if (d[i] < MIN)
{
d[i] = MAX - (MIN - d[i]);
}
}
}
std::string EncryptFile(std::string const& f) {
std::ifstream file;
std::string aux2 = "";
int i = 0, keySize = KEY.size();
char aux;
std::vector <int> v;
std::vector <char> u;
file.open(f);
if (!file.is_open())
{
std::cout << "No se pudo abrir el archivo \n";
}
else
{
file.get(aux);
while (!file.eof())
{
u.push_back(aux);
v.push_back(KEY[i % keySize] % RANGE);
file.get(aux);
++i;
}
NewEncryptedValues(u, v);
ShowNewCode(u);
for (int i = 0; i < u.size(); ++i) {
aux2 += u[i];
}
file.close();
}
return aux2;
}
void NewEncryptedValues(std::vector <char> & u, std::vector <int> const& v) {
int newCode;
for (int i = 0; i < v.size(); ++i)
{
newCode = u[i] + v[i];
if (newCode > MAX)
{
newCode = MIN + newCode % MAX;
}
u[i] = char(newCode);
}
}
void ShowNewCode(std::vector <char> const& u) {
for (int i = 0; i < u.size(); ++i)
{
std::cout << u[i];
}
std::cout << '\n';
}