|
@@ -1,32 +1,74 @@
|
|
1
|
+import java.io.*;
|
|
2
|
+import java.util.ArrayList;
|
|
3
|
+import java.util.logging.Logger;
|
|
4
|
+
|
1
|
5
|
import static java.lang.Character.isLowerCase;
|
2
|
6
|
import static java.lang.Character.isUpperCase;
|
3
|
7
|
import static java.lang.Character.toLowerCase;
|
4
|
8
|
|
5
|
|
-public class ROT13 {
|
|
9
|
+public class ROT13 {
|
6
|
10
|
|
7
|
|
- ROT13(Character cs, Character cf) {
|
8
|
|
- }
|
|
11
|
+ private int shifter;
|
9
|
12
|
|
10
|
|
- ROT13() {
|
11
|
|
- }
|
|
13
|
+ public ROT13(Character cs, Character cf) { shifter = cf - cs; }
|
12
|
14
|
|
|
15
|
+ public ROT13() { this.shifter = 13; }
|
13
|
16
|
|
14
|
|
- public String crypt(String text) throws UnsupportedOperationException {
|
|
17
|
+ public String crypt(String text) throws UnsupportedOperationException
|
|
18
|
+ {
|
|
19
|
+ StringBuilder sb = new StringBuilder();
|
15
|
20
|
|
16
|
|
- return "";
|
|
21
|
+ for (int i = 0; i < text.length(); i++)
|
|
22
|
+ {
|
|
23
|
+ sb = (!Character.isLetter(text.charAt(i))) ? sb.append(text.charAt(i)) :
|
|
24
|
+ (Character.isUpperCase(text.charAt(i))) ?
|
|
25
|
+ sb.append((char) (((int) text.charAt(i) + shifter - 65) % 26 + 65)) :
|
|
26
|
+ sb.append((char) (((int) text.charAt(i) + shifter - 97) % 26 + 97));
|
|
27
|
+ }
|
|
28
|
+ return sb.toString();
|
17
|
29
|
}
|
18
|
30
|
|
19
|
|
- public String encrypt(String text) {
|
20
|
|
- return text;
|
|
31
|
+
|
|
32
|
+ public String encrypt(String text) { return crypt(text); }
|
|
33
|
+
|
|
34
|
+ public String decrypt(String text) { return crypt(text); }
|
|
35
|
+
|
|
36
|
+ public static String rotate(String s, Character c)
|
|
37
|
+ {
|
|
38
|
+ String rotated = s.substring(s.indexOf(c)) + s.substring(0, s.indexOf(c)); return rotated;
|
21
|
39
|
}
|
22
|
40
|
|
23
|
|
- public String decrypt(String text) {
|
24
|
|
- return text;
|
|
41
|
+ public String encryptFile(File fileIn, String fileOut)
|
|
42
|
+ {
|
|
43
|
+ String fileInput; StringBuilder builder = new StringBuilder(); BufferedReader reader = null;
|
|
44
|
+
|
|
45
|
+ try { reader = new BufferedReader(new FileReader(fileIn));
|
|
46
|
+
|
|
47
|
+ while ((fileInput = reader.readLine()) != null) { builder.append(crypt(fileInput)); } }
|
|
48
|
+
|
|
49
|
+ catch (FileNotFoundException e) { System.out.println("Unable to read file " + fileIn); }
|
|
50
|
+ catch (IOException e) { e.printStackTrace(); }
|
|
51
|
+
|
|
52
|
+ finally { if (reader != null) { try { reader.close(); } catch (IOException e) { e.printStackTrace(); } } }
|
|
53
|
+
|
|
54
|
+ BufferedWriter writer = null;
|
|
55
|
+
|
|
56
|
+ try { writer = new BufferedWriter(new FileWriter(new File(fileOut))); writer.write(builder.toString()); }
|
|
57
|
+
|
|
58
|
+ catch (IOException e) { e.printStackTrace(); }
|
|
59
|
+
|
|
60
|
+ finally { try { if (writer != null) { writer.close(); } } catch (IOException e) { e.printStackTrace(); }
|
|
61
|
+ }
|
|
62
|
+ return builder.toString();
|
25
|
63
|
}
|
26
|
64
|
|
27
|
|
- public static String rotate(String s, Character c) {
|
28
|
65
|
|
29
|
|
- return "";
|
|
66
|
+ public static void main(String[] args)
|
|
67
|
+ {
|
|
68
|
+ ROT13 rot13 = new ROT13();
|
|
69
|
+ File file = new File("/Users/jasong/Labs/ZCW-ORM-SimpleAccount/.idea/EnCryptMe.txt");
|
|
70
|
+ rot13.encryptFile(file, "/Users/jasong/Labs/ZCW-ORM-SimpleAccount/.idea/IamEncrypted.txt");
|
30
|
71
|
}
|
31
|
72
|
|
|
73
|
+
|
32
|
74
|
}
|