David Thornley hace 6 años
padre
commit
c41e09c255
Se han modificado 6 ficheros con 139 adiciones y 8 borrados
  1. 48
    8
      Crypto/src/ROT13.java
  2. 27
    0
      Crypto/src/ROT13Test.java
  3. 15
    0
      SimpleCrypt.iml
  4. 21
    0
      pom.xml
  5. 14
    0
      sonnet18.dcr
  6. 14
    0
      sonnet18.enc

+ 48
- 8
Crypto/src/ROT13.java Ver fichero

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

+ 27
- 0
Crypto/src/ROT13Test.java Ver fichero

@@ -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 {
@@ -61,6 +64,7 @@ public class ROT13Test {
61 64
 
62 65
         // When
63 66
         String actual = cipher.encrypt(Q1);
67
+        System.out.println(actual);
64 68
         System.out.println(Q1);
65 69
         System.out.println(A1);
66 70
         // Then
@@ -68,6 +72,7 @@ public class ROT13Test {
68 72
 
69 73
         // When
70 74
         String actual2 = cipher.decrypt(Q2);
75
+        System.out.println(actual2);
71 76
         System.out.println(Q2);
72 77
         System.out.println(A2);
73 78
         // Then
@@ -88,4 +93,26 @@ public class ROT13Test {
88 93
         assertTrue(actual.equals(Q1));
89 94
     }
90 95
 
96
+    @Test
97
+    public void newEncryptedFile() throws IOException {
98
+        //Given
99
+        ROT13 cipher = new ROT13();
100
+
101
+        String filePath = "/Users/davidT/Labs/ZCW-SimpleCrypt0/sonnet18.txt";
102
+
103
+        File newFile = cipher.newEncryptedFile(filePath, "sonnet18.enc");
104
+        String encyrptedFile = cipher.fileToString(newFile);
105
+        assertTrue(newFile.exists());
106
+    }
107
+
108
+    @Test
109
+    public void decryptTheNewFile() throws IOException {
110
+        ROT13 cipher = new ROT13();
111
+
112
+        String filePath = "/Users/davidT/Labs/ZCW-SimpleCrypt0/sonnet18.enc";
113
+
114
+        File newFile = cipher.newEncryptedFile(filePath, "sonnet18.dcr");
115
+        String encyrptedFile = cipher.fileToString(newFile);
116
+        assertTrue(newFile.exists());
117
+    }
91 118
 }

+ 15
- 0
SimpleCrypt.iml Ver fichero

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

+ 21
- 0
pom.xml Ver fichero

@@ -2,11 +2,32 @@
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>
33
+

+ 14
- 0
sonnet18.dcr Ver fichero

@@ -0,0 +1,14 @@
1
+Shall I compare thee to a summer’s day?
2
+Thou art more lovely and more temperate:
3
+Rough winds do shake the darling buds of May,
4
+And summer’s lease hath all too short a date;
5
+Sometime too hot the eye of heaven shines,
6
+And often is his gold complexion dimm'd;
7
+And every fair from fair sometime declines,
8
+By chance or nature’s changing course untrimm'd;
9
+But thy eternal summer shall not fade,
10
+Nor lose possession of that fair thou ow’st;
11
+Nor shall death brag thou wander’st in his shade,
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.

+ 14
- 0
sonnet18.enc Ver fichero

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