-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCaesarCipher.java
More file actions
79 lines (73 loc) · 2.64 KB
/
CaesarCipher.java
File metadata and controls
79 lines (73 loc) · 2.64 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
import java.util.HashMap;
import java.util.Map;
public class CaesarCipher {
public static final char[] ALPHABET = {'а', 'б', 'в', 'г', 'д', 'е', 'ж', 'з',
'и','к', 'л', 'м', 'н', 'о', 'п', 'р', 'с', 'т', 'у', 'ф', 'х', 'ц', 'ч', 'ш', 'щ',
'ъ', 'ы', 'ь', 'э', 'я', '.', ',', '«', '»', '"', '\'', ':', '!', '?', ' '};
private static final Map<Character, Integer> alphabetIndex = new HashMap<>();
static {
for (int i = 0; i < ALPHABET.length; i++) {
alphabetIndex.put(ALPHABET[i], i);
}
}
public String encrypt(String text, int key) {
StringBuilder sb = new StringBuilder();
for (char c : text.toCharArray()) {
Integer idx = alphabetIndex.get(c);
if (idx == null) {
sb.append(c);
} else {
int newIdx = (idx + key) % ALPHABET.length;
if (newIdx < 0) newIdx += ALPHABET.length;
sb.append(ALPHABET[newIdx]);
}
}
return sb.toString();
}
public String decrypt(String text, int key) {
return encrypt(text, -key);
}
public String bruteForce(String text) {
StringBuilder result = new StringBuilder();
for (int k = 1; k < ALPHABET.length; k++) {
result.append("Ключ: ").append(k).append("\n");
result.append(decrypt(text, k)).append("\n----------------------\n");
}
return result.toString();
}
public int statisticalAnalysis(String encrypted, String sample) {
int bestKey = 0;
double minDiff = Double.MAX_VALUE;
double[] sampleFreq = getCharFrequencies(sample);
for (int k = 0; k < ALPHABET.length; k++) {
String decrypted = decrypt(encrypted, k);
double[] decryptedFreq = getCharFrequencies(decrypted);
double diff = 0;
for (int i = 0; i < ALPHABET.length; i++) {
diff += Math.pow(sampleFreq[i] - decryptedFreq[i], 2);
}
if (diff < minDiff) {
minDiff = diff;
bestKey = k;
}
}
return bestKey;
}
private double[] getCharFrequencies(String text) {
double[] freq = new double[ALPHABET.length];
int total = 0;
for (char c : text.toCharArray()) {
Integer idx = alphabetIndex.get(c);
if (idx != null) {
freq[idx]++;
total++;
}
}
if (total > 0) {
for (int i = 0; i < freq.length; i++) {
freq[i] /= total;
}
}
return freq;
}
}