|
@@ -11,14 +11,21 @@ public class ROT13 {
|
11
|
11
|
int rotateBy = 13;
|
12
|
12
|
|
13
|
13
|
private char[] rotatedAlphbet;
|
14
|
|
- private char[] alphChars = "abcdefghijklmnopqrstuvwxyz".toCharArray();
|
|
14
|
+
|
|
15
|
+ private String alphbet = "abcdefghijklmnopqrstuvwxyz";
|
|
16
|
+ private char[] alphChars = alphbet.toCharArray();
|
|
17
|
+
|
|
18
|
+
|
15
|
19
|
|
16
|
20
|
ROT13(Character cs, Character cf) {
|
17
|
21
|
rotateBy = numbersByCount(cs, cf);
|
18
|
22
|
rotatedAlphbet = rotate("abcdefghijklmnopqrstuvwxyz").toCharArray();
|
19
|
23
|
}
|
20
|
24
|
|
21
|
|
- ROT13() {}
|
|
25
|
+ ROT13() {
|
|
26
|
+ rotateBy = 13;
|
|
27
|
+ rotatedAlphbet = rotate("abcdefghijklmnopqrstuvwxyz").toCharArray();
|
|
28
|
+ }
|
22
|
29
|
|
23
|
30
|
public static int numbersByCount(Character cs, Character cf){
|
24
|
31
|
String alphabet = "abcdefghijklmnopqrstuvwxyz";
|
|
@@ -41,40 +48,26 @@ public class ROT13 {
|
41
|
48
|
}
|
42
|
49
|
|
43
|
50
|
public static void main(String[] args){
|
44
|
|
- ROT13 rot13 = new ROT13('a','n');
|
|
51
|
+ ROT13 rot13 = new ROT13('a', 'm');
|
|
52
|
+ System.out.println(rot13.rotateBy);
|
45
|
53
|
rot13.encryptFile("sonnet18.txt");
|
46
|
54
|
rot13.decryptFile("sonnet18.enc");
|
47
|
|
- //System.out.println(new ROT13().crypt("Gb trg gb gur bgure fvqr!"));
|
48
|
55
|
}
|
49
|
56
|
|
50
|
57
|
public String crypt(String text) throws UnsupportedOperationException {
|
51
|
|
- StringBuilder sb = new StringBuilder();
|
52
|
58
|
char[] cars = text.toCharArray();
|
|
59
|
+ StringBuilder sb = new StringBuilder();
|
53
|
60
|
|
54
|
61
|
for(char car: cars){
|
55
|
62
|
if(Character.isLetter(car)){
|
56
|
|
- if(Character.isUpperCase(car)){
|
57
|
|
- sb.append(Character.toUpperCase(charShift(car)));
|
58
|
|
- }else {
|
59
|
|
- sb.append(charShift(car));
|
60
|
|
- }
|
|
63
|
+ sb.append(this.rotate(this.alphbet, car).toCharArray()[rotateBy]);
|
61
|
64
|
} else{
|
62
|
65
|
sb.append(car);
|
63
|
66
|
}
|
64
|
67
|
}
|
65
|
|
- return sb.toString();
|
66
|
68
|
|
67
|
|
- }
|
|
69
|
+ return sb.toString();
|
68
|
70
|
|
69
|
|
- private Character charShift(char c){
|
70
|
|
- Character car = null;
|
71
|
|
- for(int i = 0; i < this.alphChars.length; i++) {
|
72
|
|
- if(this.alphChars[i] == Character.toLowerCase(c)){
|
73
|
|
- car = this.rotatedAlphbet[i];
|
74
|
|
- break;
|
75
|
|
- }
|
76
|
|
- }
|
77
|
|
- return car;
|
78
|
71
|
}
|
79
|
72
|
|
80
|
73
|
public String encrypt(String text) {
|
|
@@ -82,25 +75,19 @@ public class ROT13 {
|
82
|
75
|
}
|
83
|
76
|
|
84
|
77
|
public String decrypt(String text) {
|
85
|
|
-
|
86
|
78
|
char[] cars = text.toCharArray();
|
87
|
|
-
|
88
|
79
|
StringBuilder sb = new StringBuilder();
|
89
|
80
|
|
90
|
|
- for(int i = 0; i < cars.length; i++){
|
91
|
|
- if(Character.isLetter(cars[i])){
|
92
|
|
- sb.append(cars[i]);
|
93
|
|
- if(rotateBy - i < 0){
|
94
|
|
- System.out.println(rotateBy -1);
|
95
|
|
- sb.append(rotatedAlphbet[(rotateBy - i) + (rotatedAlphbet.length -1)]);
|
96
|
|
- } else {
|
97
|
|
- sb.append(rotatedAlphbet[rotateBy - i]);
|
98
|
|
- }
|
99
|
|
- } else {
|
100
|
|
- sb.append(cars[i]);
|
|
81
|
+ for(char car: cars){
|
|
82
|
+ if(Character.isLetter(car)){
|
|
83
|
+ sb.append(this.rotate(this.alphbet, car).toCharArray()[this.alphChars.length - rotateBy]);
|
|
84
|
+ } else{
|
|
85
|
+ sb.append(car);
|
101
|
86
|
}
|
102
|
87
|
}
|
|
88
|
+
|
103
|
89
|
return sb.toString();
|
|
90
|
+
|
104
|
91
|
}
|
105
|
92
|
|
106
|
93
|
public String rotate(String s) {
|
|
@@ -119,11 +106,14 @@ public class ROT13 {
|
119
|
106
|
return new String(cars);
|
120
|
107
|
}
|
121
|
108
|
|
122
|
|
- public static String rotate(String s, Character c) {
|
|
109
|
+ public String rotate(String s, Character c) {
|
|
110
|
+ char temp;
|
|
111
|
+ boolean isUpper = false;
|
123
|
112
|
char[] cars = s.toCharArray();
|
|
113
|
+ isUpper = Character.isUpperCase(c) ? true : false;
|
|
114
|
+ char car = Character.toLowerCase(c);
|
124
|
115
|
|
125
|
|
- char temp;
|
126
|
|
- while(cars[0] != c){
|
|
116
|
+ while(cars[0] != car){
|
127
|
117
|
temp = cars[0];
|
128
|
118
|
for(int i = 0; i < cars.length -1; i++){
|
129
|
119
|
cars[i] = cars[i +1];
|
|
@@ -131,7 +121,7 @@ public class ROT13 {
|
131
|
121
|
cars[cars.length -1] = temp;
|
132
|
122
|
}
|
133
|
123
|
|
134
|
|
- return new String(cars);
|
|
124
|
+ return isUpper == true ? new String(cars).toUpperCase() : new String(cars);
|
135
|
125
|
}
|
136
|
126
|
|
137
|
127
|
public void encryptFile(String fileName){
|
|
@@ -180,7 +170,7 @@ public class ROT13 {
|
180
|
170
|
String decryptFile = this.decrypt(sb.toString());
|
181
|
171
|
|
182
|
172
|
try {
|
183
|
|
- File file = new File("sonnet18dec.txt");
|
|
173
|
+ File file = new File("sonnet18.dec");
|
184
|
174
|
FileWriter fileWriter = new FileWriter(file);
|
185
|
175
|
fileWriter.write(decryptFile);
|
186
|
176
|
fileWriter.flush();
|