Vincent Sima 6 years ago
parent
commit
fae9e640e5
5 changed files with 130 additions and 12 deletions
  1. 65
    11
      Crypto/src/ROT13.java
  2. 13
    1
      Crypto/src/ROT13Test.java
  3. 15
    0
      SimpleCrypt.iml
  4. 23
    0
      pom.xml
  5. 14
    0
      sonnet18.enc

+ 65
- 11
Crypto/src/ROT13.java View File

@@ -1,32 +1,86 @@
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.*;
2
+import java.nio.file.Files;
3
+import java.nio.file.Paths;
4 4
 
5
-public class ROT13  {
5
+import static java.lang.Character.*;
6
+
7
+public class ROT13 {
8
+    int difference;
6 9
 
7 10
     ROT13(Character cs, Character cf) {
11
+        difference = (int) cf - (int) cs;
8 12
     }
9 13
 
10 14
     ROT13() {
15
+        difference = 13;
11 16
     }
12 17
 
13 18
 
14 19
     public String crypt(String text) throws UnsupportedOperationException {
15 20
 
16
-        return "";
21
+        StringBuffer result = new StringBuffer();
22
+
23
+        for (int i = 0; i < text.length(); i++) {
24
+            if (!Character.isLetter(text.charAt(i))) {
25
+                result.append(text.charAt(i));
26
+            } else if (Character.isUpperCase(text.charAt(i))) {
27
+                char ch = (char) (((int) text.charAt(i) + difference - 65) % 26 + 65);
28
+                result.append(ch);
29
+            } else {
30
+                char ch = (char) (((int) text.charAt(i) + difference - 97) % 26 + 97);
31
+                result.append(ch);
32
+            }
33
+        }
34
+        return result.toString();
17 35
     }
18 36
 
19
-    public String encrypt(String text) {
20
-        return text;
37
+
38
+    public  String encrypt(String text) {
39
+        return crypt(text);
21 40
     }
22 41
 
23 42
     public String decrypt(String text) {
24
-        return text;
43
+        return crypt(text);
44
+    }
45
+
46
+    public  String rotate(String s, Character c) {
47
+        int offset = (int) c - 65;
48
+        int i = offset % s.length();
49
+        return s.substring(i) + s.substring(0, i);
50
+    }
51
+
52
+    public String readFile(String filePath) throws IOException {
53
+        return new String(Files.readAllBytes(Paths.get(filePath)));
54
+
55
+
56
+        }
57
+    public File encryptedFile() throws IOException {
58
+        File file = new File("sonnet18.enc");
59
+        FileWriter writer = new FileWriter(file);
60
+        writer.write(encrypt(readFile("sonnet18.txt")));
61
+        writer.close();
62
+
63
+        return file;
25 64
     }
26 65
 
27
-    public static String rotate(String s, Character c) {
28 66
 
29
-        return "";
67
+
30 68
     }
31 69
 
32
-}
70
+
71
+
72
+
73
+
74
+
75
+
76
+
77
+
78
+
79
+
80
+
81
+
82
+
83
+
84
+
85
+
86
+

+ 13
- 1
Crypto/src/ROT13Test.java View File

@@ -1,5 +1,8 @@
1 1
 import org.junit.Test;
2 2
 
3
+import java.io.File;
4
+import java.io.IOException;
5
+
3 6
 import static org.junit.Assert.*;
4 7
 
5 8
 public class ROT13Test {
@@ -30,7 +33,7 @@ public class ROT13Test {
30 33
         String actual = cipher.rotate(s1, 'D');
31 34
 
32 35
         // Then
33
-        assertTrue(actual.equals(s2));
36
+        assertEquals(s2, actual);
34 37
     }
35 38
 
36 39
     @Test
@@ -88,4 +91,13 @@ public class ROT13Test {
88 91
         assertTrue(actual.equals(Q1));
89 92
     }
90 93
 
94
+    @Test
95
+    public void encryptedFile() throws IOException {
96
+        ROT13 cipher = new ROT13();
97
+        File newFile = cipher.encryptedFile();
98
+        assertTrue(newFile.exists());
99
+
100
+
101
+
102
+    }
91 103
 }

+ 15
- 0
SimpleCrypt.iml View File

@@ -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_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" 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>

+ 23
- 0
pom.xml View File

@@ -2,11 +2,34 @@
2 2
 <project xmlns="http://maven.apache.org/POM/4.0.0"
3 3
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 4
          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5
+
6
+
7
+
8
+    <build>
9
+        <plugins>
10
+            <plugin>
11
+                <groupId>org.apache.maven.plugins</groupId>
12
+                <artifactId>maven-compiler-plugin</artifactId>
13
+                <configuration>
14
+                    <source>1.8</source>
15
+                    <target>1.8</target>
16
+                </configuration>
17
+            </plugin>
18
+        </plugins>
19
+    </build>
5 20
     <modelVersion>4.0.0</modelVersion>
6 21
 
7 22
     <groupId>com.zipcodewilmington</groupId>
8 23
     <artifactId>SimpleCrypt</artifactId>
9 24
     <version>1.0-SNAPSHOT</version>
25
+    <dependencies>
26
+        <dependency>
27
+            <groupId>junit</groupId>
28
+            <artifactId>junit</artifactId>
29
+            <version>4.12</version>
30
+            <scope>compile</scope>
31
+        </dependency>
32
+    </dependencies>
10 33
 
11 34
 
12 35
 </project>

+ 14
- 0
sonnet18.enc View File

@@ -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.