Ben Blinebury 6 anos atrás
pai
commit
2f09ef8210
10 arquivos alterados com 119 adições e 20 exclusões
  1. BIN
      .DS_Store
  2. BIN
      Crypto/.DS_Store
  3. 46
    8
      Crypto/src/ROT13.java
  4. 35
    11
      Crypto/src/ROT13Test.java
  5. 17
    0
      SimpleCrypt.iml
  6. 21
    1
      pom.xml
  7. 0
    0
      sonnet18.dcr
  8. 0
    0
      sonnet18.enc
  9. BIN
      target/.DS_Store
  10. BIN
      target/generated-sources/.DS_Store

BIN
.DS_Store Ver arquivo


BIN
Crypto/.DS_Store Ver arquivo


+ 46
- 8
Crypto/src/ROT13.java Ver arquivo

@@ -1,32 +1,70 @@
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.FileWriter;
3
+import java.io.IOException;
4
+import java.nio.file.Files;
5
+import java.nio.file.Paths;
4 6
 
5 7
 public class ROT13  {
8
+    String cyphered;
9
+    int diff;
6 10
 
7 11
     ROT13(Character cs, Character cf) {
12
+        this.diff = Math.abs(cs - cf);
8 13
     }
9 14
 
10 15
     ROT13() {
16
+        this.diff = 13;
11 17
     }
12 18
 
13 19
 
14 20
     public String crypt(String text) throws UnsupportedOperationException {
21
+        StringBuffer answer = new StringBuffer();
15 22
 
16
-        return "";
23
+        for(int i = 0; i < text.length(); i++){
24
+            if (!Character.isLetter(text.charAt(i))) {
25
+                answer.append(text.charAt(i));
26
+            } else if (Character.isUpperCase(text.charAt(i))) {
27
+                char ch = (char) (((int) text.charAt(i) + diff - 65) % 26 + 65);
28
+                answer.append(ch);
29
+            } else {
30
+                char ch = (char) (((int) text.charAt(i) + diff - 97) % 26 + 97);
31
+                answer.append(ch);
32
+            }
33
+        }
34
+
35
+        return answer.toString();
17 36
     }
18 37
 
19 38
     public String encrypt(String text) {
20
-        return 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 difference = (int) c - 65;
48
+        int i = difference % s.length();
49
+        return s.substring(i) + s.substring(0,i);
25 50
     }
26 51
 
27
-    public static String rotate(String s, Character c) {
52
+    public String fileString (File filePath) throws IOException {
53
+        return new String(Files.readAllBytes(Paths.get(String.valueOf(filePath))));
54
+    }
28 55
 
29
-        return "";
56
+    public File newFile(String original, String newFileName) {
57
+        File newFile = null;
58
+        try {
59
+            newFile = new File(newFileName);
60
+            FileWriter fileWriter = new FileWriter(newFile);
61
+            fileWriter.write(encrypt(fileString(new File(original))));
62
+            fileWriter.flush();
63
+            fileWriter.close();
64
+        } catch (IOException e) {
65
+            e.printStackTrace();
66
+        }
67
+        return newFile;
30 68
     }
31 69
 
32 70
 }

+ 35
- 11
Crypto/src/ROT13Test.java Ver arquivo

@@ -1,4 +1,6 @@
1 1
 import org.junit.Test;
2
+import java.io.File;
3
+import java.io.IOException;
2 4
 
3 5
 import static org.junit.Assert.*;
4 6
 
@@ -12,8 +14,8 @@ public class ROT13Test {
12 14
         String s2 = "ABCDEF";
13 15
 
14 16
         // When
15
-        ROT13 cipher = new ROT13();
16
-        String actual = cipher.rotate(s1, 'A');
17
+        ROT13 cypher = new ROT13();
18
+        String actual = cypher.rotate(s1, 'A');
17 19
 
18 20
         // Then
19 21
         assertTrue(actual.equals(s2));
@@ -26,8 +28,8 @@ public class ROT13Test {
26 28
         String s2 = "DEFABC";
27 29
 
28 30
         // When
29
-        ROT13 cipher = new ROT13();
30
-        String actual = cipher.rotate(s1, 'D');
31
+        ROT13 cypher = new ROT13();
32
+        String actual = cypher.rotate(s1, 'D');
31 33
 
32 34
         // Then
33 35
         assertTrue(actual.equals(s2));
@@ -40,8 +42,8 @@ public class ROT13Test {
40 42
         String s2 = "NOPQRSTUVWXYZABCDEFGHIJKLM";
41 43
 
42 44
         // When
43
-        ROT13 cipher = new ROT13();
44
-        String actual = cipher.rotate(s1, 'N');
45
+        ROT13 cypher = new ROT13();
46
+        String actual = cypher.rotate(s1, 'N');
45 47
         System.out.println(s1);
46 48
         System.out.println(actual);
47 49
         // Then
@@ -51,7 +53,7 @@ public class ROT13Test {
51 53
     @Test
52 54
     public void cryptTest1() {
53 55
         // Given
54
-        ROT13 cipher = new ROT13('a', 'n');
56
+        ROT13 cypher = new ROT13('a', 'n');
55 57
 
56 58
         String Q1 = "Why did the chicken cross the road?";
57 59
         String A1 = "Jul qvq gur puvpxra pebff gur ebnq?";
@@ -60,14 +62,14 @@ public class ROT13Test {
60 62
         String A2 = "To get to the other side!";
61 63
 
62 64
         // When
63
-        String actual = cipher.encrypt(Q1);
65
+        String actual = cypher.encrypt(Q1);
64 66
         System.out.println(Q1);
65 67
         System.out.println(A1);
66 68
         // Then
67 69
         assertTrue(actual.equals(A1));
68 70
 
69 71
         // When
70
-        String actual2 = cipher.decrypt(Q2);
72
+        String actual2 = cypher.decrypt(Q2);
71 73
         System.out.println(Q2);
72 74
         System.out.println(A2);
73 75
         // Then
@@ -76,16 +78,38 @@ public class ROT13Test {
76 78
     @Test
77 79
     public void cryptTest2() {
78 80
         // Given
79
-        ROT13 cipher = new ROT13('a', 'n');
81
+        ROT13 cypher = new ROT13('a', 'n');
80 82
 
81 83
         String Q1 = "Why did the chicken cross the road?";
82 84
         System.out.println(Q1);
83 85
 
84 86
         // When
85
-        String actual = cipher.crypt(cipher.crypt(Q1));
87
+        String actual = cypher.crypt(cypher.crypt(Q1));
86 88
         System.out.println(actual);
87 89
         // Then
88 90
         assertTrue(actual.equals(Q1));
89 91
     }
90 92
 
93
+    @Test
94
+    public void hideNewFile() throws IOException {
95
+        //Given
96
+        ROT13 cypher = new ROT13();
97
+        String filePath = "/Users/ben/Labs/ZCW-SimpleCrypt0/sonnet18.txt";
98
+
99
+        File newFile = cypher.newFile(filePath, "sonnet18.enc");
100
+        String encryptedFile = cypher.fileString(newFile);
101
+        assertTrue(newFile.exists());
102
+    }
103
+
104
+    @Test
105
+    public void decryptNewFile() throws IOException {
106
+        ROT13 cypher = new ROT13();
107
+
108
+        String filePath = "/Users/ben/Labs/ZCW-SimpleCrypt0/sonnet18.enc";
109
+
110
+        File newFile = cypher.newFile(filePath, "sonnet18.dcr");
111
+        String encryptedFile = cypher.fileString(newFile);
112
+        assertTrue(newFile.exists());
113
+    }
114
+
91 115
 }

+ 17
- 0
SimpleCrypt.iml Ver arquivo

@@ -0,0 +1,17 @@
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
+    <orderEntry type="library" name="Maven: junit:junit:4.12" level="project" />
15
+    <orderEntry type="library" name="Maven: org.hamcrest:hamcrest-core:1.3" level="project" />
16
+  </component>
17
+</module>

+ 21
- 1
pom.xml Ver arquivo

@@ -1,12 +1,32 @@
1
-<?xml version="1.0" encoding="UTF-8"?>
1
+
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
+        <build>
6
+            <plugins>
7
+                <plugin>
8
+                    <groupId>org.apache.maven.plugins</groupId>
9
+                    <artifactId>maven-compiler-plugin</artifactId>
10
+                    <configuration>
11
+                        <source>1.8</source>
12
+                        <target>1.8</target>
13
+                    </configuration>
14
+                </plugin>
15
+            </plugins>
16
+        </build>
5 17
     <modelVersion>4.0.0</modelVersion>
6 18
 
7 19
     <groupId>com.zipcodewilmington</groupId>
8 20
     <artifactId>SimpleCrypt</artifactId>
9 21
     <version>1.0-SNAPSHOT</version>
22
+        <dependencies>
23
+            <dependency>
24
+                <groupId>junit</groupId>
25
+                <artifactId>junit</artifactId>
26
+                <version>4.12</version>
27
+                <scope>compile</scope>
28
+            </dependency>
29
+        </dependencies>
10 30
 
11 31
 
12 32
 </project>

+ 0
- 0
sonnet18.dcr Ver arquivo


+ 0
- 0
sonnet18.enc Ver arquivo


BIN
target/.DS_Store Ver arquivo


BIN
target/generated-sources/.DS_Store Ver arquivo