#4 laurenstewartgreen

Отворени
laurengreen заяви обединяване на 3 ревизии от laurengreen/SimpleCrypt:master във master

+ 0
- 32
Crypto/src/ROT13.java Целия файл

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

+ 20
- 0
Crypto/src/main/java/Caesar.java Целия файл

@@ -0,0 +1,20 @@
1
+public class Caesar extends ROT13 {
2
+
3
+
4
+    public Caesar() {
5
+
6
+    }
7
+
8
+    public Caesar(int shift) {
9
+        this.shift = shift;
10
+        this.shiftedUpper = rotateByNumber(alphaUpper, shift);
11
+        this.shiftedLower = rotateByNumber(alphaLower, shift);
12
+
13
+    }
14
+
15
+    public static String rotateByNumber (String input, int number) {
16
+
17
+        return input.substring(number) + input.substring(0, number);
18
+    }
19
+
20
+}

+ 79
- 0
Crypto/src/main/java/LaurensCipher.java Целия файл

@@ -0,0 +1,79 @@
1
+import java.util.ArrayList;
2
+
3
+public class LaurensCipher {
4
+
5
+    String alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
6
+    Integer[] code = new Integer[26];
7
+    int magicNumber = 2;
8
+
9
+    public LaurensCipher(int numb) {
10
+        this.magicNumber = numb;
11
+        this.code = createCipher(this.magicNumber);
12
+    }
13
+
14
+    public Integer[] getCode() {
15
+        return code;
16
+    }
17
+
18
+    public int getMagicNumber() {
19
+        return magicNumber;
20
+    }
21
+
22
+    public Integer[] createCipher(int magicNumber) {
23
+
24
+        Integer[] cipher = new Integer[26];
25
+
26
+        for (int i = 0; i < 26; i++) {
27
+          cipher[i] = ((alpha.charAt(i) + magicNumber) * magicNumber);
28
+
29
+        }
30
+
31
+        return cipher;
32
+    }
33
+
34
+    public Character getLetter(int codeNumber) {
35
+
36
+        int letterNumber = codeNumber / magicNumber - magicNumber;
37
+
38
+        int index = letterNumber - 65;
39
+
40
+        return alpha.charAt(index);
41
+
42
+    }
43
+
44
+    public String encrypt(String text) {
45
+        String result = "";
46
+        text = text.toUpperCase().replace(" ", "");
47
+        for (Character c: text.toCharArray()) {
48
+            if(Character.isLetter(c)) {
49
+                for (int i = 0; i < alpha.length(); i++) {
50
+                    if(c.equals(alpha.charAt(i))) {
51
+                        result += code[i] + " ";
52
+                    }
53
+                }
54
+            } else {
55
+                result += c + " ";
56
+            }
57
+
58
+        }
59
+
60
+        return (result.charAt(0) + result.substring(1).toLowerCase()).trim();
61
+
62
+    }
63
+
64
+    public String decrypt(String text) {
65
+        String result = "";
66
+        String[] str = text.split(" ");
67
+        ArrayList<Integer> numbers = new ArrayList<Integer>();
68
+        for(String s: str) {
69
+
70
+                numbers.add(Integer.parseInt(s));
71
+
72
+        }
73
+        for(Integer i: numbers) {
74
+            result += getLetter(i);
75
+        }
76
+        return result;
77
+    }
78
+
79
+}

+ 143
- 0
Crypto/src/main/java/ROT13.java Целия файл

@@ -0,0 +1,143 @@
1
+import java.io.*;
2
+import java.util.*;
3
+import java.util.stream.Stream;
4
+
5
+import static java.lang.Character.isLowerCase;
6
+import static java.lang.Character.isUpperCase;
7
+import static java.lang.Character.toLowerCase;
8
+
9
+public class ROT13  {
10
+
11
+    Character original;
12
+    Character adjusted;
13
+    int shift = 13;
14
+    String alphaUpper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
15
+    String shiftedUpper = "NOPQRSTUVWXYZABCDEFGHIJKLM";
16
+    String alphaLower = "abcdefghijklmnopqrstuvwxyz";
17
+    String shiftedLower = "nopqrstuvwxyzabcdefghijklm";
18
+
19
+    ROT13(Character cs, Character cf) {
20
+        this.original = cs;
21
+        this.adjusted = cf;
22
+        this.shift = cf - cs;
23
+        this.shiftedUpper = rotate(alphaUpper, Character.toUpperCase(cf));
24
+        this.shiftedLower = rotate(alphaLower, Character.toLowerCase(cf));
25
+
26
+    }
27
+
28
+    ROT13() {
29
+
30
+    }
31
+
32
+    public String getShiftedUpper() {
33
+        return shiftedUpper;
34
+    }
35
+
36
+    public String getShiftedLower() {
37
+        return shiftedLower;
38
+    }
39
+
40
+    public String crypt(String text) throws UnsupportedOperationException {
41
+
42
+        return text;
43
+    }
44
+
45
+    public String encrypt(String text) {
46
+        String result = "";
47
+        for (Character c: text.toCharArray()) {
48
+            if(Character.isLetter(c)) {
49
+                String alphabet = alphaLower;
50
+                String shifted = this.shiftedLower;
51
+                if(Character.isUpperCase(c)) {
52
+                    alphabet = alphaUpper;
53
+                    shifted = this.shiftedUpper;
54
+                }
55
+                for (int i = 0; i < alphabet.length(); i++) {
56
+                    if(c.equals(alphabet.charAt(i))) {
57
+                        result += shifted.charAt(i);
58
+                    }
59
+                }
60
+
61
+            } else {
62
+                result += c;
63
+            }
64
+
65
+        }
66
+
67
+        return result;
68
+    }
69
+
70
+    public String decrypt(String text) {
71
+        String result = "";
72
+
73
+        for (Character c: text.toCharArray()) {
74
+            if(Character.isLetter(c)) {
75
+                String alphabet = alphaLower;
76
+                String shifted = this.shiftedLower;
77
+                if(Character.isUpperCase(c)) {
78
+                    alphabet = alphaUpper;
79
+                    shifted = this.shiftedUpper;
80
+                }
81
+                for (int i = 0; i < alphabet.length(); i++) {
82
+                    if(c.equals(alphabet.charAt(i))) {
83
+                        result += shifted.charAt(i);
84
+                    }
85
+                }
86
+
87
+            } else {
88
+                result += c;
89
+            }
90
+
91
+        }
92
+
93
+        return result;
94
+    }
95
+
96
+    public static String rotate(String s, Character c) {
97
+
98
+        int index = 0;
99
+        for (int i = 0; i < s.length(); i++) {
100
+            if(s.charAt(i) == c){
101
+                index = i;
102
+            }
103
+        }
104
+        return s.substring(index) + s.substring(0, index);
105
+    }
106
+
107
+    public void fileEncrypter(String fileName, String newFileName) throws FileNotFoundException {
108
+
109
+        String fileString = fileToString(fileName);
110
+        String newFileString = encrypt(fileString);
111
+        System.out.println(newFileString);
112
+
113
+        try {
114
+            File file = new File(newFileName);
115
+            FileWriter fileWriter = new FileWriter(file);
116
+            fileWriter.write(newFileString);
117
+            fileWriter.flush();
118
+            fileWriter.close();
119
+        } catch (IOException e) {
120
+            System.out.println("Error writing file");
121
+        }
122
+
123
+    }
124
+
125
+    public String fileToString(String fileName) throws FileNotFoundException {
126
+        File file = new File(fileName);
127
+        BufferedReader reader = new BufferedReader(new FileReader(file));
128
+        StringBuilder stringBuilder = new StringBuilder();
129
+        String line = null;
130
+
131
+        try {
132
+            while((line = reader.readLine()) != null) {
133
+                stringBuilder.append(line + "\n");
134
+            }
135
+
136
+        } catch (Exception e) {
137
+            System.out.println("Error Reading File");
138
+        }
139
+
140
+        return stringBuilder.toString();
141
+    }
142
+
143
+}

+ 102
- 0
Crypto/src/test/java/CaesarTest.java Целия файл

@@ -0,0 +1,102 @@
1
+import org.junit.Assert;
2
+import org.junit.Test;
3
+
4
+import static org.junit.Assert.*;
5
+
6
+public class CaesarTest {
7
+
8
+    @Test
9
+    public void rotateStringTest0() {
10
+        // Given
11
+        String s1 = "ABCDEF";
12
+        String s2 = "ABCDEF";
13
+
14
+        // When
15
+        Caesar cipher = new Caesar();
16
+        String actual = cipher.rotateByNumber(s1, 0);
17
+
18
+        // Then
19
+        assertTrue(actual.equals(s2));
20
+    }
21
+
22
+    @Test
23
+    public void rotateStringTest1() {
24
+        // Given
25
+        String s1 = "ABCDEF";
26
+        String s2 = "DEFABC";
27
+
28
+        // When
29
+        Caesar cipher = new Caesar();
30
+        String actual = cipher.rotateByNumber(s1, 3);
31
+
32
+        // Then
33
+        assertTrue(actual.equals(s2));
34
+    }
35
+
36
+    @Test
37
+    public void rotateStringTest2() {
38
+        // Given
39
+        String s1 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
40
+        String s2 = "NOPQRSTUVWXYZABCDEFGHIJKLM";
41
+
42
+        // When
43
+        Caesar cipher = new Caesar();
44
+        String actual = cipher.rotateByNumber(s1, 13);
45
+        System.out.println(s1);
46
+        System.out.println(actual);
47
+        // Then
48
+        assertTrue(actual.equals(s2));
49
+    }
50
+
51
+    @Test
52
+    public void cryptTest1() {
53
+        // Given
54
+        Caesar cipher = new Caesar(13);
55
+
56
+        String Q1 = "Why did the chicken cross the road?";
57
+        String A1 = "Jul qvq gur puvpxra pebff gur ebnq?";
58
+
59
+        String Q2 = "Gb trg gb gur bgure fvqr!";
60
+        String A2 = "To get to the other side!";
61
+
62
+        // When
63
+        String actual = cipher.encrypt(Q1);
64
+        System.out.println(Q1);
65
+        System.out.println(A1);
66
+        // Then
67
+        Assert.assertEquals(A1, actual);
68
+
69
+        // When
70
+        String actual2 = cipher.decrypt(Q2);
71
+        System.out.println(Q2);
72
+        System.out.println(A2);
73
+        // Then
74
+        Assert.assertEquals(A2, actual2);
75
+    }
76
+
77
+    @Test
78
+    public void cryptTest2() {
79
+        // Given
80
+        Caesar cipher = new Caesar(4);
81
+
82
+        String Q1 = "This is a test of Caesar.";
83
+        String A1 = "Xlmw mw e xiwx sj geiwev.";
84
+
85
+        String Q2 = "Ynulpo wna bqj!";
86
+        String A2 = "Crypts are fun!";
87
+
88
+        // When
89
+        String actual = cipher.encrypt(Q1);
90
+        System.out.println(Q1);
91
+        System.out.println(A1);
92
+        // Then
93
+        Assert.assertEquals(A1, actual);
94
+
95
+        // When
96
+        String actual2 = cipher.decrypt(Q2);
97
+        System.out.println(Q2);
98
+        System.out.println(A2);
99
+        // Then
100
+        Assert.assertEquals(A2, actual2);
101
+    }
102
+}

+ 83
- 0
Crypto/src/test/java/LaurensCipherTest.java Целия файл

@@ -0,0 +1,83 @@
1
+import org.junit.Assert;
2
+import org.junit.Test;
3
+
4
+import static org.junit.Assert.assertTrue;
5
+
6
+public class LaurensCipherTest {
7
+
8
+    @Test
9
+    public void createCodeTest() {
10
+        // Given
11
+        LaurensCipher cipher = new LaurensCipher(4);
12
+        Integer[] expected = new Integer[26];
13
+        for (int i = 0; i < 26; i++) {
14
+            expected[i] = (('A' + i - 0) + 4) * 4;
15
+        }
16
+
17
+        // When
18
+        Integer[] actual = cipher.getCode();
19
+
20
+        // Then
21
+        Assert.assertArrayEquals(expected, actual);
22
+    }
23
+
24
+    @Test
25
+    public void getLetterTest() {
26
+        // Given
27
+        LaurensCipher cipher = new LaurensCipher(4);
28
+        Character expected = 'D';
29
+
30
+        // When
31
+        Character actual = cipher.getLetter(288);
32
+
33
+        // Then
34
+        Assert.assertEquals(expected, actual);
35
+    }
36
+
37
+    @Test
38
+    public void encryptTest() {
39
+        // Given
40
+        LaurensCipher cipher = new LaurensCipher(6);
41
+        System.out.println(cipher.getCode());
42
+        String text = "This is my secret code";
43
+        String expected = "540 468 474 534 474 534 498 570 534 450 438 528 450 540 438 510 444 450";
44
+
45
+        // When
46
+        String actual = cipher.encrypt(text);
47
+
48
+        // Then
49
+        Assert.assertEquals(expected, actual);
50
+    }
51
+
52
+    @Test
53
+    public void encryptTest2() {
54
+        // Given
55
+        LaurensCipher cipher = new LaurensCipher(10);
56
+        String text = "The top secret code";
57
+        String expected = "940 820 790 940 890 900 930 790 770 920 790 940 770 890 780 790";
58
+
59
+        // When
60
+        String actual = cipher.encrypt(text);
61
+
62
+        // Then
63
+        Assert.assertEquals(expected, actual);
64
+    }
65
+
66
+
67
+    @Test
68
+    public void decryptTest2() {
69
+        // Given
70
+        LaurensCipher cipher = new LaurensCipher(10);
71
+        String text = "Mischief Managed";
72
+        String code = cipher.encrypt(text);
73
+        String expected = "MISCHIEFMANAGED";
74
+
75
+        // When
76
+        String actual = cipher.decrypt(code);
77
+
78
+        // Then
79
+        Assert.assertEquals(expected, actual);
80
+
81
+    }
82
+
83
+}

Crypto/src/ROT13Test.java → Crypto/src/test/java/ROT13Test.java Целия файл

@@ -1,10 +1,15 @@
1
+import org.junit.Assert;
1 2
 import org.junit.Test;
2 3
 
4
+import java.io.BufferedReader;
5
+import java.io.FileNotFoundException;
6
+import java.io.FileReader;
7
+import java.io.IOException;
8
+
3 9
 import static org.junit.Assert.*;
4 10
 
5 11
 public class ROT13Test {
6 12
 
7
-
8 13
     @Test
9 14
     public void rotateStringTest0() {
10 15
         // Given
@@ -64,14 +69,14 @@ public class ROT13Test {
64 69
         System.out.println(Q1);
65 70
         System.out.println(A1);
66 71
         // Then
67
-        assertTrue(actual.equals(A1));
72
+        Assert.assertEquals(A1, actual);
68 73
 
69 74
         // When
70 75
         String actual2 = cipher.decrypt(Q2);
71 76
         System.out.println(Q2);
72 77
         System.out.println(A2);
73 78
         // Then
74
-        assertTrue(actual2.equals(A2));
79
+        Assert.assertEquals(A2, actual2);
75 80
     }
76 81
     @Test
77 82
     public void cryptTest2() {
@@ -88,4 +93,28 @@ public class ROT13Test {
88 93
         assertTrue(actual.equals(Q1));
89 94
     }
90 95
 
96
+    @Test
97
+    public void fileReaderTest() throws IOException {
98
+        // Given
99
+        ROT13 cipher = new ROT13('a', 'n');
100
+        String filePath = "sonnet18.txt";
101
+        String newFilePath = "sonnet18Encrypt.txt";
102
+
103
+        // When
104
+        cipher.fileEncrypter(filePath, newFilePath);
105
+        BufferedReader bROriginal = new BufferedReader(new FileReader(filePath));
106
+        BufferedReader bRnewFile = new BufferedReader(new FileReader(newFilePath));
107
+
108
+        String lineOriginal;
109
+        String lineNew;
110
+
111
+        // Then
112
+        while ((lineOriginal = bROriginal.readLine()) != null) {
113
+            lineNew = cipher.decrypt(bRnewFile.readLine());
114
+            Assert.assertEquals(lineOriginal, lineNew);
115
+        }
116
+
117
+    }
118
+
119
+
91 120
 }

+ 16
- 0
SimpleCrypt.iml Целия файл

@@ -0,0 +1,16 @@
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/main/java" isTestSource="false" />
8
+      <sourceFolder url="file://$MODULE_DIR$/Crypto/src/test/java" isTestSource="true" />
9
+      <excludeFolder url="file://$MODULE_DIR$/target" />
10
+    </content>
11
+    <orderEntry type="inheritedJdk" />
12
+    <orderEntry type="sourceFolder" forTests="false" />
13
+    <orderEntry type="library" name="Maven: junit:junit:4.12" level="project" />
14
+    <orderEntry type="library" name="Maven: org.hamcrest:hamcrest-core:1.3" level="project" />
15
+  </component>
16
+</module>

+ 8
- 0
pom.xml Целия файл

@@ -8,5 +8,13 @@
8 8
     <artifactId>SimpleCrypt</artifactId>
9 9
     <version>1.0-SNAPSHOT</version>
10 10
 
11
+    <dependencies>
11 12
 
13
+
14
+        <dependency>
15
+            <groupId>junit</groupId>
16
+            <artifactId>junit</artifactId>
17
+            <version>RELEASE</version>
18
+        </dependency>
19
+    </dependencies>
12 20
 </project>

+ 14
- 0
sonnet18Encrypt.txt Целия файл

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