Kaitrina High 6 years ago
parent
commit
9c13b2a525
2 changed files with 37 additions and 7 deletions
  1. 9
    0
      Crypto/src/CaesarCipher.java
  2. 28
    7
      Crypto/src/ROT13.java

+ 9
- 0
Crypto/src/CaesarCipher.java View File

@@ -0,0 +1,9 @@
1
+package src;
2
+
3
+public class CaesarCipher extends ROT13 {
4
+
5
+    CaesarCipher(Character cs, Character cf) {
6
+        super(cs, cf);
7
+    }
8
+
9
+}

+ 28
- 7
Crypto/src/ROT13.java View File

@@ -2,7 +2,7 @@ import static java.lang.Character.isLowerCase;
2 2
 import static java.lang.Character.isUpperCase;
3 3
 import static java.lang.Character.toLowerCase;
4 4
 
5
-public class ROT13  {
5
+public class ROT13 {
6 6
 
7 7
     ROT13(Character cs, Character cf) {
8 8
     }
@@ -12,21 +12,42 @@ public class ROT13  {
12 12
 
13 13
 
14 14
     public String crypt(String text) throws UnsupportedOperationException {
15
-
16
-        return "";
15
+        /**Split alphabet in half. Add 13 to chars before middle of alphabet. Subtracting 13 will rotate
16
+         to the begining of the alphabet. ASCII chars are in order so adding to ascii value will get next
17
+         alphabet*/
18
+        StringBuilder shiftStr = new StringBuilder();
19
+        for (int i = 0; i < text.length(); i++) {
20
+            char charToShift = text.charAt(i);
21
+            if (charToShift >= 'a' && charToShift <= 'm') {
22
+                charToShift += 13;
23
+            } else if (charToShift >= 'A' && charToShift <= 'M') {
24
+                charToShift += 13;
25
+            } else if (charToShift >= 'n' && charToShift <= 'z') {
26
+                charShift -= 13;
27
+            } else if (charToShift >= 'N' && charToShift <= 'Z') {
28
+                charToShift -= 13;
29
+            }
30
+        }
31
+        shiftStr.append(charToShift);
17 32
     }
33
+        return shiftStr.toString();
34
+}
18 35
 
19 36
     public String encrypt(String text) {
20
-        return text;
37
+        return crypt(text);
21 38
     }
22 39
 
23 40
     public String decrypt(String text) {
24
-        return text;
41
+        return crypt(text);
25 42
     }
26 43
 
27 44
     public static String rotate(String s, Character c) {
28
-
29
-        return "";
45
+        StringBuilder rotated = StringBuilder();
46
+        //substring: when given one index will slice at that index until length of string
47
+        String rightHalf = s.substring(s.indexOf(c));
48
+        //substring: second param is exclusive and won't include indexOf c
49
+        String leftHalf =s.substring(0, s.indexOf(c));
50
+        return rotated;
30 51
     }
31 52
 
32 53
 }