父节点
当前提交
93989804f1
共有 5 个文件被更改,包括 69 次插入7 次删除
  1. 27
    3
      Crypto/src/ROT13.java
  2. 27
    1
      Crypto/src/ROT13Test.java
  3. 1
    1
      SimpleCrypt.iml
  4. 12
    0
      pom.xml
  5. 2
    2
      sonnet18.txt

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

1
+import java.io.File;
2
+import java.io.FileNotFoundException;
3
+import java.util.Scanner;
4
+
1
 import static java.lang.Character.isLowerCase;
5
 import static java.lang.Character.isLowerCase;
2
 import static java.lang.Character.isUpperCase;
6
 import static java.lang.Character.isUpperCase;
3
 import static java.lang.Character.toLowerCase;
7
 import static java.lang.Character.toLowerCase;
13
     }
17
     }
14
 
18
 
15
 
19
 
16
-    public String crypt(String text) throws UnsupportedOperationException {
20
+    public static String crypt(String text) throws UnsupportedOperationException {
17
         StringBuilder sb = new StringBuilder();
21
         StringBuilder sb = new StringBuilder();
18
         char [] chars = text.toCharArray();
22
         char [] chars = text.toCharArray();
19
         for (int i = 0; i <chars.length ; i++) {
23
         for (int i = 0; i <chars.length ; i++) {
32
         return sb.toString();
36
         return sb.toString();
33
     }
37
     }
34
 
38
 
35
-    public String encrypt(String text) {
39
+    public static String encrypt(String text) {
36
         return crypt(text);
40
         return crypt(text);
37
     }
41
     }
38
 
42
 
41
     }
45
     }
42
 
46
 
43
     public static String rotate(String s, Character c) {
47
     public static String rotate(String s, Character c) {
48
+        //this method will rotate the given String
49
+        //by the character placement
50
+        String sub = s.substring(0,s.indexOf(c));
51
+        String sub1 = s.substring(s.indexOf(c));
52
+        //then concat the two substrings.
53
+        return sub1.concat(sub);
54
+    }
55
+    public static String textFile(String filename){
56
+        StringBuilder sb = new StringBuilder();
44
 
57
 
45
-        return "";
58
+        File file = new File(filename);
59
+        try(Scanner sc = new Scanner(file)) {
60
+        while(sc.hasNextLine()){
61
+        String line = sc.nextLine();
62
+        String xored = encrypt(line);
63
+        sb.append(xored+"\n");
64
+          }
65
+          sb.deleteCharAt(sb.length()-1);
66
+        } catch (FileNotFoundException e){
67
+            e.printStackTrace();
68
+        }
69
+        return sb.toString();
46
     }
70
     }
47
 
71
 
48
 }
72
 }

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

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

+ 1
- 1
SimpleCrypt.iml 查看文件

1
 <?xml version="1.0" encoding="UTF-8"?>
1
 <?xml version="1.0" encoding="UTF-8"?>
2
 <module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
2
 <module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
3
-  <component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_5">
3
+  <component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_7">
4
     <output url="file://$MODULE_DIR$/target/classes" />
4
     <output url="file://$MODULE_DIR$/target/classes" />
5
     <output-test url="file://$MODULE_DIR$/target/test-classes" />
5
     <output-test url="file://$MODULE_DIR$/target/test-classes" />
6
     <content url="file://$MODULE_DIR$">
6
     <content url="file://$MODULE_DIR$">

+ 12
- 0
pom.xml 查看文件

7
     <groupId>com.zipcodewilmington</groupId>
7
     <groupId>com.zipcodewilmington</groupId>
8
     <artifactId>SimpleCrypt</artifactId>
8
     <artifactId>SimpleCrypt</artifactId>
9
     <version>1.0-SNAPSHOT</version>
9
     <version>1.0-SNAPSHOT</version>
10
+    <build>
11
+        <plugins>
12
+            <plugin>
13
+                <groupId>org.apache.maven.plugins</groupId>
14
+                <artifactId>maven-compiler-plugin</artifactId>
15
+                <configuration>
16
+                    <source>7</source>
17
+                    <target>7</target>
18
+                </configuration>
19
+            </plugin>
20
+        </plugins>
21
+    </build>
10
     <dependencies>
22
     <dependencies>
11
         <dependency>
23
         <dependency>
12
             <groupId>junit</groupId>
24
             <groupId>junit</groupId>

+ 2
- 2
sonnet18.txt 查看文件

10
 Nor lose possession of that fair thou ow’st;
10
 Nor lose possession of that fair thou ow’st;
11
 Nor shall death brag thou wander’st in his shade,
11
 Nor shall death brag thou wander’st in his shade,
12
 When in eternal lines to time thou grow’st:
12
 When in eternal lines to time thou grow’st:
13
-   So long as men can breathe or eyes can see,
14
-   So long lives this, and this gives life to thee.
13
+So long as men can breathe or eyes can see,
14
+So long lives this, and this gives life to thee.