#1 trtong - ROT13, Caesar, AtbashCipher

Open
trtong wants to merge 7 commits from trtong/SimpleCrypt:master into master

+ 0
- 32
Crypto/src/ROT13.java View File

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

+ 11
- 0
pom.xml View File

@@ -8,5 +8,16 @@
8 8
     <artifactId>SimpleCrypt</artifactId>
9 9
     <version>1.0-SNAPSHOT</version>
10 10
 
11
+    <properties>
12
+        <maven.compiler.source>1.8</maven.compiler.source>
13
+        <maven.compiler.target>1.8</maven.compiler.target>
14
+    </properties>
15
+    <dependencies>
16
+        <dependency>
17
+            <groupId>junit</groupId>
18
+            <artifactId>junit</artifactId>
19
+            <version>4.12</version>
20
+        </dependency>
21
+    </dependencies>
11 22
 
12 23
 </project>

+ 83
- 0
src/main/java/AtbashCipher.java View File

@@ -0,0 +1,83 @@
1
+import java.io.*;
2
+
3
+import java.util.HashMap;
4
+import java.util.stream.Stream;
5
+
6
+
7
+public class AtbashCipher{
8
+
9
+    private static final String abc = "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz";
10
+    private static final String cba = "ZzYyXxWwVvUuTtSsRrQqPpOoNnMmLlKkJjIiHhGgFfEeDdCcBbAa";
11
+    private static final HashMap<Character, Character> key = new HashMap<>();
12
+
13
+    public AtbashCipher() {
14
+        createKey();
15
+    }
16
+
17
+    private void createKey() {
18
+        char[] abcArr = abc.toCharArray();
19
+        char[] cbaArr = cba.toCharArray();
20
+
21
+        for (int i = 0; i < abcArr.length; i++) {
22
+            key.put(abcArr[i], cbaArr[i]);
23
+        }
24
+    }
25
+
26
+    public static String cipher(String s) {
27
+        char[] charArray = s.toCharArray();
28
+
29
+        for (int i = 0; i < charArray.length; i++) {
30
+            if (key.containsKey(charArray[i])) {
31
+                charArray[i] = key.get(charArray[i]);
32
+            }
33
+        }
34
+        return charArrayToString(charArray);
35
+    }
36
+
37
+    public  static String charArrayToString(char[] chars) {
38
+        StringBuilder sb = new StringBuilder();
39
+        for (char c: chars) {
40
+            sb.append(c);
41
+        }
42
+        return sb.toString();
43
+    }
44
+
45
+    public void cipherFile(String filePathIn, String filePathOut) throws FileNotFoundException {
46
+        String inFile = fileToString(filePathIn);
47
+
48
+        PrintStream file = new PrintStream(new File(filePathOut));
49
+        PrintStream console = System.out;
50
+
51
+        System.setOut(file);
52
+
53
+        String swappedChars = Stream.of(inFile)
54
+                .map(AtbashCipher::cipher)
55
+                .reduce("", String::concat);
56
+
57
+        System.out.println(swappedChars);
58
+        System.setOut(console);
59
+    }
60
+
61
+
62
+    private String fileToString(String filePath) throws FileNotFoundException {
63
+
64
+        File file = new File(filePath);
65
+
66
+        FileReader fReader = new FileReader(file);
67
+        BufferedReader bufferedReader = new BufferedReader(fReader);
68
+
69
+        StringBuilder sb = new StringBuilder();
70
+        String line;
71
+
72
+        try {
73
+            while ((line = bufferedReader.readLine()) != null) {
74
+                sb.append(line + "\n");
75
+            }
76
+        } catch (IOException e) {
77
+            System.out.println("Error reading file");
78
+        }
79
+
80
+        return sb.toString();
81
+    }
82
+
83
+}

+ 45
- 0
src/main/java/CaeserCipher.java View File

@@ -0,0 +1,45 @@
1
+public class CaeserCipher extends ROT13 {
2
+    // takes in a shift and then returns an encoded string of the letter shift
3
+
4
+    public CaeserCipher(int shift) {
5
+        super.setShift(shift);
6
+    }
7
+
8
+    public String encrypt(String s) {
9
+        StringBuilder sb = new StringBuilder();
10
+
11
+        for (int i = 0; i < s.length(); i++) {
12
+            char c = s.charAt(i);
13
+
14
+            if (c >= 'a' && c <= 'z') {
15
+                sb.append((char) ((c - 'a' + getShift()) % 26 + 'a'));
16
+
17
+            } else if (c >= 'A'  && c <= 'Z') {
18
+                sb.append((char)((c - 'A' + getShift()) % 26 + 'A'));
19
+
20
+            } else {
21
+                sb.append(c);
22
+            }
23
+        }
24
+        return sb.toString();
25
+    }
26
+
27
+    public String decrypt(String s) {
28
+        StringBuilder sb = new StringBuilder();
29
+        for (int i = 0; i < s.length(); i++) {
30
+            char c = s.charAt(i);
31
+
32
+            if (c >= 'a' && c <= 'z') {
33
+                sb.append((char) ((c + 'a' - getShift()) % 26 + 'a'));
34
+
35
+            } else if (c >= 'A'  && c <= 'Z') {
36
+                sb.append((char)((c + 'A' - getShift()) % 26 + 'A'));
37
+
38
+            } else {
39
+                sb.append(c);
40
+            }
41
+        }
42
+        return sb.toString();
43
+
44
+    }
45
+}

+ 75
- 0
src/main/java/ROT13.java View File

@@ -0,0 +1,75 @@
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
+    private int shift;
7
+
8
+    ROT13(Character cs, Character cf) {
9
+        shift = cf - cs;
10
+        // With this constructor, we are finding how much to rotate by
11
+        // UP TO CF, not including
12
+    }
13
+
14
+    ROT13() {
15
+        shift = 13;
16
+    }
17
+
18
+    public int getShift() {
19
+        return shift;
20
+    }
21
+
22
+    public void setShift(int shift) {
23
+        this.shift = shift;
24
+    }
25
+
26
+    public String crypt(String text) throws UnsupportedOperationException {
27
+        StringBuilder sb = new StringBuilder();
28
+        // only 26 letters in the alphabet, with the 13 shift, the crpt and encrypt are equal
29
+        for (int i = 0; i < text.length(); i++) {
30
+            char c = text.charAt(i);
31
+            if (c >= 'a' && c <= 'm') {
32
+                c += shift;
33
+                sb.append(c);
34
+            } else if (c >= 'A' && c <= 'M') {
35
+                c += shift;
36
+                sb.append(c);
37
+            } else if (c >= 'n' && c <= 'z') {
38
+                c -= shift;
39
+                sb.append(c);
40
+            } else if (c >= 'N' && c <= 'Z') {
41
+                c -= shift;
42
+                sb.append(c);
43
+            } else {
44
+                sb.append(c);
45
+            }
46
+        }
47
+        // we are rotating each letter, not shifting
48
+        return sb.toString();
49
+    }
50
+
51
+    public String encrypt(String text) {
52
+        return crypt(text);
53
+    }
54
+
55
+    public String decrypt(String text) {
56
+        return crypt(text);
57
+    }
58
+
59
+    public static String rotate(String s, Character c) {
60
+        // shifting the string so that C is the first character
61
+        // Find where c is
62
+        int index = 0;
63
+        for (int i = 0; i < s.length(); i++) {
64
+            if (s.charAt(i) == c) {
65
+                index = i;
66
+                break;
67
+            }
68
+        }
69
+
70
+        int rotation = index % s.length();
71
+
72
+        return s.substring(rotation) + s.substring(0, index);
73
+    }
74
+
75
+}

+ 47
- 0
src/test/java/AtbashCipherTest.java View File

@@ -0,0 +1,47 @@
1
+import org.junit.Assert;
2
+import org.junit.Test;
3
+
4
+import java.io.BufferedReader;;
5
+import java.io.FileReader;
6
+import java.io.IOException;
7
+
8
+
9
+public class AtbashCipherTest {
10
+
11
+    @Test
12
+    public void testCipher() {
13
+        AtbashCipher cipher = new AtbashCipher();
14
+        String test = "ABCDEF";
15
+        String exp = "ZYXWVU";
16
+
17
+        String actual = cipher.cipher(test);
18
+
19
+        Assert.assertEquals(exp, actual);
20
+    }
21
+
22
+    @Test
23
+    public void testFiles() throws IOException {
24
+
25
+        AtbashCipher cipher = new AtbashCipher();
26
+
27
+        String inputFile = "sonnet18.txt";
28
+        String outputTest = "testFileOutput.txt";
29
+        String decryptTest = "decryptedTestFile.txt";
30
+
31
+        cipher.cipherFile(inputFile, outputTest);
32
+        cipher.cipherFile(outputTest, decryptTest);
33
+
34
+        BufferedReader bROriginal = new BufferedReader(new FileReader(inputFile));
35
+        BufferedReader bRDecyphered = new BufferedReader(new FileReader(decryptTest));
36
+
37
+        String originalLine;
38
+        String decrpytLine;
39
+
40
+        while ((originalLine = bROriginal.readLine()) != null) {
41
+            decrpytLine = bRDecyphered.readLine();
42
+            Assert.assertEquals(originalLine, decrpytLine);
43
+        }
44
+
45
+
46
+    }
47
+}

+ 36
- 0
src/test/java/CaeserCipherTest.java View File

@@ -0,0 +1,36 @@
1
+import org.junit.Assert;
2
+import org.junit.Test;
3
+
4
+import static org.junit.Assert.*;
5
+
6
+public class CaeserCipherTest {
7
+
8
+    @Test
9
+    public void testCaesar() {
10
+        CaeserCipher cc = new CaeserCipher(23);
11
+
12
+
13
+        String text = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
14
+        String expected = "XYZABCDEFGHIJKLMNOPQRSTUVW";
15
+
16
+        String actual = cc.encrypt(text);
17
+        System.out.println(actual);
18
+        Assert.assertEquals(expected, actual);
19
+
20
+    }
21
+
22
+    @Test
23
+    public void testDecrypt() {
24
+        CaeserCipher cc = new CaeserCipher(23);
25
+
26
+        String text = "XYZABCDEFGHIJKLMNOPQRSTUVW";
27
+        String exp = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
28
+
29
+        String actual = cc.decrypt(text);
30
+
31
+        Assert.assertEquals(exp, actual);
32
+
33
+
34
+
35
+    }
36
+}

Crypto/src/ROT13Test.java → src/test/java/ROT13Test.java View File

@@ -1,3 +1,4 @@
1
+import org.junit.Assert;
1 2
 import org.junit.Test;
2 3
 
3 4
 import static org.junit.Assert.*;
@@ -60,9 +61,10 @@ public class ROT13Test {
60 61
         String A2 = "To get to the other side!";
61 62
 
62 63
         // When
63
-        String actual = cipher.encrypt(Q1);
64
-        System.out.println(Q1);
65
-        System.out.println(A1);
64
+        String actual = cipher.crypt(Q1);
65
+        System.out.println("Actual: " + actual);
66
+        System.out.println("Q1: " + Q1);
67
+        System.out.println("A1: " + A1);
66 68
         // Then
67 69
         assertTrue(actual.equals(A1));
68 70
 
@@ -88,4 +90,10 @@ public class ROT13Test {
88 90
         assertTrue(actual.equals(Q1));
89 91
     }
90 92
 
93
+    @Test
94
+    public void testRotation() {
95
+        ROT13 cipher = new ROT13('a', 'n');
96
+        System.out.println(cipher.getShift());
97
+    }
98
+
91 99
 }