|
@@ -1,32 +1,72 @@
|
1
|
|
-import static java.lang.Character.isLowerCase;
|
2
|
|
-import static java.lang.Character.isUpperCase;
|
3
|
|
-import static java.lang.Character.toLowerCase;
|
|
1
|
+import java.io.File;
|
|
2
|
+import java.io.FileWriter;
|
|
3
|
+import java.io.IOException;
|
|
4
|
+import java.nio.file.Files;
|
|
5
|
+import java.nio.file.Paths;
|
4
|
6
|
|
5
|
7
|
public class ROT13 {
|
6
|
8
|
|
|
9
|
+ String ciphered;
|
|
10
|
+ int rotation;
|
|
11
|
+
|
7
|
12
|
ROT13(Character cs, Character cf) {
|
|
13
|
+ this.rotation = Math.abs(cs - cf);
|
8
|
14
|
}
|
9
|
15
|
|
10
|
16
|
ROT13() {
|
|
17
|
+ this.rotation = 13;
|
11
|
18
|
}
|
12
|
19
|
|
13
|
20
|
|
14
|
21
|
public String crypt(String text) throws UnsupportedOperationException {
|
15
|
22
|
|
16
|
|
- return "";
|
|
23
|
+ StringBuilder sb = new StringBuilder();
|
|
24
|
+
|
|
25
|
+ for(int i = 0; i < text.length(); i++){
|
|
26
|
+ if(!Character.isLetter(text.charAt(i))){
|
|
27
|
+ sb.append(text.charAt(i));
|
|
28
|
+ } else if(Character.isUpperCase(text.charAt(i))){
|
|
29
|
+ char ch = (char) (((int) text.charAt(i) + rotation - 65) % 26 + 65);
|
|
30
|
+ sb.append(ch);
|
|
31
|
+ } else {
|
|
32
|
+ char ch = (char) (((int) text.charAt(i)+ rotation - 97) % 26 + 97);
|
|
33
|
+ sb.append(ch);
|
|
34
|
+ }
|
|
35
|
+ } return sb.toString();
|
17
|
36
|
}
|
18
|
37
|
|
19
|
38
|
public String encrypt(String text) {
|
20
|
|
- return text;
|
|
39
|
+
|
|
40
|
+ return crypt(text);
|
21
|
41
|
}
|
22
|
42
|
|
23
|
43
|
public String decrypt(String text) {
|
24
|
|
- return text;
|
|
44
|
+
|
|
45
|
+ return crypt(text);
|
25
|
46
|
}
|
26
|
47
|
|
27
|
|
- public static String rotate(String s, Character c) {
|
|
48
|
+ public String rotate(String s, Character c) {
|
|
49
|
+ int difference = (int) c - 65;
|
|
50
|
+ int i = difference % s.length();
|
|
51
|
+ return s.substring(i) + s.substring(0,i);
|
|
52
|
+ }
|
28
|
53
|
|
29
|
|
- return "";
|
|
54
|
+ public String fileToString (File filePath) throws IOException {
|
|
55
|
+ return new String(Files.readAllBytes(Paths.get(String.valueOf(filePath))));
|
30
|
56
|
}
|
31
|
57
|
|
|
58
|
+ public File newEncryptedFile(String original, String newFileName) {
|
|
59
|
+ File newFile = null;
|
|
60
|
+ try {
|
|
61
|
+ newFile = new File(newFileName);
|
|
62
|
+ FileWriter fileWriter = new FileWriter(newFile);
|
|
63
|
+ fileWriter.write(encrypt(fileToString(new File(original))));
|
|
64
|
+ fileWriter.flush();
|
|
65
|
+ fileWriter.close();
|
|
66
|
+
|
|
67
|
+ } catch (IOException e) {
|
|
68
|
+ e.printStackTrace();
|
|
69
|
+ }
|
|
70
|
+ return newFile;
|
|
71
|
+ }
|
32
|
72
|
}
|