Bladeren bron

added another cipher

Allison Ziegler 6 jaren geleden
bovenliggende
commit
55ffe12404
5 gewijzigde bestanden met toevoegingen van 105 en 11 verwijderingen
  1. 6
    0
      Crypto/src/CipherTwo.java
  2. 48
    0
      Crypto/src/CipherTwoTest.java
  3. 28
    11
      Crypto/src/ROT13.java
  4. 15
    0
      SimpleCrypt.iml
  5. 8
    0
      pom.xml

+ 6
- 0
Crypto/src/CipherTwo.java Bestand weergeven

@@ -0,0 +1,6 @@
1
+public class CipherTwo extends ROT13 {
2
+
3
+    public CipherTwo(Character cs, Character cf) {
4
+        super(cs, Character.isLowerCase(cf) ? (char) ('a' + (cf - 'a') / 2) : (char) ('A' + (cf - 'A') / 2));
5
+    }
6
+}

+ 48
- 0
Crypto/src/CipherTwoTest.java Bestand weergeven

@@ -0,0 +1,48 @@
1
+import org.junit.Test;
2
+
3
+import static org.junit.Assert.*;
4
+
5
+public class CipherTwoTest {
6
+
7
+    @Test
8
+    public void cryptTest1() {
9
+        // Given
10
+        CipherTwo cipher = new CipherTwo('a', 'n');
11
+
12
+        String Q1 = "Why did the chicken cross the road?";
13
+        String A1 = "Cne joj znk inoiqkt ixuyy znk xugj?";
14
+
15
+        String Q2 = "Zu mkz zu znk uznkx yojk!";
16
+        String A2 = "To get to the other side!";
17
+
18
+        // When
19
+        String actual = cipher.encrypt(Q1);
20
+        System.out.println(Q1);
21
+        System.out.println(A1);
22
+        // Then
23
+        assertTrue(actual.equals(A1));
24
+
25
+        // When
26
+        String actual2 = cipher.decrypt(Q2);
27
+        System.out.println(Q2);
28
+        System.out.println(A2);
29
+        // Then
30
+        assertTrue(actual2.equals(A2));
31
+    }
32
+    @Test
33
+    public void cryptTest2() {
34
+        // Given
35
+        CipherTwo cipher = new CipherTwo('a', 'n');
36
+
37
+        String Q1 = "Why did the chicken cross the road?";
38
+        System.out.println(Q1);
39
+
40
+        String expected = "Itk pup ftq otuowqz odaee ftq damp?";
41
+        // When
42
+        String actual = cipher.crypt(cipher.crypt(Q1));
43
+        System.out.println(actual);
44
+        // Then
45
+        assertTrue(actual.equals(expected));
46
+    }
47
+
48
+}

+ 28
- 11
Crypto/src/ROT13.java Bestand weergeven

@@ -1,32 +1,49 @@
1
-import static java.lang.Character.isLowerCase;
2
-import static java.lang.Character.isUpperCase;
3
-import static java.lang.Character.toLowerCase;
4
-
5 1
 public class ROT13  {
6 2
 
3
+    private int shift;
4
+
7 5
     ROT13(Character cs, Character cf) {
6
+        shift = cf - cs;
8 7
     }
9 8
 
10 9
     ROT13() {
10
+        this.shift = 13;
11 11
     }
12 12
 
13
-
14 13
     public String crypt(String text) throws UnsupportedOperationException {
15
-
16
-        return "";
14
+        return encrypt(text);
17 15
     }
18 16
 
19 17
     public String encrypt(String text) {
20
-        return text;
18
+        StringBuilder encrypted = new StringBuilder();
19
+        for (int i = 0; i < text.length(); i++) {
20
+            if (Character.isLowerCase(text.charAt(i)))
21
+                encrypted.append((char) ('a' + ( (text.charAt(i) - 'a')  + shift) % 26));
22
+            else if (Character.isUpperCase((text.charAt(i))))
23
+                encrypted.append((char) ('A' + ( (text.charAt(i) - 'A') + shift) %26));
24
+            else encrypted.append(text.charAt(i));
25
+            System.out.println(encrypted);
26
+        }
27
+        return encrypted.toString();
21 28
     }
22 29
 
23 30
     public String decrypt(String text) {
24
-        return text;
31
+        StringBuilder encrypted = new StringBuilder();
32
+        for (int i = 0; i < text.length(); i++) {
33
+            if (Character.isLowerCase(text.charAt(i)))
34
+                encrypted.append((char) ('a' + ( (text.charAt(i) - 'a')  + (26 - shift)) % 26));
35
+            else if (Character.isUpperCase((text.charAt(i))))
36
+                encrypted.append((char) ('A' + ( (text.charAt(i) - 'A') + (26 - shift)) %26));
37
+            else encrypted.append(text.charAt(i));
38
+        }
39
+        return encrypted.toString();
25 40
     }
26 41
 
27 42
     public static String rotate(String s, Character c) {
28
-
29
-        return "";
43
+        int index = s.indexOf(c);
44
+        String beginning = s.substring(index, s.length());
45
+        String end = s.substring(0, index);
46
+        return beginning + end;
30 47
     }
31 48
 
32 49
 }

+ 15
- 0
SimpleCrypt.iml Bestand weergeven

@@ -0,0 +1,15 @@
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" 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" name="Maven: junit:junit:4.12" level="project" />
13
+    <orderEntry type="library" name="Maven: org.hamcrest:hamcrest-core:1.3" level="project" />
14
+  </component>
15
+</module>

+ 8
- 0
pom.xml Bestand weergeven

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