Lauren Green 7 лет назад
Родитель
Сommit
d02ee84603

+ 2
- 1
Crypto/src/main/java/Caesar.java Просмотреть файл

7
 
7
 
8
     public Caesar(int shift) {
8
     public Caesar(int shift) {
9
         this.shift = shift;
9
         this.shift = shift;
10
-        this.shifted = rotateByNumber(alpha, shift);
10
+        this.shiftedUpper = rotateByNumber(alphaUpper, shift);
11
+        this.shiftedLower = rotateByNumber(alphaLower, shift);
11
 
12
 
12
     }
13
     }
13
 
14
 

+ 79
- 0
Crypto/src/main/java/LaurensCipher.java Просмотреть файл

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

+ 72
- 20
Crypto/src/main/java/ROT13.java Просмотреть файл

1
-import java.util.Arrays;
2
-import java.util.List;
3
-import java.util.Map;
1
+import java.io.*;
2
+import java.util.*;
4
 import java.util.stream.Stream;
3
 import java.util.stream.Stream;
5
 
4
 
6
 import static java.lang.Character.isLowerCase;
5
 import static java.lang.Character.isLowerCase;
12
     Character original;
11
     Character original;
13
     Character adjusted;
12
     Character adjusted;
14
     int shift = 13;
13
     int shift = 13;
15
-    String alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
16
-    String shifted = "NOPQRSTUVWXYZABCDEFGHIJKLM";
14
+    String alphaUpper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
15
+    String shiftedUpper = "NOPQRSTUVWXYZABCDEFGHIJKLM";
16
+    String alphaLower = "abcdefghijklmnopqrstuvwxyz";
17
+    String shiftedLower = "nopqrstuvwxyzabcdefghijklm";
17
 
18
 
18
     ROT13(Character cs, Character cf) {
19
     ROT13(Character cs, Character cf) {
19
         this.original = cs;
20
         this.original = cs;
20
         this.adjusted = cf;
21
         this.adjusted = cf;
21
         this.shift = cf - cs;
22
         this.shift = cf - cs;
22
-        this.shifted = rotate(alpha, cf);
23
+        this.shiftedUpper = rotate(alphaUpper, Character.toUpperCase(cf));
24
+        this.shiftedLower = rotate(alphaLower, Character.toLowerCase(cf));
23
 
25
 
24
     }
26
     }
25
 
27
 
27
 
29
 
28
     }
30
     }
29
 
31
 
30
-    public String getShifted() {
31
-        return shifted;
32
+    public String getShiftedUpper() {
33
+        return shiftedUpper;
32
     }
34
     }
33
 
35
 
36
+    public String getShiftedLower() {
37
+        return shiftedLower;
38
+    }
34
 
39
 
35
     public String crypt(String text) throws UnsupportedOperationException {
40
     public String crypt(String text) throws UnsupportedOperationException {
36
 
41
 
39
 
44
 
40
     public String encrypt(String text) {
45
     public String encrypt(String text) {
41
         String result = "";
46
         String result = "";
42
-        text = text.toUpperCase();
43
         for (Character c: text.toCharArray()) {
47
         for (Character c: text.toCharArray()) {
44
             if(Character.isLetter(c)) {
48
             if(Character.isLetter(c)) {
45
-                for (int i = 0; i < alpha.length(); i++) {
46
-                    if(c.equals(alpha.charAt(i))) {
47
-                        result += getShifted().charAt(i);
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);
48
                     }
58
                     }
49
                 }
59
                 }
60
+
50
             } else {
61
             } else {
51
                 result += c;
62
                 result += c;
52
             }
63
             }
53
 
64
 
54
         }
65
         }
55
 
66
 
56
-        return result.charAt(0) + result.substring(1).toLowerCase();
57
-
67
+        return result;
58
     }
68
     }
59
 
69
 
60
     public String decrypt(String text) {
70
     public String decrypt(String text) {
61
         String result = "";
71
         String result = "";
62
-        text = text.toUpperCase();
72
+
63
         for (Character c: text.toCharArray()) {
73
         for (Character c: text.toCharArray()) {
64
             if(Character.isLetter(c)) {
74
             if(Character.isLetter(c)) {
65
-                for (int i = 0; i < alpha.length(); i++) {
66
-                    if(c.equals(alpha.charAt(i))) {
67
-                        result += getShifted().charAt(i);
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);
68
                     }
84
                     }
69
                 }
85
                 }
86
+
70
             } else {
87
             } else {
71
                 result += c;
88
                 result += c;
72
             }
89
             }
73
 
90
 
74
         }
91
         }
75
 
92
 
76
-        return result.charAt(0) + result.substring(1).toLowerCase();
93
+        return result;
77
     }
94
     }
78
 
95
 
79
     public static String rotate(String s, Character c) {
96
     public static String rotate(String s, Character c) {
80
 
97
 
81
-        c = Character.toUpperCase(c);
82
         int index = 0;
98
         int index = 0;
83
         for (int i = 0; i < s.length(); i++) {
99
         for (int i = 0; i < s.length(); i++) {
84
             if(s.charAt(i) == c){
100
             if(s.charAt(i) == c){
88
         return s.substring(index) + s.substring(0, index);
104
         return s.substring(index) + s.substring(0, index);
89
     }
105
     }
90
 
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
+
91
 }
143
 }

+ 83
- 0
Crypto/src/test/java/LaurensCipherTest.java Просмотреть файл

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

+ 29
- 1
Crypto/src/test/java/ROT13Test.java Просмотреть файл

1
 import org.junit.Assert;
1
 import org.junit.Assert;
2
 import org.junit.Test;
2
 import org.junit.Test;
3
 
3
 
4
+import java.io.BufferedReader;
5
+import java.io.FileNotFoundException;
6
+import java.io.FileReader;
7
+import java.io.IOException;
8
+
4
 import static org.junit.Assert.*;
9
 import static org.junit.Assert.*;
5
 
10
 
6
 public class ROT13Test {
11
 public class ROT13Test {
52
     public void cryptTest1() {
57
     public void cryptTest1() {
53
         // Given
58
         // Given
54
         ROT13 cipher = new ROT13('a', 'n');
59
         ROT13 cipher = new ROT13('a', 'n');
55
-        System.out.println(cipher.getShifted());
56
 
60
 
57
         String Q1 = "Why did the chicken cross the road?";
61
         String Q1 = "Why did the chicken cross the road?";
58
         String A1 = "Jul qvq gur puvpxra pebff gur ebnq?";
62
         String A1 = "Jul qvq gur puvpxra pebff gur ebnq?";
89
         assertTrue(actual.equals(Q1));
93
         assertTrue(actual.equals(Q1));
90
     }
94
     }
91
 
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
+
92
 }
120
 }

+ 14
- 0
sonnet18Encrypt.txt Просмотреть файл

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.