-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDecrypt.java
More file actions
executable file
·84 lines (76 loc) · 2.41 KB
/
Decrypt.java
File metadata and controls
executable file
·84 lines (76 loc) · 2.41 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
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
/**
* Abstract Class for Decryption of ciphertext encoded with a Cipher.
*
* @author David W. Arnold
* @version 09/11/2019
*/
public abstract class Decrypt
{
protected String tess;
protected String ciphertext;
protected File cipherFile;
protected char[] charAlphabet;
public Decrypt(File tessFile, File cipherFile) throws IOException
{
tess = new BufferedReader(new FileReader(tessFile)).readLine();
ciphertext = new BufferedReader(new FileReader(cipherFile)).readLine();
this.cipherFile = cipherFile;
setCharAlphabet(tessFile);
}
protected int findIndex(char[] arr, int t)
{
if (arr == null) {
return -1;
}
int len = arr.length;
int i = 0;
while (i < len) {
if (arr[i] == t) {
return i;
}
i++;
}
return -1;
}
protected String getExercise(File file)
{
String cipherFileName = file.getName();
return cipherFileName.substring(1, cipherFileName.length() - 4);
}
protected int getTotalOccurrences(String plaintext, ArrayList<String> english_common_pairs_and_repeats)
{
int totalOccurrences = 0;
totalOccurrences = getTotalOccurrencesHelper(plaintext, totalOccurrences, english_common_pairs_and_repeats);
return totalOccurrences;
}
protected int getTotalOccurrencesHelper(String newTmpPT, int totalOccurrences, ArrayList<String> english_trigraphs)
{
for (String trigraph : english_trigraphs) {
int lastIndex = 0;
while (lastIndex != -1) {
lastIndex = newTmpPT.indexOf(trigraph, lastIndex);
if (lastIndex != -1) {
totalOccurrences++;
lastIndex += trigraph.length();
}
}
}
return totalOccurrences;
}
private void setCharAlphabet(File tess)
{
String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
if (tess.getName().equals("tess26.txt")) {
charAlphabet = alphabet.toCharArray();
} else if (tess.getName().equals("tess27.txt")) {
charAlphabet = (alphabet + "|").toCharArray();
} else {
charAlphabet = alphabet.toCharArray();
}
}
}