Shivam Patel 6 年之前
父節點
當前提交
44a3114da4
共有 4 個檔案被更改,包括 102 行新增7 行删除
  1. 53
    7
      Crypto/src/ROT13.java
  2. 27
    0
      Crypto/src/ROT13Test.java
  3. 8
    0
      pom.xml
  4. 14
    0
      sonnet18.enc

+ 53
- 7
Crypto/src/ROT13.java 查看文件

@@ -1,32 +1,78 @@
1
-import static java.lang.Character.isLowerCase;
2
-import static java.lang.Character.isUpperCase;
3
-import static java.lang.Character.toLowerCase;
1
+import java.io.File;
2
+import java.io.FileNotFoundException;
3
+import java.io.FileWriter;
4
+import java.io.IOException;
5
+import java.util.Collections;
6
+import java.util.Scanner;
4 7
 
5 8
 public class ROT13  {
6 9
 
10
+    private int shift;
11
+
7 12
     ROT13(Character cs, Character cf) {
13
+        this.shift = cs-cf;
8 14
     }
9 15
 
10 16
     ROT13() {
17
+        shift = 13;
11 18
     }
12 19
 
13 20
 
14 21
     public String crypt(String text) throws UnsupportedOperationException {
15 22
 
16
-        return "";
23
+        StringBuilder build = new StringBuilder();
24
+        char[] arr = text.toCharArray();
25
+        for (char a: arr) {
26
+            char c = a;
27
+            if((c >= 'a' && c<= 'm') || (c >= 'A' && c<= 'M')){
28
+                c += 13;
29
+            }
30
+            else if((c >= 'n' && c <= 'z') || (c>= 'N' && c <= 'Z')){
31
+                c-= 13;
32
+            }
33
+            build.append(c);
34
+        }
35
+        return build.toString();
17 36
     }
18 37
 
38
+
39
+
40
+
19 41
     public String encrypt(String text) {
20
-        return text;
42
+        return crypt(text);
21 43
     }
22 44
 
23 45
     public String decrypt(String text) {
24
-        return text;
46
+        return crypt(text);
25 47
     }
26 48
 
27 49
     public static String rotate(String s, Character c) {
50
+        String rotate = s.substring(s.indexOf(c));
51
+        String rotateNext = s.substring(0,s.indexOf(c));
52
+
53
+
54
+        return rotate.concat(rotateNext);
55
+    }
56
+
57
+    public String encrptFile(String filename) throws FileNotFoundException{
58
+        StringBuilder build = new StringBuilder();
59
+
60
+        File f = new File(filename);
61
+        try(Scanner scan = new Scanner(f)){
62
+            while(scan.hasNextLine()){
63
+
64
+                String line = scan.nextLine();
65
+                String rot13 = this.encrypt(line);
66
+                build.append(rot13+ "\n");
67
+            }
68
+        }
69
+        catch (FileNotFoundException e){
70
+            e.printStackTrace();
71
+        }
72
+
73
+
28 74
 
29
-        return "";
75
+        return build.toString();
30 76
     }
31 77
 
32 78
 }

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

@@ -1,5 +1,8 @@
1 1
 import org.junit.Test;
2 2
 
3
+import java.io.File;
4
+import java.io.FileNotFoundException;
5
+
3 6
 import static org.junit.Assert.*;
4 7
 
5 8
 public class ROT13Test {
@@ -88,4 +91,28 @@ public class ROT13Test {
88 91
         assertTrue(actual.equals(Q1));
89 92
     }
90 93
 
94
+    @Test
95
+    public void sonnetEnc() throws FileNotFoundException {
96
+        ROT13 rot13 = new ROT13();
97
+        //File filename = new File("sonnet18.enc");
98
+        String expected = rot13.encrypt("Shall I compare thee to a summer’s day?\n" +
99
+                "Thou art more lovely and more temperate:\n" +
100
+                "Rough winds do shake the darling buds of May,\n" +
101
+                "And summer’s lease hath all too short a date;\n" +
102
+                "Sometime too hot the eye of heaven shines,\n" +
103
+                "And often is his gold complexion dimm'd;\n" +
104
+                "And every fair from fair sometime declines,\n" +
105
+                "By chance or nature’s changing course untrimm'd;\n" +
106
+                "But thy eternal summer shall not fade,\n" +
107
+                "Nor lose possession of that fair thou ow’st;\n" +
108
+                "Nor shall death brag thou wander’st in his shade,\n" +
109
+                "When in eternal lines to time thou grow’st:\n" +
110
+                "   So long as men can breathe or eyes can see,\n" +
111
+                "   So long lives this, and this gives life to thee.\n");
112
+
113
+        String actual = rot13.encrptFile("sonnet18.txt");
114
+
115
+        assertEquals(expected,actual);
116
+    }
117
+
91 118
 }

+ 8
- 0
pom.xml 查看文件

@@ -7,6 +7,14 @@
7 7
     <groupId>com.zipcodewilmington</groupId>
8 8
     <artifactId>SimpleCrypt</artifactId>
9 9
     <version>1.0-SNAPSHOT</version>
10
+    <dependencies>
11
+        <dependency>
12
+            <groupId>junit</groupId>
13
+            <artifactId>junit</artifactId>
14
+            <version>RELEASE</version>
15
+            <scope>compile</scope>
16
+        </dependency>
17
+    </dependencies>
10 18
 
11 19
 
12 20
 </project>

+ 14
- 0
sonnet18.enc 查看文件

@@ -0,0 +1,14 @@
1
+Funyy V pbzcner gurr gb n fhzzre’f qnl?
2
+Gubh neg zber ybiryl naq zber grzcrengr:
3
+Ebhtu jvaqf qb funxr gur qneyvat ohqf bs Znl,
4
+Naq fhzzre’f yrnfr ungu nyy gbb fubeg n qngr;
5
+Fbzrgvzr gbb ubg gur rlr bs urnira fuvarf,
6
+Naq bsgra vf uvf tbyq pbzcyrkvba qvzz'q;
7
+Naq rirel snve sebz snve fbzrgvzr qrpyvarf,
8
+Ol punapr be angher’f punatvat pbhefr hagevzz'q;
9
+Ohg gul rgreany fhzzre funyy abg snqr,
10
+Abe ybfr cbffrffvba bs gung snve gubh bj’fg;
11
+Abe funyy qrngu oent gubh jnaqre’fg va uvf funqr,
12
+Jura va rgreany yvarf gb gvzr gubh tebj’fg:
13
+   Fb ybat nf zra pna oerngur be rlrf pna frr,
14
+   Fb ybat yvirf guvf, naq guvf tvirf yvsr gb gurr.