|
|
@@ -1,32 +1,157 @@
|
|
|
1
|
+import java.io.*;
|
|
|
2
|
+
|
|
1
|
3
|
import static java.lang.Character.isLowerCase;
|
|
2
|
4
|
import static java.lang.Character.isUpperCase;
|
|
3
|
5
|
import static java.lang.Character.toLowerCase;
|
|
4
|
6
|
|
|
5
|
7
|
public class ROT13 {
|
|
6
|
8
|
|
|
|
9
|
+ int shift;
|
|
7
|
10
|
ROT13(Character cs, Character cf) {
|
|
|
11
|
+ shift = cf - cs;
|
|
8
|
12
|
}
|
|
9
|
13
|
|
|
10
|
14
|
ROT13() {
|
|
11
|
15
|
}
|
|
12
|
16
|
|
|
13
|
|
-
|
|
14
|
17
|
public String crypt(String text) throws UnsupportedOperationException {
|
|
15
|
|
-
|
|
16
|
|
- return "";
|
|
|
18
|
+ StringBuilder sb = new StringBuilder();
|
|
|
19
|
+ for(int i=0;i<text.length();i++){
|
|
|
20
|
+ if((text.charAt(i) >= 'a' && text.charAt(i) <= 'm') || (text.charAt(i) >= 'A' && text.charAt(i) <= 'M')){
|
|
|
21
|
+ sb.append((char)(text.charAt(i) + 13));
|
|
|
22
|
+ }
|
|
|
23
|
+ else if((text.charAt(i) >= 'n' && text.charAt(i) <= 'z') || (text.charAt(i) >= 'N' && text.charAt(i) <= 'Z')){
|
|
|
24
|
+ sb.append((char)(text.charAt(i) - 13));
|
|
|
25
|
+ }
|
|
|
26
|
+ else
|
|
|
27
|
+ sb.append(text.charAt(i));
|
|
|
28
|
+ }
|
|
|
29
|
+ return sb.toString();
|
|
17
|
30
|
}
|
|
18
|
31
|
|
|
19
|
32
|
public String encrypt(String text) {
|
|
20
|
|
- return text;
|
|
|
33
|
+ String encrypted = "";
|
|
|
34
|
+ //Individual characters of a String are numbered from 0 until n-1 where n is the
|
|
|
35
|
+ //length of the string
|
|
|
36
|
+ //So what we want to repeat is "get the i character of the String, encrypt it, and build
|
|
|
37
|
+ //it onto the String named encrypted" We want to do that for each value of i
|
|
|
38
|
+ //To actually encrypt, we need to worry about "wraparound" issues around z
|
|
|
39
|
+ for (int i = 0; i < text.length(); i++) {
|
|
|
40
|
+ //Check if it is a letter. If it isn't, then we don't have to do anything anyway
|
|
|
41
|
+ if( (text.charAt(i) >= 'A' && text.charAt(i) <= 'Z') ||
|
|
|
42
|
+ (text.charAt(i) >= 'a' && text.charAt(i) <= 'z'))
|
|
|
43
|
+ {
|
|
|
44
|
+ //Check if there was "wraparound" For example z + 3 should give us c
|
|
|
45
|
+ //In this case, the way to get this result is by subtracting 23 instead of adding 3
|
|
|
46
|
+ if (((char)(text.charAt(i) + shift) > 'Z' &&
|
|
|
47
|
+ text.charAt(i) <= 'Z')
|
|
|
48
|
+ || ((char)(text.charAt(i) + shift) > 'z'
|
|
|
49
|
+ && text.charAt(i) <= 'z'))
|
|
|
50
|
+
|
|
|
51
|
+ {
|
|
|
52
|
+ //add the next character which is encrypted to the String
|
|
|
53
|
+ encrypted = encrypted + (char)(text.charAt(i) + shift - 26);
|
|
|
54
|
+ }
|
|
|
55
|
+ else
|
|
|
56
|
+ {
|
|
|
57
|
+ //add the next character which is encrypted to the String
|
|
|
58
|
+ encrypted = encrypted + (char)(text.charAt(i) + shift);
|
|
|
59
|
+ }
|
|
|
60
|
+ }
|
|
|
61
|
+ else
|
|
|
62
|
+ {
|
|
|
63
|
+ //add the original character to the Stringx
|
|
|
64
|
+ encrypted = encrypted + text.charAt(i);
|
|
|
65
|
+ }
|
|
|
66
|
+ }
|
|
|
67
|
+
|
|
|
68
|
+ //return the resulting String
|
|
|
69
|
+ return encrypted;
|
|
21
|
70
|
}
|
|
22
|
71
|
|
|
23
|
72
|
public String decrypt(String text) {
|
|
24
|
|
- return text;
|
|
|
73
|
+ String decrypted = "";
|
|
|
74
|
+ for (int i = 0; i < text.length(); i++) {
|
|
|
75
|
+ if( (text.charAt(i) >= 'A' && text.charAt(i) <= 'Z') ||
|
|
|
76
|
+ (text.charAt(i) >= 'a' && text.charAt(i) <= 'z'))
|
|
|
77
|
+ {
|
|
|
78
|
+ if (((char)(text.charAt(i) - shift) < 'A' &&
|
|
|
79
|
+ text.charAt(i) >= 'A')
|
|
|
80
|
+ || ((char)(text.charAt(i) - shift) < 'a'
|
|
|
81
|
+ && text.charAt(i) >= 'a'))
|
|
|
82
|
+
|
|
|
83
|
+ {
|
|
|
84
|
+ decrypted = decrypted + (char)(text.charAt(i) - shift + 26);
|
|
|
85
|
+ }
|
|
|
86
|
+ else {
|
|
|
87
|
+ decrypted = decrypted + (char)(text.charAt(i) - shift);
|
|
|
88
|
+ }
|
|
|
89
|
+ }
|
|
|
90
|
+ else
|
|
|
91
|
+ {
|
|
|
92
|
+ decrypted = decrypted + text.charAt(i);
|
|
|
93
|
+ }
|
|
|
94
|
+ }
|
|
|
95
|
+ return decrypted;
|
|
25
|
96
|
}
|
|
26
|
97
|
|
|
27
|
98
|
public static String rotate(String s, Character c) {
|
|
|
99
|
+ int shift = c - s.charAt(0);
|
|
|
100
|
+ return s.substring(shift) + s.substring(0,shift);
|
|
|
101
|
+ }
|
|
|
102
|
+
|
|
|
103
|
+ public String readFile(String fileName){
|
|
28
|
104
|
|
|
29
|
|
- return "";
|
|
|
105
|
+ StringBuilder sb = new StringBuilder();
|
|
|
106
|
+ // This will reference one line at a time
|
|
|
107
|
+ String line = null;
|
|
|
108
|
+
|
|
|
109
|
+ try {
|
|
|
110
|
+ // FileReader reads text files in the default encoding.
|
|
|
111
|
+ FileReader fileReader =
|
|
|
112
|
+ new FileReader(fileName);
|
|
|
113
|
+
|
|
|
114
|
+ // Always wrap FileReader in BufferedReader.
|
|
|
115
|
+ BufferedReader bufferedReader =
|
|
|
116
|
+ new BufferedReader(fileReader);
|
|
|
117
|
+
|
|
|
118
|
+ while((line = bufferedReader.readLine()) != null) {
|
|
|
119
|
+ sb.append(line + "\n");
|
|
|
120
|
+ }
|
|
|
121
|
+
|
|
|
122
|
+ // Always close files.
|
|
|
123
|
+ bufferedReader.close();
|
|
|
124
|
+ }
|
|
|
125
|
+ catch(FileNotFoundException ex) {
|
|
|
126
|
+ System.out.println(
|
|
|
127
|
+ "Unable to open file '" +
|
|
|
128
|
+ fileName + "'");
|
|
|
129
|
+ }
|
|
|
130
|
+ catch(IOException ex) {
|
|
|
131
|
+ System.out.println(
|
|
|
132
|
+ "Error reading file '"
|
|
|
133
|
+ + fileName + "'");
|
|
|
134
|
+ // Or we could just do this:
|
|
|
135
|
+ // ex.printStackTrace();
|
|
|
136
|
+ }
|
|
|
137
|
+ return sb.toString();
|
|
30
|
138
|
}
|
|
31
|
139
|
|
|
|
140
|
+ public void encryptFile(String fileName, String newFileName){
|
|
|
141
|
+ String text = readFile(fileName);
|
|
|
142
|
+ String encryptedText = encrypt(text);
|
|
|
143
|
+ writeToFile(newFileName, encryptedText);
|
|
|
144
|
+ }
|
|
|
145
|
+
|
|
|
146
|
+ private void writeToFile(String newFileName, String encryptedText) {
|
|
|
147
|
+ try {
|
|
|
148
|
+ FileWriter fileWriter = new FileWriter(newFileName);
|
|
|
149
|
+ BufferedWriter br = new BufferedWriter(fileWriter);
|
|
|
150
|
+ br.write(encryptedText);
|
|
|
151
|
+ br.flush();
|
|
|
152
|
+ br.close();
|
|
|
153
|
+ }catch (IOException ie){
|
|
|
154
|
+ ie.printStackTrace();
|
|
|
155
|
+ }
|
|
|
156
|
+ }
|
|
32
|
157
|
}
|