Jared Norris пре 6 година
родитељ
комит
47ffb00b0f
3 измењених фајлова са 130 додато и 7 уклоњено
  1. 58
    7
      Crypto/src/ROT13.java
  2. 58
    0
      Crypto/src/ROT13Test.java
  3. 14
    0
      sonnet18.enc

+ 58
- 7
Crypto/src/ROT13.java Прегледај датотеку

@@ -1,16 +1,26 @@
1
+import java.io.IOException;
2
+import java.nio.file.*;
3
+import java.util.*;
4
+
1 5
 public class ROT13  {
2 6
     private String cryptKey;
3 7
     private String alphabet = "abcdefghijklmnopqrstuvwxyz";
8
+    private String sonnet = "/Users/jaredn/Labs/week5/ZCW-SimpleCrypt0/sonnet18.txt";
9
+    private String cryptSonnet = "/Users/jaredn/Labs/week5/ZCW-SimpleCrypt0/sonnet18.enc";
4 10
 
5 11
 
6 12
     ROT13(Character cs, Character cf) {
7
-        int index = Math.abs((int)cs - (int)cf);
8
-        cryptKey = rotate(alphabet, alphabet.charAt(index));
13
+        cryptKey = rotate(alphabet, alphabet.charAt(Math.abs((int)cs - (int)cf)));
9 14
     }
10 15
 
11 16
     ROT13(){ cryptKey = rotate(alphabet, 'n'); }
12 17
 
13 18
     public String crypt(String text) throws UnsupportedOperationException {
19
+        return encrypt(text);
20
+    }
21
+
22
+    @SuppressWarnings("Duplicates")
23
+    public String encrypt(String text) {
14 24
         StringBuilder result = new StringBuilder();
15 25
 
16 26
         for(Character c : text.toCharArray()) {
@@ -25,15 +35,56 @@ public class ROT13  {
25 35
         return result.toString();
26 36
     }
27 37
 
28
-    public String encrypt(String text) {
29
-        return crypt(text);
30
-    }
31
-
38
+    @SuppressWarnings("Duplicates")
32 39
     public String decrypt(String text) {
33
-        return crypt(text);
40
+        StringBuilder result = new StringBuilder();
41
+
42
+        for(Character c : text.toCharArray()) {
43
+            if (Character.isLetter(c)) {
44
+                if (Character.isUpperCase(c)) {
45
+                    result.append(Character.toUpperCase(alphabet.charAt(cryptKey.indexOf(Character.toLowerCase(c)))));
46
+                }
47
+                else result.append(alphabet.charAt(cryptKey.indexOf(c)));
48
+            }
49
+            else result.append(c);
50
+        }
51
+        return result.toString();
34 52
     }
35 53
 
36 54
     public static String rotate(String s, Character c) {
37 55
         return s.substring(s.indexOf(c)) + s.substring(0, s.indexOf(c));
38 56
     }
57
+
58
+    public void encryptSonnet() throws IOException {
59
+        Files.write(Paths.get(cryptSonnet), encryptFile(sonnet).getBytes());
60
+    }
61
+
62
+    public String decryptSonnet() {
63
+        return decryptFile(cryptSonnet);
64
+    }
65
+
66
+    public String encryptFile(String filePath) {
67
+        try {
68
+            return encrypt(parseFile(filePath));
69
+        } catch (IOException e) {
70
+            System.out.println(e.getMessage());
71
+        }
72
+        return null;
73
+    }
74
+
75
+    public String decryptFile(String encryptedFilePath) {
76
+        try {
77
+            return decrypt(parseFile(encryptedFilePath));
78
+        } catch (IOException e) {
79
+            System.out.println(e.getMessage());
80
+        }
81
+        return null;
82
+    }
83
+
84
+    public String parseFile(String filePath) throws IOException {
85
+        List<String> content = Files.readAllLines(Paths.get(filePath));
86
+        StringBuilder result = new StringBuilder();
87
+        content.forEach(e -> result.append(e).append("\n"));
88
+        return result.toString();
89
+    }
39 90
 }

+ 58
- 0
Crypto/src/ROT13Test.java Прегледај датотеку

@@ -1,6 +1,8 @@
1 1
 import static org.junit.jupiter.api.Assertions.*;
2 2
 import org.junit.jupiter.api.Test;
3 3
 
4
+import java.io.IOException;
5
+
4 6
 public class ROT13Test {
5 7
 
6 8
 
@@ -87,4 +89,60 @@ public class ROT13Test {
87 89
         assertTrue(actual.equals(Q1));
88 90
     }
89 91
 
92
+    //-------------------------------------------------------------------
93
+
94
+    @Test
95
+    public void decryptTest1() {
96
+        ROT13 x = new ROT13('a', 'd');
97
+
98
+        String expected = "this is a test string";
99
+        String actual = x.decrypt(x.encrypt(expected));
100
+
101
+        assertEquals(expected, actual);
102
+    }
103
+    @Test
104
+    public void decryptTest2() {
105
+        ROT13 x = new ROT13('a', 'z');
106
+
107
+        String expected = "TeStz   AAaarreE Fu!n";
108
+        String actual = x.decrypt(x.encrypt(expected));
109
+
110
+        assertEquals(expected, actual);
111
+    }
112
+    @Test
113
+    public void parseFileTest() {
114
+        ROT13 x = new ROT13();
115
+        String actual = "";
116
+
117
+        try {
118
+            actual = x.parseFile("/Users/jaredn/Labs/week5/ZCW-SimpleCrypt0/sonnet18.txt");
119
+        } catch (IOException e) {
120
+            System.out.println(e.getMessage());
121
+        }
122
+
123
+        assertNotNull(actual);
124
+
125
+    }
126
+    @Test
127
+    public void encryptFileTest() {
128
+        ROT13 x = new ROT13();
129
+        String actual = x.encryptFile("/Users/jaredn/Labs/week5/ZCW-SimpleCrypt0/sonnet18.txt");
130
+
131
+        assertNotNull(actual);
132
+    }
133
+    @Test
134
+    public void decryptFileTest() {
135
+        ROT13 x = new ROT13();
136
+        String expected = null;
137
+        try {
138
+            x.encryptSonnet();
139
+            expected = x.parseFile("/Users/jaredn/Labs/week5/ZCW-SimpleCrypt0/sonnet18.txt");
140
+        } catch (IOException e) {
141
+            System.out.println(e.getMessage());
142
+        }
143
+        String actual = x.decryptSonnet();
144
+
145
+        assertEquals(expected, actual);
146
+    }
147
+
90 148
 }

+ 14
- 0
sonnet18.enc Прегледај датотеку

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