-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParser
More file actions
181 lines (140 loc) · 5.73 KB
/
Parser
File metadata and controls
181 lines (140 loc) · 5.73 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package ie.gmit.dip;
import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
public class Parser {
private String[] charPairs = new String[100];
public static boolean isValid(String url){
try {
new URL(url).toURI();
return true;
}
catch (Exception e) {
return false;
}
}
public String[] parseEncrypt (String resource, boolean isUrl, char[][] newArrayofArrays) {
BufferedReader br = null;
try {
if(isUrl) {
// buffer reader is assigned the value of an openinputstream if its a url
br = new BufferedReader(new InputStreamReader(new URL(resource).openStream()));
}else {
// buffer reader is assigned the value of an fileinputstream if its a file
br = new BufferedReader(new InputStreamReader(new FileInputStream(resource)));
}
FileWriter fw = new FileWriter("encrypted.txt");
String line = null;
while ((line = br.readLine()) != null){
// any characters that are not alphanumeric are removed and replaced with "" and all characters are made uppercase to match the matrix array.
line = line.replaceAll("[^A-Za-z0-9]", "").toUpperCase();
// to ensure an even length of line an X has been added to the line when the line is uneven.
if ((line.length() % 2) != 0) {
line = line + "X";
}
// a string array of pairs of characters is the created from line.
String[] bigrams = line.split("(?<=\\G.{2})");
// a new FourSquareCipher object is created
FourSquareCipher cipher = new FourSquareCipher(newArrayofArrays);
// the string array bigrams is passed to the encrypt method of the FourSquareCipher class line by line.
String encryptedtext = cipher.encrypt(bigrams);
// the encrypted text is then added to the file per line
fw.write(encryptedtext + "\n");
}
// the input streams and most importantly the writer streams are closed.
br.close();
fw.flush();
fw.close();
} catch (MalformedURLException e) {
System.out.println("an incorrect URL has been encountered, please use a correctly formatted url");
} catch (IOException e) {
System.out.println("File not found "+ resource +" did you forget the file extension?");
}
return charPairs;
}
public String[] parseDecrypt (String resource, boolean isUrl, char[][] newArrayofArrays) {
BufferedReader br = null;
try {
if(isUrl) {
br = new BufferedReader(new InputStreamReader(new URL(resource).openStream()));
}else {
br = new BufferedReader(new InputStreamReader(new FileInputStream(resource)));
}
FileWriter fw = new FileWriter("decrypted.txt");
String line = null;
while ((line = br.readLine()) != null){
// any characters that are not alphanumeric are removed and replaced with "" and all characters are made uppercase to match matrix.
line = line.replaceAll("[^A-Za-z0-9]", "").toUpperCase();
// to ensure an even length of line an X has been added to the line when the line is uneven.
if ((line.length() % 2) != 0) {
line = line + "X";
}
// a string array of pairs of characters is the created from line.
String[] bigrams = line.split("(?<=\\G.{2})");
FourSquareCipher cipher = new FourSquareCipher(newArrayofArrays);
String encryptedtext = cipher.decrypt(bigrams);
fw.write(encryptedtext + "\n");
}
br.close();
fw.flush();
fw.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return charPairs;
}
public char[] parseKey(String parse){
// passed string is converted to upper case
parse = parse.toUpperCase();
// anything that is not A-Z in upper case is removed and any j's are replaced with i's
parse = parse.replaceAll("[^A-Z]", "").replace("J", "I");;
String alphabet = "ABCDEFGHIKLMNOPQRSTUVWXYZ";
// the whole alphabet is added to the end of the parse string
String keyword = parse + alphabet;
// this keyword is then stripped of any duplicate letters while keeping its original order.
keyword = removeDuplicates(keyword);
// the stripped down string is then returned as a char[] array.
return keyword.toCharArray();
}
static String removeDuplicates(String s) {
// a new stringbuilder is created
StringBuilder noDupes = new StringBuilder();
// the string is traversed
for (int i = 0; i < s.length(); i++) {
String si = s.substring(i, i + 1);
// where the substring of one letter is not part of the stringbuilder then append it to the stringbuilder
if (noDupes.indexOf(si) == -1) {
noDupes.append(si);
}
}
// return the string without duplicates
return noDupes.toString();
}
/*
public void addBigrams (String[] s) {
for (int i = 0; i < s.length; i++) {
append(s[i]);
}
}
public void append(String s) {
// if there is not enough capacity in the char pairs array then the array length is doubled(? this is probably a little bit like napalm for ants in the garden but it'll get the job done) in order to accommodate the new array.
ensureCapacity();
// each string in the bigrams string array is then added to the charPairs array.
charPairs[index] = s;
//System.out.println(charPairs[index]); - used in testing the code
index++;
//System.out.println(index);- used in testing the code
}
private void ensureCapacity() {
if (index >= charPairs.length) {
String[] temp = new String[charPairs.length * 2];
for (int i = 0; i < charPairs.length; i++) {
temp[i] = charPairs[i];
}
charPairs = temp;
}
}
*/
}