Explorar el Código

rot1, caesar and numeric ciphers

yauhenip hace 6 años
padre
commit
987b436498

+ 0
- 32
Crypto/src/ROT13.java Ver fichero

@@ -1,32 +0,0 @@
1
-import static java.lang.Character.isLowerCase;
2
-import static java.lang.Character.isUpperCase;
3
-import static java.lang.Character.toLowerCase;
4
-
5
-public class ROT13  {
6
-
7
-    ROT13(Character cs, Character cf) {
8
-    }
9
-
10
-    ROT13() {
11
-    }
12
-
13
-
14
-    public String crypt(String text) throws UnsupportedOperationException {
15
-
16
-        return "";
17
-    }
18
-
19
-    public String encrypt(String text) {
20
-        return text;
21
-    }
22
-
23
-    public String decrypt(String text) {
24
-        return text;
25
-    }
26
-
27
-    public static String rotate(String s, Character c) {
28
-
29
-        return "";
30
-    }
31
-
32
-}

+ 6
- 0
Crypto/src/java/Caesar.java Ver fichero

@@ -0,0 +1,6 @@
1
+public class Caesar extends ROT13 {
2
+
3
+    public Caesar(Character cs, Character cf) {
4
+        super(cs, cf);
5
+    }
6
+}

+ 6
- 0
Crypto/src/java/NumericCipher.java Ver fichero

@@ -0,0 +1,6 @@
1
+public class NumericCipher extends  ROT13{
2
+
3
+    public NumericCipher(int shift) {
4
+        super(shift);
5
+    }
6
+}

+ 63
- 0
Crypto/src/java/ROT13.java Ver fichero

@@ -0,0 +1,63 @@
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;
6
+
7
+public class ROT13  {
8
+    protected int shift;
9
+
10
+    ROT13(Character cs, Character cf) {
11
+        this.shift = cf - cs;
12
+    }
13
+
14
+    ROT13() {
15
+        this.shift = 13;
16
+    }
17
+
18
+    ROT13(int shift) {
19
+        this.shift = shift;
20
+    }
21
+
22
+    public String crypt(String text) throws UnsupportedOperationException {
23
+        char[] arrayToEncrypt = text.toCharArray();
24
+            for (int i = 0; i < arrayToEncrypt.length; i++) {
25
+                if (Character.isLowerCase(arrayToEncrypt[i])) {
26
+                    arrayToEncrypt[i] = (char) (((int) arrayToEncrypt[i] + shift - (int) 'a') % 26 + (int) 'a');
27
+                } else if (Character.isUpperCase(arrayToEncrypt[i])) {
28
+                    arrayToEncrypt[i] = (char) (((int) arrayToEncrypt[i] + shift - (int) 'A') % 26 + (int) 'A');
29
+                }
30
+            }
31
+        return String.valueOf(arrayToEncrypt);
32
+    }
33
+
34
+    public String encrypt(String text) {
35
+        return crypt(text);
36
+    }
37
+
38
+    public String decrypt(String text) {
39
+        shift = 26 - shift;
40
+        return crypt(text);
41
+    }
42
+
43
+    public static String rotate(String s, Character c) {
44
+        int n = s.indexOf(c);
45
+        return s.substring(n) + s.substring(0,n);
46
+    }
47
+
48
+    public String encryptFile() throws IOException {
49
+        String rawData = new String(Files.readAllBytes(Paths.get("sonnet18.txt")));
50
+        String encryptedData = encrypt(rawData);
51
+        File file = new File("sonnet18.enc");
52
+        FileWriter fileWriter = new FileWriter(file);
53
+        fileWriter.write(encryptedData);
54
+        fileWriter.flush();
55
+        fileWriter.close();
56
+        return encryptedData;
57
+    }
58
+
59
+    public String decryptFile() throws IOException {
60
+        String rawData = new String(Files.readAllBytes(Paths.get("sonnet18.enc")));
61
+        return decrypt(rawData);
62
+    }
63
+}

Crypto/src/ROT13Test.java → Crypto/src/test/ROT13Test.java Ver fichero

@@ -1,6 +1,5 @@
1
-import org.junit.Test;
2
-
3
-import static org.junit.Assert.*;
1
+import org.junit.jupiter.api.Test;
2
+import static org.junit.jupiter.api.Assertions.assertTrue;
4 3
 
5 4
 public class ROT13Test {
6 5
 
@@ -87,5 +86,4 @@ public class ROT13Test {
87 86
         // Then
88 87
         assertTrue(actual.equals(Q1));
89 88
     }
90
-
91 89
 }

+ 142
- 0
Crypto/src/test/TestCaesar.java Ver fichero

@@ -0,0 +1,142 @@
1
+import org.junit.jupiter.api.Test;
2
+
3
+import java.io.IOException;
4
+
5
+import static org.junit.jupiter.api.Assertions.*;
6
+
7
+public class TestCaesar {
8
+
9
+    @Test
10
+    public void encryptTest1() {
11
+        // Given
12
+        Caesar caesar = new Caesar('A', 'X');
13
+
14
+        String Q1 = "ABCDEFGHIJKLMNOPQRSTUVWXYZj";
15
+        String A1 = "XYZABCDEFGHIJKLMNOPQRSTUVWg";
16
+
17
+        // When
18
+        String actual = caesar.encrypt(Q1);
19
+
20
+        // Then
21
+        assertEquals(A1, actual);
22
+    }
23
+
24
+    @Test
25
+    public void encryptTest2() {
26
+        // Given
27
+        Caesar caesar = new Caesar('a', 'b');
28
+
29
+        String Q1 = "!defend the east wall of the castle?";
30
+        String A1 = "!efgfoe uif fbtu xbmm pg uif dbtumf?";
31
+
32
+        // When
33
+        String actual = caesar.encrypt(Q1);
34
+
35
+        // Then
36
+        assertEquals(A1, actual);
37
+    }
38
+
39
+    @Test
40
+    public void encryptTest3() {
41
+        // Given
42
+        Caesar caesar = new Caesar('a', 'x');
43
+
44
+        String Q1 = "QEB NRFZH YOLTK CLU GRJMP LSBO QEB IXWV ALD";
45
+        String A1 = "THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG";
46
+
47
+        // When
48
+        String actual = caesar.encrypt(A1);
49
+
50
+        // Then
51
+        assertEquals(Q1, actual);
52
+    }
53
+
54
+    @Test
55
+    public void decryptTest1() {
56
+        // Given
57
+        Caesar caesar = new Caesar('A', 'X');
58
+
59
+        String Q1 = "ABCDEFGHIJKLMNOPQRSTUVWXYZj";
60
+        String A1 = "XYZABCDEFGHIJKLMNOPQRSTUVWg";
61
+
62
+        // When
63
+        String actual = caesar.decrypt(A1);
64
+
65
+        // Then
66
+        assertEquals(Q1, actual);
67
+    }
68
+
69
+    @Test
70
+    public void decryptTest2() {
71
+        // Given
72
+        Caesar caesar = new Caesar('a', 'x');
73
+
74
+        String Q1 = "QEB NRFZH YOLTK CLU GRJMP LSBO QEB IXWV ALD";
75
+        String A1 = "THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG";
76
+
77
+        // When
78
+        String actual = caesar.decrypt(Q1);
79
+
80
+        // Then
81
+        assertEquals(A1, actual);
82
+    }
83
+
84
+    @Test
85
+    public void cryptTest2() {
86
+        // Given
87
+        Caesar cipher = new Caesar('h', 'm');
88
+        String Q1 = "Why did the chicken cross the road?";
89
+        // When
90
+        String actual = cipher.decrypt(cipher.encrypt(cipher.decrypt(cipher.encrypt(Q1))));
91
+        // Then
92
+        assertTrue(actual.equals(Q1));
93
+    }
94
+
95
+    @Test
96
+    public void testEncryptFile() throws IOException {
97
+        // Given
98
+        Caesar cipher = new Caesar('a', 'f');
99
+        String Q1 = "Xmfqq N htrufwj ymjj yt f xzrrjw’x ifd?\n" +
100
+                "Ymtz fwy rtwj qtajqd fsi rtwj yjrujwfyj:\n" +
101
+                "Wtzlm bnsix it xmfpj ymj ifwqnsl gzix tk Rfd,\n" +
102
+                "Fsi xzrrjw’x qjfxj mfym fqq ytt xmtwy f ifyj;\n" +
103
+                "Xtrjynrj ytt mty ymj jdj tk mjfajs xmnsjx,\n" +
104
+                "Fsi tkyjs nx mnx ltqi htruqjcnts inrr'i;\n" +
105
+                "Fsi jajwd kfnw kwtr kfnw xtrjynrj ijhqnsjx,\n" +
106
+                "Gd hmfshj tw sfyzwj’x hmfslnsl htzwxj zsywnrr'i;\n" +
107
+                "Gzy ymd jyjwsfq xzrrjw xmfqq sty kfij,\n" +
108
+                "Stw qtxj utxxjxxnts tk ymfy kfnw ymtz tb’xy;\n" +
109
+                "Stw xmfqq ijfym gwfl ymtz bfsijw’xy ns mnx xmfij,\n" +
110
+                "Bmjs ns jyjwsfq qnsjx yt ynrj ymtz lwtb’xy:\n" +
111
+                "   Xt qtsl fx rjs hfs gwjfymj tw jdjx hfs xjj,\n" +
112
+                "   Xt qtsl qnajx ymnx, fsi ymnx lnajx qnkj yt ymjj.\n";
113
+        // When
114
+        String actual = cipher.encryptFile();
115
+        // Then
116
+        assertTrue(actual.equals(Q1));
117
+    }
118
+
119
+    @Test
120
+    public void testDecryptFile() throws IOException {
121
+        // Given
122
+        Caesar cipher = new Caesar('b', 'g');
123
+        String Q1 = "Shall I compare thee to a summer’s day?\n" +
124
+                "Thou art more lovely and more temperate:\n" +
125
+                "Rough winds do shake the darling buds of May,\n" +
126
+                "And summer’s lease hath all too short a date;\n" +
127
+                "Sometime too hot the eye of heaven shines,\n" +
128
+                "And often is his gold complexion dimm'd;\n" +
129
+                "And every fair from fair sometime declines,\n" +
130
+                "By chance or nature’s changing course untrimm'd;\n" +
131
+                "But thy eternal summer shall not fade,\n" +
132
+                "Nor lose possession of that fair thou ow’st;\n" +
133
+                "Nor shall death brag thou wander’st in his shade,\n" +
134
+                "When in eternal lines to time thou grow’st:\n" +
135
+                "   So long as men can breathe or eyes can see,\n" +
136
+                "   So long lives this, and this gives life to thee.\n";
137
+        // When
138
+        String actual = cipher.decryptFile();
139
+        // Then
140
+        assertEquals(Q1, actual);
141
+    }
142
+}

+ 98
- 0
Crypto/src/test/TestNumericCipher.java Ver fichero

@@ -0,0 +1,98 @@
1
+import org.junit.jupiter.api.Test;
2
+
3
+import java.io.IOException;
4
+
5
+import static org.junit.jupiter.api.Assertions.assertEquals;
6
+import static org.junit.jupiter.api.Assertions.assertTrue;
7
+
8
+public class TestNumericCipher {
9
+
10
+    @Test
11
+    public void encryptTest() {
12
+        // Given
13
+        NumericCipher cipher = new NumericCipher(23);
14
+
15
+        String Q1 = "QEB NRFZH YOLTK CLU GRJMP LSBO QEB IXWV ALD";
16
+        String A1 = "THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG";
17
+
18
+        // When
19
+        String actual = cipher.encrypt(A1);
20
+
21
+        // Then
22
+        assertEquals(Q1, actual);
23
+    }
24
+
25
+    @Test
26
+    public void decryptTest2() {
27
+        // Given
28
+        NumericCipher cipher = new NumericCipher(23);
29
+
30
+        String Q1 = "QEB NRFZH YOLTK CLU GRJMP LSBO QEB IXWV ALD";
31
+        String A1 = "THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG";
32
+
33
+        // When
34
+        String actual = cipher.decrypt(Q1);
35
+
36
+        // Then
37
+        assertEquals(A1, actual);
38
+    }
39
+
40
+    @Test
41
+    public void cryptTest2() {
42
+        // Given
43
+        NumericCipher cipher = new NumericCipher(4);
44
+        String Q1 = "Why did the chicken cross the road?";
45
+        // When
46
+        String actual = (cipher.decrypt(cipher.encrypt(Q1)));
47
+        // Then
48
+        assertTrue(actual.equals(Q1));
49
+    }
50
+
51
+    @Test
52
+    public void testEncryptFile() throws IOException {
53
+        // Given
54
+        ROT13 cipher = new NumericCipher(5);
55
+        String Q1 = "Xmfqq N htrufwj ymjj yt f xzrrjw’x ifd?\n" +
56
+                "Ymtz fwy rtwj qtajqd fsi rtwj yjrujwfyj:\n" +
57
+                "Wtzlm bnsix it xmfpj ymj ifwqnsl gzix tk Rfd,\n" +
58
+                "Fsi xzrrjw’x qjfxj mfym fqq ytt xmtwy f ifyj;\n" +
59
+                "Xtrjynrj ytt mty ymj jdj tk mjfajs xmnsjx,\n" +
60
+                "Fsi tkyjs nx mnx ltqi htruqjcnts inrr'i;\n" +
61
+                "Fsi jajwd kfnw kwtr kfnw xtrjynrj ijhqnsjx,\n" +
62
+                "Gd hmfshj tw sfyzwj’x hmfslnsl htzwxj zsywnrr'i;\n" +
63
+                "Gzy ymd jyjwsfq xzrrjw xmfqq sty kfij,\n" +
64
+                "Stw qtxj utxxjxxnts tk ymfy kfnw ymtz tb’xy;\n" +
65
+                "Stw xmfqq ijfym gwfl ymtz bfsijw’xy ns mnx xmfij,\n" +
66
+                "Bmjs ns jyjwsfq qnsjx yt ynrj ymtz lwtb’xy:\n" +
67
+                "   Xt qtsl fx rjs hfs gwjfymj tw jdjx hfs xjj,\n" +
68
+                "   Xt qtsl qnajx ymnx, fsi ymnx lnajx qnkj yt ymjj.\n";
69
+        // When
70
+        String actual = cipher.encryptFile();
71
+        // Then
72
+        assertTrue(actual.equals(Q1));
73
+    }
74
+
75
+    @Test
76
+    public void testDecryptFile() throws IOException {
77
+        // Given
78
+        ROT13 cipher = new NumericCipher(5);
79
+        String Q1 = "Shall I compare thee to a summer’s day?\n" +
80
+                "Thou art more lovely and more temperate:\n" +
81
+                "Rough winds do shake the darling buds of May,\n" +
82
+                "And summer’s lease hath all too short a date;\n" +
83
+                "Sometime too hot the eye of heaven shines,\n" +
84
+                "And often is his gold complexion dimm'd;\n" +
85
+                "And every fair from fair sometime declines,\n" +
86
+                "By chance or nature’s changing course untrimm'd;\n" +
87
+                "But thy eternal summer shall not fade,\n" +
88
+                "Nor lose possession of that fair thou ow’st;\n" +
89
+                "Nor shall death brag thou wander’st in his shade,\n" +
90
+                "When in eternal lines to time thou grow’st:\n" +
91
+                "   So long as men can breathe or eyes can see,\n" +
92
+                "   So long lives this, and this gives life to thee.\n";
93
+        // When
94
+        String actual = cipher.decryptFile();
95
+        // Then
96
+        assertEquals(Q1, actual);
97
+    }
98
+}

+ 7
- 0
pom.xml Ver fichero

@@ -7,6 +7,13 @@
7 7
     <groupId>com.zipcodewilmington</groupId>
8 8
     <artifactId>SimpleCrypt</artifactId>
9 9
     <version>1.0-SNAPSHOT</version>
10
+    <dependencies>
11
+        <dependency>
12
+            <groupId>org.junit.jupiter</groupId>
13
+            <artifactId>junit-jupiter-api</artifactId>
14
+            <version>5.2.0</version>
15
+        </dependency>
16
+    </dependencies>
10 17
 
11 18
 
12 19
 </project>