-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
234 lines (189 loc) · 7.58 KB
/
main.cpp
File metadata and controls
234 lines (189 loc) · 7.58 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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <unordered_map>
#include <string>
#include <algorithm>
#include <vector>
#include <fstream>
#include <cctype>
bool isValidAlphanumeric(const std::string &input) {
for (char c : input)
if (!std::isalnum(static_cast<unsigned char>(c))) return false;
return true;
}
bool validatePlaintext(const std::string &text) {
if (text.length() > 100) {
std::cerr << "Text exceeds 100 characters.\n";
return false;
}
if (!isValidAlphanumeric(text)) {
std::cerr << "Text must be alphanumeric only (A-Z, a-z, 0-9).\n";
return false;
}
return true;
}
bool validateKey(const std::string &key) {
if (key == "-") return true;
if (key.length() < 4 || key.length() > 16) {
std::cerr << "The key must be between 4 and 16 characters.\n";
return false;
}
if (!isValidAlphanumeric(key)) {
std::cerr << "The key must be alphanumeric only.\n";
return false;
}
return true;
}
int generateRandom(int min, int max){
return min + (std::rand() % (max - min + 1));
}
std::string createKey(std::string plainText, char characters[47]){
std::string temporaryKey;
int maxLength = std::min(16, (int)plainText.length());
for (int i=0; i < maxLength; i++){
temporaryKey.push_back(characters[generateRandom(0,46)]);
}
std::cout << "Generated key: " << temporaryKey << std::endl;
return temporaryKey;
}
std::string createCipher(std::string key, std::unordered_map<char,int> charToIndex, std::string plaintext, char characters[47]){
while (key.length() < plaintext.length()) key += key;
key = key.substr(0, plaintext.length());
std::string cipher;
for (size_t i = 0; i < plaintext.length(); i++){
int sum = charToIndex[plaintext[i]] + charToIndex[key[i]];
if (sum > 46) sum -= 47;
cipher.push_back(characters[sum]);
}
std::cout << "Cipher: " << cipher << std::endl;
return cipher;
}
std::string transposition(const std::string &key, const std::string &cipher, int index) {
std::string transposedCipher;
int keyLength = (index == 1) ? key.length() / 2 : key.length();
if (keyLength == 0) keyLength = 1;
int rows = (cipher.length() + keyLength - 1) / keyLength;
std::vector<std::vector<char>> matrix(rows, std::vector<char>(keyLength, ':'));
int idx = 0;
for (int r = 0; r < rows; r++) {
for (int c = 0; c < keyLength; c++) {
if (idx < cipher.size())
matrix[r][c] = cipher[idx++];
}
}
for (int c = 0; c < keyLength; c++)
for (int r = 0; r < rows; r++)
transposedCipher.push_back(matrix[r][c]);
std::cout << "transposedCipher: " << transposedCipher << std::endl;
return transposedCipher;
}
std::string shiftKey(std::string key) {
int len = key.length();
int half = len/2;
std::string newKey = key.substr(half) + key.substr(0,half);
std::cout << "shifted key: " << newKey << std::endl;
return newKey;
}
std::string xorCipher(const std::string &text, std::string key) {
while(key.length() < text.length()) key += key;
key = key.substr(0,text.length());
std::string cipher;
cipher.resize(text.size());
for(size_t i=0; i<text.size(); i++) {
unsigned char a = static_cast<unsigned char>(text[i]);
unsigned char b = static_cast<unsigned char>(key[i]);
unsigned char x = a ^ b;
cipher[i] = static_cast<char>(x);
}
std::cout << "xor: " << cipher << std::endl;
return cipher;
}
std::string decryptCipher(std::string key, std::unordered_map<char,int> charToIndex, std::string cipher, char characters[47]) {
while(key.length() < cipher.length()) key += key;
key = key.substr(0,cipher.length());
std::string plaintext;
for(size_t i=0; i<cipher.size(); i++){
int diff = charToIndex[cipher[i]] - charToIndex[key[i]];
if(diff<0) diff+=47;
plaintext.push_back(characters[diff]);
}
std::cout << "Cipher-PlainText: " << plaintext << std::endl;
return plaintext;
}
std::string inverseTransposition(const std::string &key, const std::string &transposedCipher, int index){
std::string cipher;
int keyLength = (index==1)? key.length()/2 : key.length();
if(keyLength==0) keyLength=1;
int rows = (transposedCipher.length() + keyLength - 1) / keyLength;
int fullCols = transposedCipher.length() % keyLength;
if (fullCols == 0) fullCols = keyLength;
std::vector<int> colHeights(keyLength, rows);
for (int c = fullCols; c < keyLength; c++) {
colHeights[c] = rows - 1;
}
std::vector<std::vector<char>> matrix(rows, std::vector<char>(keyLength, ':'));
int idx = 0;
for (int c = 0; c < keyLength; c++) {
for (int r = 0; r < colHeights[c]; r++) {
if (idx < transposedCipher.size())
matrix[r][c] = transposedCipher[idx++];
}
}
for (int r = 0; r < rows; r++)
for (int c = 0; c < keyLength; c++)
cipher.push_back(matrix[r][c]);
while(!cipher.empty() && cipher.back() == ':')
cipher.pop_back();
std::cout << "Inverse-transposedCipher: " << cipher << std::endl;
return cipher;
}
int main(){
std::srand(static_cast<unsigned int>(std::time(nullptr)));
char characters[47] = {
'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X',
'Y','Z','.','_','-','?','!','*','/','#','%','$','&','0','1','2','3','4','5','6','7','8','9'
};
std::unordered_map<char,int> charToIndex;
for(int i=0;i<47;i++) charToIndex[characters[i]]=i;
std::ifstream infile("input.txt");
if(!infile){ std::cerr << "No se pudo abrir input.txt\n"; return 1; }
std::string plaintext, key;
std::getline(infile, plaintext);
std::getline(infile, key);
infile.close();
std::transform(plaintext.begin(), plaintext.end(), plaintext.begin(), ::toupper);
std::transform(key.begin(), key.end(), key.begin(), ::toupper);
if(!validatePlaintext(plaintext) || !validateKey(key)) return 1;
key = shiftKey(key);
std::string cipher = createCipher(key,charToIndex,plaintext,characters);
cipher = transposition(key,cipher,0);
cipher = transposition(key,cipher,0);
cipher = transposition(key,cipher,1);
cipher = xorCipher(cipher,key);
std::ofstream outfile("cifrado.txt", std::ios::binary);
if(!outfile){ std::cerr << "No se pudo crear cifrado.txt\n"; return 1; }
outfile << cipher;
outfile.close();
std::ifstream keyfile("decryption_key.txt");
if(!keyfile){ std::cerr << "No se pudo abrir decryption_key.txt\n"; return 1; }
std::string decryptKey;
std::getline(keyfile, decryptKey);
keyfile.close();
std::ifstream cipherfile("cifrado.txt", std::ios::binary);
if(!cipherfile){ std::cerr << "No se pudo abrir cifrado.txt\n"; return 1; }
std::string loadedCipher((std::istreambuf_iterator<char>(cipherfile)), std::istreambuf_iterator<char>());
cipherfile.close();
decryptKey = shiftKey(decryptKey);
std::string detrans = xorCipher(loadedCipher, decryptKey);
detrans = inverseTransposition(decryptKey,detrans,1);
detrans = inverseTransposition(decryptKey,detrans,0);
detrans = inverseTransposition(decryptKey,detrans,0);
std::string decryptedPlaintext = decryptCipher(decryptKey,charToIndex,detrans,characters);
std::ofstream decfile("decryptedtext.txt");
if(!decfile){ std::cerr << "No se pudo crear decryptedtext.txt\n"; return 1; }
decfile << decryptedPlaintext;
decfile.close();
std::cout << "Cifrado y descifrado completados.\n";
return 0;
}