/
cezar.c
123 lines (104 loc) · 3.11 KB
/
cezar.c
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
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
//program deszyfrujacy
void decrypt_file(FILE * crypt, int, FILE * decrypted);
int find_shift(FILE * crypt);
char maximum(int[25]);
int main(void) {
FILE * crypt, *decrypted;
int shift;
//otwieramy dwa pliki
if((crypt = fopen("szyfr_v1.txt", "r")) == NULL) {
perror("Blad otwarcia pliku");
exit(1);
}
decrypted = fopen("deszyfr.txt", "w");
shift = find_shift(crypt); //ustalamy przesuniecie
rewind(crypt); //wracamy na poczatek pliku
decrypt_file(crypt, ahift, decrypted); //odszyfrowujemy
printf("Udanie odszyfrowano i zapisano do pliku!");
fclose(crypt);
fclose(decrypted);
}
void decrypt_file(FILE * crypt, int shift, FILE * decrypted) { //odszufrowanie
int ch;
int i = 1;
while((ch = getc(crypt)) != EOF) {
if((ch >= 'a') && (ch <= 'z')) {
if((ch - shift >= 'a') && (ch - shift <= 'z')) {
ch -= shift;
} else {
ch += 'z' - 'a' - shift + 1;
}
} else if((ch >= 'A') && (ch <= 'Z')) {
if((ch - shift >= 'A') && (ch - shift <= 'Z')) {
ch -= shift;
} else {
ch += 'Z' - 'A' - shift + 1;
}
}
putc(ch, decrypted);
}
}
int find_shift(FILE * crypt) { //szukanie przesuniecia
int i, ch, shift;
int signs[25];
int common[4];
for(ch = 0; ch < 25; ch++) //zerujemy tablice czestosci
signs[ch] = 0;
while((ch = getc(crypt)) != EOF) { //zliczamy liczbe wystapien
if((ch >= 'A') && (ch <= 'Z')) {
ch += ('a' - 'A');
}
if((ch >= 'a') && (ch <= 'z'))
signs[ch - 'a']++;
}
for(i = 0; i < 4; i++) { //ustalamy 4 najczestsze
common[i] = maximum(signs);
signs[common[i]] = 0; //zerujemy znaleziorny, zeby wyszukac kolejny
}
i = 0;
shift = -1;
do {
switch (common[i] - common[i + 1]) { //dzialamy na roznicach
case 8:
shift = common[i] + 'a' - 'i';
break; //8 to i - a
case -8:
shift = common[i];
break; //a -i
case 10:
shift = common[i] + 'a' - 'o';
break; //o - e
case -10:
shift = common[i] + 'a' - 'e';
break; //e - o
case 6:
shift = common[i] + 'a' - 'o';
break; //o - i
case -6:
shift = common[i] + 'a' - 'i';
break; //i - 0
case 14:
shift = common[i] + 'a' - 'o';
break; //o - a
case -14:
shift = common[i];
break; //a - o
}
i++;
} while((shift == -1) && (i < 3));
if(shift < 0)
shift = 26 + shift; //robimy tak zeby przesuniecie bylo dodatnie
return shift;
}
char maximum(int array[25]) { //szukanie maksimum tablicy
int i, index_max, max = 0;
for(i = 0; i < 25; i++)
if(array[i] > max) {
max = array[i];
index_max = i;
}
return index_max; //zwracanie indeksu maksymalnego elementu
}