#14 roympanju_1

Offen
roympanju_1 möchte 2 Commits von roympanju_1/ZCW-SimpleCrypt0:master nach master zusammenführen
5 geänderte Dateien mit 183 neuen und 5 gelöschten Zeilen
  1. 73
    5
      Crypto/src/ROT13.java
  2. 73
    0
      Crypto/src/ROT13Test.java
  3. 15
    0
      SimpleCrypt.iml
  4. 8
    0
      pom.xml
  5. 14
    0
      sonnet18.enc

+ 73
- 5
Crypto/src/ROT13.java Datei anzeigen

@@ -1,32 +1,100 @@
1
+import java.io.*;
2
+
1 3
 import static java.lang.Character.isLowerCase;
2 4
 import static java.lang.Character.isUpperCase;
3
-import static java.lang.Character.toLowerCase;
5
+
4 6
 
5 7
 public class ROT13  {
8
+    private int shift;
6 9
 
7 10
     ROT13(Character cs, Character cf) {
11
+        this.shift = cf - cs;
8 12
     }
9 13
 
10 14
     ROT13() {
15
+        this.shift = 13;
11 16
     }
12 17
 
13 18
 
14 19
     public String crypt(String text) throws UnsupportedOperationException {
20
+        StringBuilder sb = new StringBuilder();
21
+        for(int i=0; i<text.length(); i++){
22
+            if(isLowerCase(text.charAt(i))){
23
+                sb.append((char)(((int)(text.charAt(i))+ shift - 97)%26+97));
24
+            }
25
+            else if(isUpperCase(text.charAt(i))){
26
+                sb.append((char)(((int)(text.charAt(i))+ shift - 65)%26+65));
27
+            }
28
+            else {
29
+                sb.append(text.charAt(i));
30
+            }
31
+        }
15 32
 
16
-        return "";
33
+        return sb.toString();
17 34
     }
18 35
 
19 36
     public String encrypt(String text) {
20
-        return text;
37
+
38
+        return crypt(text);
21 39
     }
22 40
 
23 41
     public String decrypt(String text) {
24
-        return text;
42
+
43
+
44
+        return crypt(text);
25 45
     }
26 46
 
27 47
     public static String rotate(String s, Character c) {
48
+        String subStr = s.substring(0, s.indexOf(c));
49
+        String subStr1 = s.substring(s.indexOf(c));
50
+
51
+
52
+        return subStr1.concat(subStr);
53
+    }
54
+
55
+    public String fileReader(String file2){
56
+        File file = new File(file2);
57
+        BufferedReader br = null;
58
+        String str;
59
+        StringBuilder build = new StringBuilder();
60
+        try {
61
+            br = new BufferedReader(new FileReader(file));
62
+            while ((str = br.readLine()) != null){
63
+                build.append(str).append('\n');
64
+            }
65
+            build.deleteCharAt(build.length()-1);
66
+        }catch (FileNotFoundException e){
67
+            e.printStackTrace();
68
+        }catch (IOException e){
69
+            e.printStackTrace();
70
+        }
71
+        finally {
72
+            try {
73
+                br.close();
74
+            }catch (IOException e){
75
+                e.printStackTrace();
76
+            }
77
+        }
78
+        fileWrite(build.toString());
79
+
80
+        return build.toString();
81
+    }
28 82
 
29
-        return "";
83
+    public void fileWrite (String file){
84
+        BufferedWriter wr = null;
85
+        try {
86
+            wr = new BufferedWriter(new FileWriter("sonnet18.enc"));
87
+            wr.write(file);
88
+        }catch (IOException e){
89
+            e.printStackTrace();
90
+        }
91
+        finally {
92
+            try{
93
+                wr.close();
94
+            }catch (IOException e){
95
+                e.printStackTrace();
96
+            }
97
+        }
30 98
     }
31 99
 
32 100
 }

+ 73
- 0
Crypto/src/ROT13Test.java Datei anzeigen

@@ -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 {
@@ -87,5 +89,76 @@ public class ROT13Test {
87 89
         // Then
88 90
         assertTrue(actual.equals(Q1));
89 91
     }
92
+    @Test
93
+    public void fileReadTest(){
94
+        ROT13 fileCipher = new ROT13();
95
+
96
+        String file = "Shall I compare thee to a summer’s day?\n" +
97
+                "Thou art more lovely and more temperate:\n" +
98
+                "Rough winds do shake the darling buds of May,\n" +
99
+                "And summer’s lease hath all too short a date;\n" +
100
+                "Sometime too hot the eye of heaven shines,\n" +
101
+                "And often is his gold complexion dimm'd;\n" +
102
+                "And every fair from fair sometime declines,\n" +
103
+                "By chance or nature’s changing course untrimm'd;\n" +
104
+                "But thy eternal summer shall not fade,\n" +
105
+                "Nor lose possession of that fair thou ow’st;\n" +
106
+                "Nor shall death brag thou wander’st in his shade,\n" +
107
+                "When in eternal lines to time thou grow’st:\n" +
108
+                "   So long as men can breathe or eyes can see,\n" +
109
+                "   So long lives this, and this gives life to thee.";
110
+        String read = fileCipher.fileReader("/Users/roym/labs_week3/ZCW-SimpleCrypt0/sonnet18.txt");
111
+        //String actual = fileCipher.crypt(read);
112
+        assertEquals(file, read);
113
+
114
+    }
115
+
116
+    @Test
117
+    public void encryptTest(){
118
+        ROT13 fileCipher = new ROT13();
119
+        String file = "Shall I compare thee to a summer’s day?\n" +
120
+                "Thou art more lovely and more temperate:\n" +
121
+                "Rough winds do shake the darling buds of May,\n" +
122
+                "And summer’s lease hath all too short a date;\n" +
123
+                "Sometime too hot the eye of heaven shines,\n" +
124
+                "And often is his gold complexion dimm'd;\n" +
125
+                "And every fair from fair sometime declines,\n" +
126
+                "By chance or nature’s changing course untrimm'd;\n" +
127
+                "But thy eternal summer shall not fade,\n" +
128
+                "Nor lose possession of that fair thou ow’st;\n" +
129
+                "Nor shall death brag thou wander’st in his shade,\n" +
130
+                "When in eternal lines to time thou grow’st:\n" +
131
+                "   So long as men can breathe or eyes can see,\n" +
132
+                "   So long lives this, and this gives life to thee.";
133
+        String expected =  fileCipher.encrypt(file);
134
+        String read = fileCipher.fileReader("/Users/roym/labs_week3/ZCW-SimpleCrypt0/sonnet18.txt");
135
+        String actual = fileCipher.encrypt(read);
136
+        fileCipher.fileWrite(actual);
137
+
138
+        assertEquals(expected, actual);
139
+    }
140
+
141
+    @Test
142
+    public void fileDecrypt(){
143
+        ROT13 fileCipher = new ROT13();
144
+        String expected = "Shall I compare thee to a summer’s day?\n" +
145
+                "Thou art more lovely and more temperate:\n" +
146
+                "Rough winds do shake the darling buds of May,\n" +
147
+                "And summer’s lease hath all too short a date;\n" +
148
+                "Sometime too hot the eye of heaven shines,\n" +
149
+                "And often is his gold complexion dimm'd;\n" +
150
+                "And every fair from fair sometime declines,\n" +
151
+                "By chance or nature’s changing course untrimm'd;\n" +
152
+                "But thy eternal summer shall not fade,\n" +
153
+                "Nor lose possession of that fair thou ow’st;\n" +
154
+                "Nor shall death brag thou wander’st in his shade,\n" +
155
+                "When in eternal lines to time thou grow’st:\n" +
156
+                "   So long as men can breathe or eyes can see,\n" +
157
+                "   So long lives this, and this gives life to thee.";
158
+        String read = fileCipher.fileReader("/Users/roym/labs_week3/ZCW-SimpleCrypt0/sonnet18.enc");
159
+        String actual = fileCipher.decrypt(read);
160
+        assertEquals(expected, actual);
161
+    }
162
+
90 163
 
91 164
 }

+ 15
- 0
SimpleCrypt.iml Datei anzeigen

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

+ 8
- 0
pom.xml Datei anzeigen

@@ -7,6 +7,14 @@
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>compile</scope>
16
+        </dependency>
17
+    </dependencies>
10 18
 
11 19
 
12 20
 </project>

+ 14
- 0
sonnet18.enc Datei anzeigen

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