浏览代码

FINISHEDDDDD

Amy Gill 6 年前
父节点
当前提交
6f025f6e31
共有 2 个文件被更改,包括 43 次插入5 次删除
  1. 41
    3
      Crypto/src/ROT13.java
  2. 2
    2
      Crypto/src/ROT13Test.java

+ 41
- 3
Crypto/src/ROT13.java 查看文件

@@ -6,11 +6,20 @@ import static java.lang.Character.toLowerCase;
6 6
 
7 7
 public class ROT13  {
8 8
 
9
+    private String myCipher;
10
+    private String myAlphabet;
11
+
9 12
     public static void main(String[] args) {
13
+
10 14
         System.out.println("0384");
11 15
     }
12 16
 
13 17
     ROT13(Character cs, Character cf) {
18
+        myAlphabet = "abcdefghijklmnopqrstuvwxyz";
19
+        myCipher = rotate(myAlphabet,cf);
20
+        myAlphabet += myAlphabet.toUpperCase();
21
+        myCipher += myCipher.toUpperCase();
22
+
14 23
     }
15 24
 
16 25
     ROT13() {
@@ -19,15 +28,44 @@ public class ROT13  {
19 28
 
20 29
     public String crypt(String text) throws UnsupportedOperationException {
21 30
 
22
-        return "";
31
+        return encrypt(decrypt(text));
23 32
     }
24 33
 
25 34
     public String encrypt(String text) {
26
-        return text;
35
+
36
+        String encyrptedMessage = "";
37
+
38
+        for (int i = 0; i < text.length(); i++){
39
+            char charToRotate = text.charAt(i);
40
+            int indexOfCurrentStringLetter  = myAlphabet.indexOf(charToRotate);
41
+            if (indexOfCurrentStringLetter == -1){
42
+                encyrptedMessage+=charToRotate;
43
+            } else {
44
+                encyrptedMessage += Character.toString(myCipher.charAt(indexOfCurrentStringLetter));
45
+            }
46
+
47
+        }
48
+
49
+        return encyrptedMessage;
27 50
     }
28 51
 
29 52
     public String decrypt(String text) {
30
-        return text;
53
+
54
+        String decryptedMessage = "";
55
+
56
+        for (int i = 0; i < text.length(); i++){
57
+            char charToRotate = text.charAt(i);
58
+            int indexOfCurrentStringLetter  = myCipher.indexOf(charToRotate);
59
+            if (indexOfCurrentStringLetter == -1){
60
+                decryptedMessage+=charToRotate;
61
+            } else {
62
+                decryptedMessage += Character.toString(myAlphabet.charAt(indexOfCurrentStringLetter));
63
+            }
64
+
65
+
66
+        }
67
+
68
+        return decryptedMessage;
31 69
     }
32 70
 
33 71
     public static String rotate(String s, Character c) {

+ 2
- 2
Crypto/src/ROT13Test.java 查看文件

@@ -67,14 +67,14 @@ public class ROT13Test {
67 67
         System.out.println(Q1);
68 68
         System.out.println(A1);
69 69
         // Then
70
-        assertTrue(actual.equals(A1));
70
+        assertEquals(A1, actual);
71 71
 
72 72
         // When
73 73
         String actual2 = cipher.decrypt(Q2);
74 74
         System.out.println(Q2);
75 75
         System.out.println(A2);
76 76
         // Then
77
-        assertTrue(actual2.equals(A2));
77
+        assertEquals(A2, actual2);
78 78
     }
79 79
     @Test
80 80
     public void cryptTest2() {