瀏覽代碼

Caesar Cipher

Trinh Tong 6 年之前
父節點
當前提交
ef1e42f7cb
共有 4 個檔案被更改,包括 108 行新增20 行删除
  1. 46
    0
      src/main/java/CaeserCipher.java
  2. 23
    19
      src/main/java/ROT13.java
  3. 36
    0
      src/test/java/CaeserCipherTest.java
  4. 3
    1
      src/test/java/ROT13Test.java

+ 46
- 0
src/main/java/CaeserCipher.java 查看文件

@@ -0,0 +1,46 @@
1
+public class CaeserCipher extends ROT13 {
2
+    // takes in a shift and then returns an encoded string of the letter shift
3
+
4
+    public CaeserCipher(int shift) {
5
+        super.setShift(shift);
6
+    }
7
+
8
+
9
+    public String encrypt(String s) {
10
+        StringBuilder sb = new StringBuilder();
11
+
12
+        for (int i = 0; i < s.length(); i++) {
13
+            char c = s.charAt(i);
14
+
15
+            if (c >= 'a' && c <= 'z') {
16
+                sb.append((char) ((c - 'a' + getShift()) % 26 + 'a'));
17
+
18
+            } else if (c >= 'A'  && c <= 'Z') {
19
+                sb.append((char)((c - 'A' + getShift()) % 26 + 'A'));
20
+
21
+            } else {
22
+                sb.append(c);
23
+            }
24
+        }
25
+        return sb.toString();
26
+    }
27
+
28
+    public String decrypt(String s) {
29
+        StringBuilder sb = new StringBuilder();
30
+        for (int i = 0; i < s.length(); i++) {
31
+            char c = s.charAt(i);
32
+
33
+            if (c >= 'a' && c <= 'z') {
34
+                sb.append((char) ((c + 'a' - getShift()) % 26 + 'a'));
35
+
36
+            } else if (c >= 'A'  && c <= 'Z') {
37
+                sb.append((char)((c + 'A' - getShift()) % 26 + 'A'));
38
+
39
+            } else {
40
+                sb.append(c);
41
+            }
42
+        }
43
+        return sb.toString();
44
+
45
+    }
46
+}

+ 23
- 19
src/main/java/ROT13.java 查看文件

@@ -3,45 +3,49 @@ import static java.lang.Character.isUpperCase;
3 3
 import static java.lang.Character.toLowerCase;
4 4
 
5 5
 public class ROT13  {
6
-
7
-    private int rotation;
6
+    private int shift;
8 7
 
9 8
     ROT13(Character cs, Character cf) {
10
-        rotation = cf - cs;
9
+        shift = cf - cs;
11 10
         // With this constructor, we are finding how much to rotate by
12 11
         // UP TO CF, not including
13 12
     }
14 13
 
15 14
     ROT13() {
15
+        shift = 13;
16
+    }
17
+
18
+    public int getShift() {
19
+        return shift;
16 20
     }
17 21
 
18
-    public int getRotation() {
19
-        return rotation;
22
+    public void setShift(int shift) {
23
+        this.shift = shift;
20 24
     }
21 25
 
22 26
     public String crypt(String text) throws UnsupportedOperationException {
23
-        String s = "";
24
-        // only 26 letters in the alphabet, with the 13 rotation, the crpt and encrypt are equal
27
+        StringBuilder sb = new StringBuilder();
28
+        // only 26 letters in the alphabet, with the 13 shift, the crpt and encrypt are equal
25 29
         for (int i = 0; i < text.length(); i++) {
26 30
             char c = text.charAt(i);
27 31
             if (c >= 'a' && c <= 'm') {
28
-                c += rotation;
29
-                s += c;
32
+                c += shift;
33
+                sb.append(c);
30 34
             } else if (c >= 'A' && c <= 'M') {
31
-                c += rotation;
32
-                s += c;
35
+                c += shift;
36
+                sb.append(c);
33 37
             } else if (c >= 'n' && c <= 'z') {
34
-                c -= rotation;
35
-                s += c;
38
+                c -= shift;
39
+                sb.append(c);
36 40
             } else if (c >= 'N' && c <= 'Z') {
37
-                c -= rotation;
38
-                s += c;
41
+                c -= shift;
42
+                sb.append(c);
39 43
             } else {
40
-                s += c;
44
+                sb.append(c);
41 45
             }
42 46
         }
43 47
         // we are rotating each letter, not shifting
44
-        return s;
48
+        return sb.toString();
45 49
     }
46 50
 
47 51
     public String encrypt(String text) {
@@ -63,9 +67,9 @@ public class ROT13  {
63 67
             }
64 68
         }
65 69
 
66
-        int shift = index % s.length();
70
+        int rotation = index % s.length();
67 71
 
68
-        return s.substring(shift) + s.substring(0, index);
72
+        return s.substring(rotation) + s.substring(0, index);
69 73
     }
70 74
 
71 75
 }

+ 36
- 0
src/test/java/CaeserCipherTest.java 查看文件

@@ -0,0 +1,36 @@
1
+import org.junit.Assert;
2
+import org.junit.Test;
3
+
4
+import static org.junit.Assert.*;
5
+
6
+public class CaeserCipherTest {
7
+
8
+    @Test
9
+    public void testCaesar() {
10
+        CaeserCipher cc = new CaeserCipher(23);
11
+
12
+
13
+        String text = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
14
+        String expected = "XYZABCDEFGHIJKLMNOPQRSTUVW";
15
+
16
+        String actual = cc.encrypt(text);
17
+        System.out.println(actual);
18
+        Assert.assertEquals(expected, actual);
19
+
20
+    }
21
+
22
+    @Test
23
+    public void testDecrypt() {
24
+        CaeserCipher cc = new CaeserCipher(23);
25
+
26
+        String text = "XYZABCDEFGHIJKLMNOPQRSTUVW";
27
+        String exp = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
28
+
29
+        String actual = cc.decrypt(text);
30
+
31
+        Assert.assertEquals(exp, actual);
32
+
33
+
34
+
35
+    }
36
+}

+ 3
- 1
src/test/java/ROT13Test.java 查看文件

@@ -1,3 +1,4 @@
1
+import org.junit.Assert;
1 2
 import org.junit.Test;
2 3
 
3 4
 import static org.junit.Assert.*;
@@ -92,6 +93,7 @@ public class ROT13Test {
92 93
     @Test
93 94
     public void testRotation() {
94 95
         ROT13 cipher = new ROT13('a', 'n');
95
-        System.out.println(cipher.getRotation());
96
+        System.out.println(cipher.getShift());
96 97
     }
98
+
97 99
 }