#6 Nick Satinover - Completed All

Aperto
nsatinover vorrebbe unire 8 commit da nsatinover/SimpleCrypt:master a master

+ 9
- 0
Crypto/src/Main/Caesar.java Vedi File

1
+public class Caesar extends ROT13 {
2
+    public Caesar(Character cs, Character cf) {
3
+        super(cs, cf);
4
+    }
5
+
6
+    public Caesar() {
7
+        
8
+    }
9
+}

+ 110
- 0
Crypto/src/Main/FileCrypter.java Vedi File

1
+import java.io.File;
2
+import java.lang.*;
3
+import java.util.*;
4
+
5
+public class FileCrypter extends ROT13 {
6
+
7
+    private Formatter formatter;
8
+    private Scanner scanner;
9
+    private String fileToString;
10
+    private String fileIn;
11
+    private String fileOut;
12
+    private String encryptedString;
13
+
14
+    FileCrypter(){
15
+        fileIn = "Sonnet18.txt";
16
+        fileOut = "Sonnet18.enc";
17
+    }
18
+
19
+    public String getFileIn() {
20
+        return fileIn;
21
+    }
22
+
23
+    public void setFileIn(String fileIn) {
24
+        this.fileIn = fileIn;
25
+    }
26
+
27
+    public String getFileOut() {
28
+        return fileOut;
29
+    }
30
+
31
+    public void setFileOut(String fileOut) {
32
+        this.fileOut = fileOut;
33
+    }
34
+
35
+    /**
36
+     * Methods to read in from an existing file
37
+     */
38
+    public void connectToFile(){
39
+        try {
40
+            scanner = new Scanner(new File(fileIn));
41
+        }
42
+        catch (Exception e){
43
+            System.out.println("File not found.");
44
+        }
45
+    }
46
+
47
+    public String readFile(){
48
+        StringBuilder builder = new StringBuilder();
49
+        while (scanner.hasNextLine()){
50
+            builder.append(scanner.nextLine() + "\n");
51
+        }
52
+        fileToString = builder.toString();
53
+        return builder.toString();
54
+    }
55
+
56
+    /**
57
+     * Methods to print out to a new file
58
+     */
59
+    public void openFile(){
60
+        try {
61
+            formatter = new Formatter(fileOut);
62
+        }
63
+        catch (Exception e){
64
+            System.out.println("Error writing to file.");
65
+        }
66
+    }
67
+
68
+    public String addRecords(String encryptedStr){
69
+        formatter.format("%s", encryptedStr);
70
+        return encryptedStr;
71
+    }
72
+
73
+    public void closeFile(){
74
+        formatter.close();
75
+        scanner.close();
76
+    }
77
+
78
+    public void encryptFile(){
79
+        ROT13 rot13 = new ROT13('a', 'n');
80
+        connectToFile();
81
+        encryptedString = rot13.encrypt(readFile());
82
+
83
+        /**
84
+         * Create/ overwrite and output to file
85
+         */
86
+        openFile();
87
+        addRecords(encryptedString);
88
+        closeFile();
89
+    }
90
+
91
+
92
+
93
+    public static void main(String[] args) {
94
+        FileCrypter fileCrypter = new FileCrypter();
95
+        ROT13 rot13 = new ROT13('a', 'n');
96
+
97
+        fileCrypter.connectToFile();
98
+        fileCrypter.encryptedString = rot13.encrypt(fileCrypter.readFile());
99
+
100
+        /**
101
+         * Create/ overwrite and output to file
102
+         */
103
+        fileCrypter.openFile();
104
+        fileCrypter.addRecords(fileCrypter.encryptedString);
105
+        fileCrypter.closeFile();
106
+
107
+
108
+    }
109
+
110
+}

+ 95
- 0
Crypto/src/Main/ROT13.java Vedi File

1
+import java.util.Arrays;
2
+import java.util.List;
3
+
4
+public class ROT13  {
5
+    private int rotate;
6
+    private int encryptShift = 26;
7
+    private boolean encrypt;
8
+
9
+    public boolean isEncrypt() {
10
+        return encrypt;
11
+    }
12
+
13
+    public void setEncrypt(boolean encrypt) {
14
+        this.encrypt = encrypt;
15
+    }
16
+
17
+    ROT13(Character cs, Character cf) {
18
+        rotate = (int)(cs) - (int)(cf);
19
+    }
20
+
21
+    ROT13() {
22
+    }
23
+
24
+    public int getRotate() {
25
+        return rotate;
26
+    }
27
+
28
+    public String crypt(String text){
29
+        return encrypt(text);
30
+    }
31
+
32
+    public String crypt(String text, int rotate) throws UnsupportedOperationException {
33
+        List<String> textList = Arrays.asList(text.split(" "));
34
+        StringBuilder builder = new StringBuilder();
35
+
36
+        for (String s: textList) {
37
+            char[] charArr = s.toCharArray();
38
+            for (char c: charArr) {
39
+                String strToAppend = isEncrypt() ? shiftCharEncrypt(c, rotate) : shiftCharDecrypt(c, rotate);
40
+                builder.append(strToAppend);
41
+            }
42
+            builder.append(" ");
43
+        }
44
+        return builder.toString().trim();
45
+    }
46
+
47
+    public String encrypt(String text) {
48
+        setEncrypt(true);
49
+        return crypt(text, getRotate());
50
+    }
51
+
52
+    public String decrypt(String text) {
53
+        setEncrypt(false);
54
+        return crypt(text, getRotate() * -1);
55
+    }
56
+
57
+    public String shiftCharEncrypt(char c, int rotate){
58
+        if (c > 64 && c != 8217){
59
+            return  ( (c > 96 && (c + rotate) > 96) || (c <= 96 && (c + rotate) > 64) ) ?
60
+                    ( Character.toString( (char)(c + rotate) ) ) :
61
+                    ( Character.toString( (char)(c + rotate + encryptShift) ) );
62
+        } else {
63
+            return Character.toString(c);
64
+        }
65
+    }
66
+
67
+    public String shiftCharDecrypt(char c, int rotate){
68
+        if (c > 64){
69
+            return  ( (c + rotate > 122) || (c <= 90  && c + rotate > 90 ) ) ?
70
+                    ( Character.toString( (char)(c + rotate + encryptShift * -1) ) ) :
71
+                    ( Character.toString( (char)(c + rotate) ) );
72
+        } else {
73
+            return Character.toString(c);
74
+        }
75
+    }
76
+
77
+    public static String rotate(String s, Character c) {
78
+        int rotateBy = (int)(c) - (int)(s.charAt(0));
79
+        char[] charArr = s.toCharArray();
80
+        char[] returnChar = new char[charArr.length];
81
+
82
+        int x = 0;
83
+        for (int i = rotateBy; i < returnChar.length; i++) {
84
+            returnChar[x] = (char)( (int)(charArr[i]));
85
+            x++;
86
+        }
87
+
88
+        for (int i = 0; i < rotateBy; i++) {
89
+            returnChar[x] = (char)( (int)(charArr[i]));
90
+            x++;
91
+        }
92
+        return String.copyValueOf(returnChar);
93
+    }
94
+
95
+}

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

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

+ 91
- 0
Crypto/src/Test/CaesarTest.java Vedi File

1
+import org.junit.Test;
2
+import static org.junit.Assert.*;
3
+import static org.junit.Assert.assertTrue;
4
+
5
+public class CaesarTest {
6
+
7
+    @Test
8
+    public void rotateStringTest0() {
9
+        // Given
10
+        String s1 = "ABCDEF";
11
+        String s2 = "BCDEFA";
12
+
13
+        // When
14
+        Caesar cipher = new Caesar();
15
+        String actual = cipher.rotate(s1, 'B');
16
+        System.out.println(actual);
17
+        // Then
18
+        assertTrue(actual.equals(s2));
19
+    }
20
+
21
+    @Test
22
+    public void rotateStringTest1() {
23
+        // Given
24
+        String s1 = "ABCDEF";
25
+        String s2 = "DEFABC";
26
+
27
+        // When
28
+        Caesar cipher = new Caesar();
29
+        String actual = cipher.rotate(s1, 'D');
30
+
31
+        // Then
32
+        assertTrue(actual.equals(s2));
33
+    }
34
+
35
+    @Test
36
+    public void rotateStringTest2() {
37
+        // Given
38
+        String s1 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
39
+        String s2 = "NOPQRSTUVWXYZABCDEFGHIJKLM";
40
+
41
+        // When
42
+        Caesar cipher = new Caesar();
43
+        String actual = cipher.rotate(s1, 'N');
44
+        System.out.println(s1);
45
+        System.out.println(actual);
46
+        // Then
47
+        assertTrue(actual.equals(s2));
48
+    }
49
+
50
+    @Test
51
+    public void cryptTest1() {
52
+        // Given
53
+        Caesar cipher = new Caesar('a', 'b');
54
+
55
+        String Q1 = "Caesar Salad!";  //"Why did the chicken cross the road?";
56
+        String A1 = "Bzdrzq Rzkzc!"; //"Jul qvq gur puvpxra pebff gur ebnq?";
57
+
58
+        String Q2 = "With Chicken"; //"Gb trg gb gur bgure fvqr!";
59
+        String A2 = "Xjui Dijdlfo"; //"To get to the other side!";
60
+
61
+        // When
62
+        String actual = cipher.encrypt(Q1);
63
+        System.out.println(Q1);
64
+        System.out.println(A1);
65
+        // Then
66
+        assertTrue(actual.equals(A1));
67
+
68
+        // When
69
+        String actual2 = cipher.decrypt(Q2);
70
+        System.out.println(Q2);
71
+        System.out.println(A2);
72
+        // Then
73
+        assertTrue(actual2.equals(A2));
74
+    }
75
+    @Test
76
+    public void cryptTest2() {
77
+        // Given
78
+        Caesar cipher = new Caesar('b', 'o');
79
+
80
+        String Q1 = "Eat more Chicken?";
81
+        System.out.println(Q1);
82
+
83
+        // When
84
+        String actual = cipher.crypt(cipher.crypt(Q1));
85
+        System.out.println(actual);
86
+        // Then
87
+        assertTrue(actual.equals(Q1));
88
+    }
89
+
90
+}
91
+

Crypto/src/ROT13Test.java → Crypto/src/Test/ROT13Test.java Vedi File

14
         // When
14
         // When
15
         ROT13 cipher = new ROT13();
15
         ROT13 cipher = new ROT13();
16
         String actual = cipher.rotate(s1, 'A');
16
         String actual = cipher.rotate(s1, 'A');
17
-
17
+        System.out.println(actual);
18
         // Then
18
         // Then
19
         assertTrue(actual.equals(s2));
19
         assertTrue(actual.equals(s2));
20
     }
20
     }

+ 27
- 0
Crypto/src/Test/fileCrypterTest.java Vedi File

1
+import org.junit.Test;
2
+
3
+import static org.junit.Assert.assertTrue;
4
+
5
+public class fileCrypterTest {
6
+
7
+    @Test
8
+    public void cryptTest1() {
9
+        // Given
10
+        FileCrypter fileCrypter = new FileCrypter();
11
+        fileCrypter.setFileIn("Sonnet18.txt");
12
+
13
+        fileCrypter.connectToFile();
14
+        String expected = fileCrypter.readFile();
15
+
16
+        // When
17
+        fileCrypter.encryptFile();
18
+        fileCrypter.setFileIn("Sonnet18.enc");
19
+        fileCrypter.encryptFile();
20
+        fileCrypter.connectToFile();
21
+        String actual = fileCrypter.readFile();
22
+
23
+        // Then
24
+        assertTrue(actual.equals(expected));
25
+
26
+    }
27
+}

+ 16
- 0
SimpleCrypt.iml Vedi File

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" isTestSource="false" />
8
+      <sourceFolder url="file://$MODULE_DIR$/Crypto/src/Test" 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" scope="TEST" name="Maven: junit:junit:4.12" level="project" />
14
+    <orderEntry type="library" scope="TEST" name="Maven: org.hamcrest:hamcrest-core:1.3" level="project" />
15
+  </component>
16
+</module>

+ 8
- 0
pom.xml Vedi File

7
     <groupId>com.zipcodewilmington</groupId>
7
     <groupId>com.zipcodewilmington</groupId>
8
     <artifactId>SimpleCrypt</artifactId>
8
     <artifactId>SimpleCrypt</artifactId>
9
     <version>1.0-SNAPSHOT</version>
9
     <version>1.0-SNAPSHOT</version>
10
+    <dependencies>
11
+        <dependency>
12
+            <groupId>junit</groupId>
13
+            <artifactId>junit</artifactId>
14
+            <version>RELEASE</version>
15
+            <scope>test</scope>
16
+        </dependency>
17
+    </dependencies>
10
 
18
 
11
 
19
 
12
 </project>
20
 </project>

+ 1
- 1
sonnet18.txt Vedi File

11
 Nor shall death brag thou wander’st in his shade,
11
 Nor shall death brag thou wander’st in his shade,
12
 When in eternal lines to time thou grow’st:
12
 When in eternal lines to time thou grow’st:
13
    So long as men can breathe or eyes can see,
13
    So long as men can breathe or eyes can see,
14
-   So long lives this, and this gives life to thee.
14
+   So long lives this, and this gives life to thee.