Parcourir la source

changes to the crypto lab

Roy il y a 6 ans
Parent
révision
9bab3f16f9
3 fichiers modifiés avec 135 ajouts et 1 suppressions
  1. 48
    1
      Crypto/src/ROT13.java
  2. 73
    0
      Crypto/src/ROT13Test.java
  3. 14
    0
      sonnet18.enc

+ 48
- 1
Crypto/src/ROT13.java Voir le fichier

@@ -1,6 +1,8 @@
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  {
6 8
     private int shift;
@@ -50,4 +52,49 @@ public class ROT13  {
50 52
         return subStr1.concat(subStr);
51 53
     }
52 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
+    }
82
+
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
+        }
98
+    }
99
+
53 100
 }

+ 73
- 0
Crypto/src/ROT13Test.java Voir le fichier

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

+ 14
- 0
sonnet18.enc Voir le fichier

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