|
@@ -0,0 +1,101 @@
|
|
1
|
+import static java.lang.Character.isLowerCase;
|
|
2
|
+import static java.lang.Character.isUpperCase;
|
|
3
|
+import static java.lang.Character.toLowerCase;
|
|
4
|
+
|
|
5
|
+public class ROT13 {
|
|
6
|
+
|
|
7
|
+ int rotateBy = 13;
|
|
8
|
+
|
|
9
|
+ private char[] rotatedAlphbet;
|
|
10
|
+ private char[] alphChars = "abcdefghijklmnopqrstuvwxyz".toCharArray();
|
|
11
|
+
|
|
12
|
+ ROT13(Character cs, Character cf) {
|
|
13
|
+ rotateBy = numbersByCount(cs, cf);
|
|
14
|
+ rotatedAlphbet = rotate("abcdefghijklmnopqrstuvwxyz", cf).toCharArray();
|
|
15
|
+ }
|
|
16
|
+
|
|
17
|
+ ROT13() {}
|
|
18
|
+
|
|
19
|
+ public static int numbersByCount(Character cs, Character cf){
|
|
20
|
+ String alphabet = "abcdefghijklmnopqrstuvwxyz";
|
|
21
|
+ char[] alphChars = alphabet.toCharArray();
|
|
22
|
+
|
|
23
|
+ int counter = 0;
|
|
24
|
+ for(int i = 0; i < alphChars.length; i++){
|
|
25
|
+ if(counter == 0){
|
|
26
|
+ if(alphChars[i] == cs){
|
|
27
|
+ counter++;
|
|
28
|
+ }
|
|
29
|
+ } else if(alphChars[i] != cf){
|
|
30
|
+ counter++;
|
|
31
|
+ } else{
|
|
32
|
+ break;
|
|
33
|
+ }
|
|
34
|
+ }
|
|
35
|
+ return counter;
|
|
36
|
+
|
|
37
|
+ }
|
|
38
|
+
|
|
39
|
+ public static void main(String[] args){
|
|
40
|
+ new ROT13('a','n');
|
|
41
|
+ //System.out.println(new ROT13().crypt("Gb trg gb gur bgure fvqr!"));
|
|
42
|
+ }
|
|
43
|
+
|
|
44
|
+ public String crypt(String text) throws UnsupportedOperationException {
|
|
45
|
+ StringBuilder sb = new StringBuilder();
|
|
46
|
+ char[] cars = text.toCharArray();
|
|
47
|
+
|
|
48
|
+ for(char car: cars){
|
|
49
|
+ if(Character.isLetter(car)){
|
|
50
|
+ if(Character.isUpperCase(car)){
|
|
51
|
+ sb.append(Character.toUpperCase(charShift(car)));
|
|
52
|
+ }else {
|
|
53
|
+ sb.append(charShift(car));
|
|
54
|
+ }
|
|
55
|
+ } else{
|
|
56
|
+ sb.append(car);
|
|
57
|
+ }
|
|
58
|
+
|
|
59
|
+ }
|
|
60
|
+ return sb.toString();
|
|
61
|
+
|
|
62
|
+ }
|
|
63
|
+
|
|
64
|
+ private Character charShift(char c){
|
|
65
|
+ Character car = null;
|
|
66
|
+ for(int i = 0; i < this.alphChars.length; i++) {
|
|
67
|
+ if(this.alphChars[i] == Character.toLowerCase(c)){
|
|
68
|
+ car = this.rotatedAlphbet[i];
|
|
69
|
+ break;
|
|
70
|
+ }
|
|
71
|
+ }
|
|
72
|
+ return car;
|
|
73
|
+ }
|
|
74
|
+
|
|
75
|
+ public String encrypt(String text) {
|
|
76
|
+ return this.crypt(text);
|
|
77
|
+ }
|
|
78
|
+
|
|
79
|
+ public String decrypt(String text) {
|
|
80
|
+
|
|
81
|
+ return this.crypt(text);
|
|
82
|
+ }
|
|
83
|
+
|
|
84
|
+ public static String rotate(String s, Character c) {
|
|
85
|
+ char[] cars = s.toCharArray();
|
|
86
|
+
|
|
87
|
+ char temp;
|
|
88
|
+ while(cars[0] != c){
|
|
89
|
+ temp = cars[0];
|
|
90
|
+ for(int i = 0; i < cars.length -1; i++){
|
|
91
|
+ cars[i] = cars[i +1];
|
|
92
|
+ }
|
|
93
|
+ cars[cars.length -1] = temp;
|
|
94
|
+ }
|
|
95
|
+
|
|
96
|
+ return new String(cars);
|
|
97
|
+ }
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+}
|