#17 Crypt

Open
primeranary wants to merge 1 commits from primeranary/ZCW-SimpleCrypt0:master into master
5 changed files with 125 additions and 32 deletions
  1. 0
    32
      Crypto/src/ROT13.java
  2. 73
    0
      Crypto/src/java/ROT13.java
  3. 22
    0
      Crypto/src/test/ROT13Test.java
  4. 16
    0
      SimpleCrypt.iml
  5. 14
    0
      pom.xml

+ 0
- 32
Crypto/src/ROT13.java View File

@@ -1,32 +0,0 @@
1
-import static java.lang.Character.isLowerCase;
2
-import static java.lang.Character.isUpperCase;
3
-import static java.lang.Character.toLowerCase;
4
-
5
-public class ROT13  {
6
-
7
-    ROT13(Character cs, Character cf) {
8
-    }
9
-
10
-    ROT13() {
11
-    }
12
-
13
-
14
-    public String crypt(String text) throws UnsupportedOperationException {
15
-
16
-        return "";
17
-    }
18
-
19
-    public String encrypt(String text) {
20
-        return text;
21
-    }
22
-
23
-    public String decrypt(String text) {
24
-        return text;
25
-    }
26
-
27
-    public static String rotate(String s, Character c) {
28
-
29
-        return "";
30
-    }
31
-
32
-}

+ 73
- 0
Crypto/src/java/ROT13.java View File

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

Crypto/src/ROT13Test.java → Crypto/src/test/ROT13Test.java View File

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

+ 16
- 0
SimpleCrypt.iml View File

@@ -0,0 +1,16 @@
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_8">
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/test" isTestSource="true" />
8
+      <sourceFolder url="file://$MODULE_DIR$/Crypto/src/java" isTestSource="false" />
9
+      <excludeFolder url="file://$MODULE_DIR$/target" />
10
+    </content>
11
+    <orderEntry type="inheritedJdk" />
12
+    <orderEntry type="sourceFolder" forTests="false" />
13
+    <orderEntry type="library" scope="TEST" name="Maven: junit:junit:4.12" level="project" />
14
+    <orderEntry type="library" scope="TEST" name="Maven: org.hamcrest:hamcrest-core:1.3" level="project" />
15
+  </component>
16
+</module>

+ 14
- 0
pom.xml View File

@@ -7,6 +7,20 @@
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>test</scope>
16
+        </dependency>
17
+        <dependency>
18
+            <groupId>junit</groupId>
19
+            <artifactId>junit</artifactId>
20
+            <version>RELEASE</version>
21
+            <scope>test</scope>
22
+        </dependency>
23
+    </dependencies>
10 24
 
11 25
 
12 26
 </project>