|
@@ -1,32 +1,110 @@
|
|
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) {
|
|
65
|
+ class EncryptOffset {
|
|
66
|
+
|
|
67
|
+ private int offset;
|
|
68
|
+ private boolean isOffset;
|
28
|
69
|
|
29
|
|
- return "";
|
|
70
|
+ public EncryptOffset(Character offset) {
|
|
71
|
+ super();
|
|
72
|
+ this.offset = offset;
|
|
73
|
+ isOffset = false;
|
|
74
|
+ }
|
|
75
|
+
|
|
76
|
+ public String encryptWithOffset(String text) {
|
|
77
|
+ StringBuilder sb = new StringBuilder();
|
|
78
|
+
|
|
79
|
+ for (int i = 0; i < text.length(); i++)
|
|
80
|
+ {
|
|
81
|
+ if (isOffset = false) {
|
|
82
|
+ sb = (!Character.isLetter(text.charAt(i))) ? sb.append(text.charAt(i)) :
|
|
83
|
+ (Character.isUpperCase(text.charAt(i))) ?
|
|
84
|
+ sb.append((char) (((int) text.charAt(i) + shifter - 65) % 26 + 65)) :
|
|
85
|
+ sb.append((char) (((int) text.charAt(i) + shifter - 97) % 26 + 97));
|
|
86
|
+ isOffset = true;
|
|
87
|
+ } else if (isOffset = true){
|
|
88
|
+ sb = (!Character.isLetter(text.charAt(i))) ? sb.append(text.charAt(i)) :
|
|
89
|
+ (Character.isUpperCase(text.charAt(i))) ?
|
|
90
|
+ sb.append((char) ((((int) text.charAt(i) + shifter - 65) - offset) % 26 + 65)) :
|
|
91
|
+ sb.append((char) ((((int) text.charAt(i) + shifter - 97) - offset) % 26 + 97));
|
|
92
|
+ isOffset = false;
|
|
93
|
+ }
|
|
94
|
+ }
|
|
95
|
+ return sb.toString();
|
|
96
|
+ }
|
30
|
97
|
}
|
31
|
98
|
|
|
99
|
+ public static void main(String[] args)
|
|
100
|
+ {
|
|
101
|
+ ROT13 rot13 = new ROT13();
|
|
102
|
+ File file = new File("/Users/jasong/Labs/ZCW-ORM-SimpleAccount/.idea/EnCryptMe.txt");
|
|
103
|
+ // rot13.encryptFile(file, "/Users/jasong/Labs/ZCW-ORM-SimpleAccount/.idea/IamEncrypted.txt");
|
|
104
|
+ ROT13.EncryptOffset encryptOffset = rot13.new EncryptOffset('b');
|
|
105
|
+ System.out.println(encryptOffset.encryptWithOffset
|
|
106
|
+ ("This file needs to be read and encrypted. It can also be used for file reading practice!"));
|
|
107
|
+ }
|
|
108
|
+
|
|
109
|
+
|
32
|
110
|
}
|