#15 Khalil

Avoinna
Ksaboor haluaa yhdistää 3 committia lähteestä Ksaboor/ZCW-SimpleCrypt0:master kohteeseen master
5 muutettua tiedostoa jossa 116 lisäystä ja 11 poistoa
  1. 48
    8
      Crypto/src/ROT13.java
  2. 31
    1
      Crypto/src/ROT13Test.java
  3. 15
    0
      SimpleCrypt.iml
  4. 20
    0
      pom.xml
  5. 2
    2
      sonnet18.txt

+ 48
- 8
Crypto/src/ROT13.java Näytä tiedosto

@@ -1,32 +1,72 @@
1
+import java.io.File;
2
+import java.io.FileNotFoundException;
3
+import java.util.Scanner;
4
+
1 5
 import static java.lang.Character.isLowerCase;
2 6
 import static java.lang.Character.isUpperCase;
3 7
 import static java.lang.Character.toLowerCase;
4 8
 
5 9
 public class ROT13  {
6
-
10
+    private int shift;
7 11
     ROT13(Character cs, Character cf) {
12
+        this.shift = cs - cf;
8 13
     }
9 14
 
10 15
     ROT13() {
16
+        shift = 13;
11 17
     }
12 18
 
13 19
 
14
-    public String crypt(String text) throws UnsupportedOperationException {
15
-
16
-        return "";
20
+    public static String crypt(String text) throws UnsupportedOperationException {
21
+        StringBuilder sb = new StringBuilder();
22
+        char [] chars = text.toCharArray();
23
+        for (int i = 0; i <chars.length ; i++) {
24
+            char c = text.charAt(i);
25
+            if(c >= 'a' && c <= 'm') {
26
+                c += 13;
27
+            }else if  (c >= 'A' && c <= 'M') {
28
+                c += 13;
29
+            } else if  (c >= 'n' && c <= 'z') {
30
+                c -= 13;
31
+            } else if  (c >= 'N' && c <= 'Z') {
32
+                c -= 13;
33
+            }
34
+            sb.append(c);
35
+        }
36
+        return sb.toString();
17 37
     }
18 38
 
19
-    public String encrypt(String text) {
20
-        return text;
39
+    public static String encrypt(String text) {
40
+        return crypt(text);
21 41
     }
22 42
 
23 43
     public String decrypt(String text) {
24
-        return text;
44
+        return crypt(text);
25 45
     }
26 46
 
27 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();
28 57
 
29
-        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();
30 70
     }
31 71
 
32 72
 }

+ 31
- 1
Crypto/src/ROT13Test.java Näytä tiedosto

@@ -1,5 +1,8 @@
1
+import org.junit.Assert;
1 2
 import org.junit.Test;
2 3
 
4
+import java.io.File;
5
+
3 6
 import static org.junit.Assert.*;
4 7
 
5 8
 public class ROT13Test {
@@ -61,8 +64,11 @@ public class ROT13Test {
61 64
 
62 65
         // When
63 66
         String actual = cipher.encrypt(Q1);
67
+        System.out.println(actual);
68
+
64 69
         System.out.println(Q1);
65 70
         System.out.println(A1);
71
+      //  System.out.println(actual);
66 72
         // Then
67 73
         assertTrue(actual.equals(A1));
68 74
 
@@ -70,6 +76,7 @@ public class ROT13Test {
70 76
         String actual2 = cipher.decrypt(Q2);
71 77
         System.out.println(Q2);
72 78
         System.out.println(A2);
79
+        System.out.println(actual2);
73 80
         // Then
74 81
         assertTrue(actual2.equals(A2));
75 82
     }
@@ -87,5 +94,28 @@ public class ROT13Test {
87 94
         // Then
88 95
         assertTrue(actual.equals(Q1));
89 96
     }
90
-
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
+    }
91 121
 }

+ 15
- 0
SimpleCrypt.iml Näytä tiedosto

@@ -0,0 +1,15 @@
1
+<?xml version="1.0" encoding="UTF-8"?>
2
+<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
3
+  <component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_7">
4
+    <output url="file://$MODULE_DIR$/target/classes" />
5
+    <output-test url="file://$MODULE_DIR$/target/test-classes" />
6
+    <content url="file://$MODULE_DIR$">
7
+      <sourceFolder url="file://$MODULE_DIR$/Crypto/src" isTestSource="false" />
8
+      <excludeFolder url="file://$MODULE_DIR$/target" />
9
+    </content>
10
+    <orderEntry type="inheritedJdk" />
11
+    <orderEntry type="sourceFolder" forTests="false" />
12
+    <orderEntry type="library" name="Maven: junit:junit:4.12" level="project" />
13
+    <orderEntry type="library" name="Maven: org.hamcrest:hamcrest-core:1.3" level="project" />
14
+  </component>
15
+</module>

+ 20
- 0
pom.xml Näytä tiedosto

@@ -7,6 +7,26 @@
7 7
     <groupId>com.zipcodewilmington</groupId>
8 8
     <artifactId>SimpleCrypt</artifactId>
9 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>
22
+    <dependencies>
23
+        <dependency>
24
+            <groupId>junit</groupId>
25
+            <artifactId>junit</artifactId>
26
+            <version>RELEASE</version>
27
+            <scope>compile</scope>
28
+        </dependency>
29
+    </dependencies>
10 30
 
11 31
 
12 32
 </project>

+ 2
- 2
sonnet18.txt Näytä tiedosto

@@ -10,5 +10,5 @@ But thy eternal summer shall not fade,
10 10
 Nor lose possession of that fair thou ow’st;
11 11
 Nor shall death brag thou wander’st in his shade,
12 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.