-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDecoding.java
More file actions
77 lines (68 loc) · 2.66 KB
/
Decoding.java
File metadata and controls
77 lines (68 loc) · 2.66 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
import java.io.IOException;
import java.nio.CharBuffer;
public class Decoding {
static String pathSource = "TextAfterEncryption.txt";
static String pathDestination = "TextAfterEncryptionAndDecryption.txt";
public static void decoding(int key) {
CharBuffer ch = WorkWithFile.read(pathSource);
StringBuilder output = new StringBuilder();
for (int i = 0; i < ch.length(); i++) {
char c = ch.charAt(i);
if (Alphabet.SIMBOLS.indexOf(c) != -1) {
int newIndex = getNewIndex(key, c);
output.append(Alphabet.SIMBOLS.charAt(newIndex));
} else {
output.append(c);
}
}
try {
WorkWithFile.write(pathDestination, output);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void brutForce() {
CharBuffer ch = WorkWithFile.read(pathSource);
for (int key = 1; key <= 20; key++) {
StringBuilder output = new StringBuilder();
for (int i = 0; i < ch.length(); i++) {
char c = ch.charAt(i);
if (Alphabet.SIMBOLS.indexOf(c) != -1) {
int newIndex = getNewIndex(key, c);
output.append(Alphabet.SIMBOLS.charAt(newIndex));
} else {
output.append(c);
}
}
if (isDecryptionValid(String.valueOf(output))) {
try {
WorkWithFile.write(pathDestination, output);
} catch (IOException e) {
e.printStackTrace();
}
break;
}
}
}
private static int getNewIndex(int key, char c) {
return (Alphabet.SIMBOLS.indexOf(c) - key + Alphabet.SIMBOLS.length()) % Alphabet.SIMBOLS.length();
}
private static boolean isDecryptionValid(String output) {
String[] searchWordsAndSigns = {" в ", " на ", " без ", " до ", " для ", " за ", " через ", " над ", " по ", " из ", " у ", " около ",
" под ", " о ", " про ", " к ", " перед ", " при ", " с ", " между ", ", ", ". ", "! ", "? "};
int minFoundWords = 20;
int countFoundWords = 0;
for (String word : searchWordsAndSigns) {
int index = 0;
while (index != -1) {
index = output.indexOf(word, index);
if (index == -1) {
break;
}
countFoundWords++;
index += word.length();
}
}
return countFoundWords >= minFoundWords;
}
}