Przeglądaj źródła

crypto part 1 and last part 2

Jamez-s 6 lat temu
rodzic
commit
f9c6ba6443

BIN
.DS_Store Wyświetl plik


+ 58
- 4
Crypto/src/ROT13.java Wyświetl plik

@@ -1,32 +1,86 @@
1
+import com.sun.xml.internal.ws.policy.privateutil.PolicyUtils.IO;
2
+import java.io.File;
3
+import java.io.FileWriter;
4
+import java.io.IOException;
5
+import java.nio.file.Files;
6
+import java.nio.file.Paths;
7
+
1 8
 import static java.lang.Character.isLowerCase;
2 9
 import static java.lang.Character.isUpperCase;
3 10
 import static java.lang.Character.toLowerCase;
4 11
 
5
-public class ROT13  {
12
+public class ROT13 {
13
+
14
+	private int shiftAmount;
15
+
16
+
17
+    protected String startUpper;
18
+    protected String startLower;
19
+    protected String registerUpper;
20
+    protected String registerLower;
21
+    protected String symmetric;
6 22
 
7 23
     ROT13(Character cs, Character cf) {
24
+    	shiftAmount = cf - cs;
8 25
     }
9 26
 
10 27
     ROT13() {
28
+    	shiftAmount = 13;
11 29
     }
12 30
 
13 31
 
14 32
     public String crypt(String text) throws UnsupportedOperationException {
15 33
 
16
-        return "";
34
+
35
+    	char[] arr = text.toCharArray();
36
+        for (int i = 0; i < arr.length; i++) {
37
+        	arr[i] = (isLowerCase(arr[i])) ? // if lower case character at index i in array, do first statement, else do second
38
+			        (char)((arr[i] + shiftAmount - 97) % 26 + 97) : (char)((arr[i] + shiftAmount - 65) % 26 + 65); // casting to char
39
+        }
40
+        return String.valueOf(arr); //returns charArray as String
17 41
     }
18 42
 
43
+
19 44
     public String encrypt(String text) {
20
-        return text;
45
+
46
+        return crypt(text);
21 47
     }
22 48
 
23 49
     public String decrypt(String text) {
24
-        return text;
50
+        return crypt(text);
51
+    }
52
+
53
+    public String fileToString(File fp) throws IOException {
54
+    	// get the text of a given file from its filepath
55
+
56
+    	return new String(Files.readAllBytes(Paths.get(String.valueOf(fp))));
57
+	    //get value of String then convert to bytestream
25 58
     }
26 59
 
60
+	public File StringToFile(String originalFile, String fileName) throws IOException {
61
+		// Create a file using a fileWriter. encrypt text then write to fileName
62
+		// String originalFile to encrypted text then write to fileName
63
+
64
+		File file = new File(originalFile);
65
+
66
+		// Creates the file
67
+		file.createNewFile();
68
+
69
+		// Create file writer object
70
+		FileWriter writer = new FileWriter(file);
71
+
72
+		// write contents to file
73
+		writer.write(encrypt(fileToString(new File(originalFile))));
74
+		writer.flush();
75
+		writer.close();
76
+
77
+		return file;
78
+	}
79
+
27 80
     public static String rotate(String s, Character c) {
28 81
 
29 82
         return "";
30 83
     }
31 84
 
32 85
 }
86
+

+ 95
- 84
Crypto/src/ROT13Test.java Wyświetl plik

@@ -5,87 +5,98 @@ import static org.junit.Assert.*;
5 5
 public class ROT13Test {
6 6
 
7 7
 
8
-    @Test
9
-    public void rotateStringTest0() {
10
-        // Given
11
-        String s1 = "ABCDEF";
12
-        String s2 = "ABCDEF";
13
-
14
-        // When
15
-        ROT13 cipher = new ROT13();
16
-        String actual = cipher.rotate(s1, 'A');
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
-        ROT13 cipher = new ROT13();
30
-        String actual = cipher.rotate(s1, 'D');
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
-        ROT13 cipher = new ROT13();
44
-        String actual = cipher.rotate(s1, 'N');
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
-        ROT13 cipher = new ROT13('a', 'n');
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
-        assertTrue(actual.equals(A1));
68
-
69
-        // When
70
-        String actual2 = cipher.decrypt(Q2);
71
-        System.out.println(Q2);
72
-        System.out.println(A2);
73
-        // Then
74
-        assertTrue(actual2.equals(A2));
75
-    }
76
-    @Test
77
-    public void cryptTest2() {
78
-        // Given
79
-        ROT13 cipher = new ROT13('a', 'n');
80
-
81
-        String Q1 = "Why did the chicken cross the road?";
82
-        System.out.println(Q1);
83
-
84
-        // When
85
-        String actual = cipher.crypt(cipher.crypt(Q1));
86
-        System.out.println(actual);
87
-        // Then
88
-        assertTrue(actual.equals(Q1));
89
-    }
90
-
91
-}
8
+	@Test
9
+	public void rotateStringTest0() {
10
+		// Given
11
+		String s1 = "ABCDEF";
12
+		String s2 = "ABCDEF";
13
+
14
+		// When
15
+		ROT13 cipher = new ROT13();
16
+		String actual = cipher.rotate(s1, 'A');
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
+		ROT13 cipher = new ROT13();
30
+		String actual = cipher.rotate(s1, 'D');
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
+		ROT13 cipher = new ROT13();
44
+		String actual = cipher.rotate(s1, 'N');
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
+		ROT13 cipher = new ROT13('a', 'n');
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
+		assertTrue(actual.equals(A1));
68
+
69
+		// When
70
+		String actual2 = cipher.decrypt(Q2);
71
+		System.out.println(Q2);
72
+		System.out.println(A2);
73
+		// Then
74
+		assertTrue(actual2.equals(A2));
75
+	}
76
+
77
+	@Test
78
+	public void cryptTest2() {
79
+		// Given
80
+		ROT13 cipher = new ROT13('a', 'n');
81
+
82
+		String Q1 = "Why did the chicken cross the road?";
83
+		System.out.println(Q1);
84
+
85
+		// When
86
+		String actual = cipher.crypt(cipher.crypt(Q1));
87
+		System.out.println(actual);
88
+		// Then
89
+		assertTrue(actual.equals(Q1));
90
+	}
91
+
92
+	@Test
93
+	public void cryptTestMax() {
94
+		for (int i = 0; i < Integer.MAX_VALUE; i++) {
95
+			System.out.println(i + " ");
96
+			if (i % 16 == 8) System.out.println(i);
97
+		}
98
+
99
+
100
+	}
101
+}
102
+

+ 0
- 0
Crypto/src/sonnetEncrypt Wyświetl plik


+ 25
- 0
SimpleCrypt.iml Wyświetl plik

@@ -0,0 +1,25 @@
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_8">
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" isTestSource="false" />
8
+      <excludeFolder url="file://$MODULE_DIR$/target" />
9
+    </content>
10
+    <orderEntry type="inheritedJdk" />
11
+    <orderEntry type="sourceFolder" forTests="false" />
12
+    <orderEntry type="library" exported="" name="Maven: junit:junit:4.12" level="project" />
13
+    <orderEntry type="library" exported="" name="Maven: org.hamcrest:hamcrest-core:1.3" level="project" />
14
+    <orderEntry type="library" exported="" name="Maven: org.junit.jupiter:junit-jupiter-api:5.3.0-M1" level="project" />
15
+    <orderEntry type="library" exported="" name="Maven: org.apiguardian:apiguardian-api:1.0.0" level="project" />
16
+    <orderEntry type="library" exported="" name="Maven: org.opentest4j:opentest4j:1.1.0" level="project" />
17
+    <orderEntry type="library" exported="" name="Maven: org.junit.platform:junit-platform-commons:1.3.0-M1" level="project" />
18
+    <orderEntry type="library" exported="" name="Maven: org.testng:testng:6.14.3" level="project" />
19
+    <orderEntry type="library" exported="" name="Maven: com.beust:jcommander:1.72" level="project" />
20
+    <orderEntry type="library" exported="" name="Maven: org.apache-extras.beanshell:bsh:2.0b6" level="project" />
21
+    <orderEntry type="library" name="settings" level="project" />
22
+    <orderEntry type="library" name="pom" level="project" />
23
+    <orderEntry type="library" name="apache-maven-3.5" level="project" />
24
+  </component>
25
+</module>

+ 26
- 0
pom.xml Wyświetl plik

@@ -7,6 +7,32 @@
7 7
     <groupId>com.zipcodewilmington</groupId>
8 8
     <artifactId>SimpleCrypt</artifactId>
9 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
+        <dependency>
18
+            <groupId>org.junit.jupiter</groupId>
19
+            <artifactId>junit-jupiter-api</artifactId>
20
+            <version>RELEASE</version>
21
+            <scope>compile</scope>
22
+        </dependency>
23
+        <dependency>
24
+            <groupId>org.testng</groupId>
25
+            <artifactId>testng</artifactId>
26
+            <version>RELEASE</version>
27
+            <scope>compile</scope>
28
+        </dependency>
29
+        <dependency>
30
+            <groupId>junit</groupId>
31
+            <artifactId>junit</artifactId>
32
+            <version>RELEASE</version>
33
+            <scope>compile</scope>
34
+        </dependency>
35
+    </dependencies>
10 36
 
11 37
 
12 38
 </project>

BIN
target/.DS_Store Wyświetl plik


BIN
target/classes/.DS_Store Wyświetl plik


+ 0
- 0
target/classes/sonnetEncrypt Wyświetl plik