-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcaesar.cpp
More file actions
131 lines (106 loc) · 3.11 KB
/
caesar.cpp
File metadata and controls
131 lines (106 loc) · 3.11 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
#include <iostream>
#include <fstream>
#include <string>
#include <cmath>
#include <cctype>
using namespace std;
string lexoTekstin(const string& filename) {
ifstream file(filename);
if(!file) {
cerr << "Nuk mund te hapet fajlli: " << filename << endl;
return "";
}
string text, line;
while(getline(file, line)) {
for(char ch : line){
text += tolower(ch);
}
}
return text;
}
void llogaritFrekuencat(const string& text, double freq[26]){
int counts[26] = {0};
int total = 0;
for (char ch : text){
if (isalpha(ch)){
counts[ch - 'a']++;
total++;
}
}
for(int i = 0; i < 26; i++){
freq[i] = (total > 0) ? (double)counts[i] / total : 0.0;
}
}
string dekriptoCezar(const string& text, int key) {
string result;
for (char ch : text) {
if (isalpha(ch)) {
result += (ch - 'a' - key + 26) % 26 + 'a';
} else {
result += ch;
}
}
return result;
}
double ndryshimiTotal(const double f1[26], const double f2[26]) {
double total = 0.0;
for (int i = 0; i < 26; i++) {
total += fabs(f1[i] - f2[i]);
}
return total;
}
void sulmoCezarin(const string& trainingFile, const string& encryptedFile){
string trainingText = lexoTekstin(trainingFile);
string encryptedText = lexoTekstin(encryptedFile);
double modelFreq[26];
llogaritFrekuencat(trainingText, modelFreq);
int bestKey = 0;
double minDiff = 1e9;
string decryptedMessage;
for(int key = 0; key < 26 ; key++){
string candidate = dekriptoCezar(encryptedText, key);
double candidateFreq[26];
llogaritFrekuencat(candidate, candidateFreq);
double diff = ndryshimiTotal(modelFreq, candidateFreq);
if(diff < minDiff){
minDiff = diff;
bestKey = key;
decryptedMessage = candidate;
}
}
cout << "\nÇelesi me me shume gjasa: " << bestKey << endl;
cout << "\nMesazhi i dekriptuar: " << decryptedMessage << endl;
}
int main(){
string trainingFile = "training.txt";
string encryptedFile = "secret.txt";
int choice;
while (true){
cout<<"Do you want to: \n";
cout << "1. Use the existing content in 'secret.txt'\n";
cout << "2. Write a new encrypted message to 'secret.txt'\n";
cout << "Enter your choice (1 or 2): ";
cin >> choice;
cin.ignore();
if (choice == 1) {
break;
} else if (choice == 2) {
cout << "Write your encrypted message below:\n";
string newMessage;
getline(cin, newMessage);
ofstream outFile(encryptedFile);
if (!outFile) {
cerr << "Could not write to file: " << encryptedFile << endl;
return 1;
}
outFile << newMessage;
outFile.close();
cout << "Encrypted message saved to '" << encryptedFile << "'\n";
break;
} else {
cout << "\n Incorrect input! Please enter 1 or 2.\n\n";
}
}
sulmoCezarin(trainingFile, encryptedFile);
return 0;
}