Просмотр исходного кода

functional but doesnt work unless its symmetrical

Jennifer Chao 7 лет назад
Родитель
Сommit
e2f8d9d838
6 измененных файлов: 121 добавлений и 50 удалений
  1. 0
    32
      Crypto/src/ROT13.java
  2. 74
    0
      Crypto/src/main/ROT13.java
  3. 5
    2
      Crypto/src/test/ROT13Test.java
  4. 13
    15
      README.md
  5. 15
    0
      SimpleCrypt.iml
  6. 14
    1
      pom.xml

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

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

@@ -0,0 +1,74 @@
1
+package Crypto.src.main;
2
+
3
+import static java.lang.Character.isLowerCase;
4
+import static java.lang.Character.isUpperCase;
5
+import static java.lang.Character.toLowerCase;
6
+
7
+public class ROT13  {
8
+
9
+    private String[] upperAlpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".split("");
10
+    private String[] lowerAlpha = "abcdefghijklmnopqrstuvwxyz".split("");
11
+    private int shift;
12
+    private boolean symmetric = false;
13
+    // startUpper, registerUpper, startLower, registerLower
14
+
15
+    // make code so that it works for others (e.g. ROT4 [a to d], etc.)
16
+
17
+    public ROT13(Character cs, Character cf) {
18
+        if (toLowerCase(cs) == 'a' && toLowerCase(cf) == 'n') {
19
+            symmetric = true;
20
+        }
21
+    }
22
+
23
+    public ROT13() {
24
+        this('a', 'm');
25
+    }
26
+
27
+
28
+    public String crypt(String text) throws UnsupportedOperationException {
29
+        if (!symmetric) {
30
+            throw new UnsupportedOperationException();
31
+        }
32
+        return encrypt(text);
33
+    }
34
+
35
+    public String encrypt(String text) {
36
+        StringBuilder builder = new StringBuilder();
37
+
38
+        for (int i = 0; i < text.length(); i++) {
39
+            char c = text.toLowerCase().charAt(i);
40
+            if (c >= 'a' && c <= 'z') {
41
+                char newC = (char) (c + 13);
42
+
43
+                if (newC > 'z') {
44
+                    newC = (char) (newC - 26);
45
+                }
46
+
47
+                if (Character.isUpperCase(text.charAt(i))) {
48
+                   newC = Character.toUpperCase(newC);
49
+                }
50
+
51
+                builder.append(newC);
52
+            } else {
53
+                builder.append(c);
54
+            }
55
+        }
56
+
57
+        for (int i = 0; i < builder.length(); i++) {
58
+
59
+        }
60
+
61
+        return builder.toString();
62
+    }
63
+
64
+    public String decrypt(String text) {
65
+        return encrypt(text);
66
+    }
67
+
68
+    public static String rotate(String s, Character c) {
69
+        int offset = s.indexOf(c);
70
+        int i = offset % s.length();
71
+        return s.substring(i) + s.substring(0, i);
72
+    }
73
+
74
+}

Crypto/src/ROT13Test.java → Crypto/src/test/ROT13Test.java Просмотреть файл

@@ -1,6 +1,8 @@
1
-import org.junit.Test;
1
+package Crypto.src.test;
2 2
 
3
-import static org.junit.Assert.*;
3
+import Crypto.src.main.ROT13;
4
+import org.junit.Test;
5
+import static org.junit.Assert.assertTrue;
4 6
 
5 7
 public class ROT13Test {
6 8
 
@@ -28,6 +30,7 @@ public class ROT13Test {
28 30
         // When
29 31
         ROT13 cipher = new ROT13();
30 32
         String actual = cipher.rotate(s1, 'D');
33
+        System.out.println(actual);
31 34
 
32 35
         // Then
33 36
         assertTrue(actual.equals(s2));

+ 13
- 15
README.md Просмотреть файл

@@ -4,12 +4,12 @@ a simple set of crypt problems.
4 4
 ### Part 1
5 5
 Create a few ciphers. Use String inside of your classes.
6 6
 
7
-* ROT13 - take the 26 letters of the alphabet and create a `String <- crypt(String)` method in the ROT13 class
7
+* Crypto.src.main.ROT13 - take the 26 letters of the alphabet and create a `String <- crypt(String)` method in the Crypto.src.main.ROT13 class
8 8
   * crypt("Why did the chicken cross the road?") should produce "Jul qvq gur puvpxra pebff gur ebnq?"
9 9
   * crypt("Gb trg gb gur bgure fvqr!") should produce "To get to the other side!"
10
-* Make a constructor that takes two arguments to set the cipher correspondence. `ROT13 superSecure = new ROT13("a","m");`
10
+* Make a constructor that takes two arguments to set the cipher correspondence. `Crypto.src.main.ROT13 superSecure = new Crypto.src.main.ROT13("a","m");`
11 11
   * this defines the SHIFT of the two Character arrays.
12
-* Caesar - make a subclass of ROT13 that implements the famous caesar cipher.
12
+* Caesar - make a subclass of Crypto.src.main.ROT13 that implements the famous caesar cipher.
13 13
 * Create you own cipher, using a different set of 
14 14
 
15 15
 ### Part 2
@@ -19,45 +19,43 @@ Prove that when you read in (sonnet18.enc), run the same crypt again, and prove
19 19
 
20 20
 ## Explanation
21 21
 
22
-ROT13 ("rotate by 13 places", sometimes hyphenated ROT-13) is a simple letter substitution cipher that replaces a letter with the 13th letter after it, in the alphabet. ROT13 is a special case of the Caesar cipher, developed in ancient Rome.
22
+Crypto.src.main.ROT13 ("rotate by 13 places", sometimes hyphenated ROT-13) is a simple letter substitution cipher that replaces a letter with the 13th letter after it, in the alphabet. Crypto.src.main.ROT13 is a special case of the Caesar cipher, developed in ancient Rome.
23 23
 
24
-Because there are 26 letters (2×13) in the basic Latin alphabet, ROT13 is its own inverse; that is, to undo ROT13, the same algorithm is applied, so the same action can be used for encoding and decoding. The algorithm provides virtually no cryptographic security, and is often cited as a canonical example of weak encryption.
24
+Because there are 26 letters (2×13) in the basic Latin alphabet, Crypto.src.main.ROT13 is its own inverse; that is, to undo Crypto.src.main.ROT13, the same algorithm is applied, so the same action can be used for encoding and decoding. The algorithm provides virtually no cryptographic security, and is often cited as a canonical example of weak encryption.
25 25
 
26
-ROT13 is used in online forums as a means of hiding spoilers, punchlines, puzzle solutions, and offensive materials from the casual glance. ROT13 has been described as the "Usenet equivalent of a magazine printing the answer to a quiz upside down".[2] ROT13 has inspired a variety of letter and word games on-line, and is frequently mentioned in newsgroup conversations.
26
+Crypto.src.main.ROT13 is used in online forums as a means of hiding spoilers, punchlines, puzzle solutions, and offensive materials from the casual glance. Crypto.src.main.ROT13 has been described as the "Usenet equivalent of a magazine printing the answer to a quiz upside down".[2] Crypto.src.main.ROT13 has inspired a variety of letter and word games on-line, and is frequently mentioned in newsgroup conversations.
27 27
 
28
-Applying ROT13 to a piece of text merely requires examining its alphabetic characters and replacing each one by the letter 13 places further along in the alphabet, wrapping back to the beginning if necessary.[3] A becomes N, B becomes O, and so on up to M, which becomes Z, then the sequence continues at the beginning of the alphabet: N becomes A, O becomes B, and so on to Z, which becomes M. Only those letters which occur in the English alphabet are affected; numbers, symbols, whitespace, and all other characters are left unchanged.
28
+Applying Crypto.src.main.ROT13 to a piece of text merely requires examining its alphabetic characters and replacing each one by the letter 13 places further along in the alphabet, wrapping back to the beginning if necessary.[3] A becomes N, B becomes O, and so on up to M, which becomes Z, then the sequence continues at the beginning of the alphabet: N becomes A, O becomes B, and so on to Z, which becomes M. Only those letters which occur in the English alphabet are affected; numbers, symbols, whitespace, and all other characters are left unchanged.
29 29
 
30 30
 ```Java
31 31
 String s = "we hold these truths to be self evident";
32 32
 
33
-//WHEN you create a ROT13 with 'a' and 'n' THEN 
34
-
35
-if (crypt(crypt(s)) == s) {
33
+Crypto.src.main.ROT1Crypto.src.main.ROT13 (crypt(crypt(s)) == s) {
36 34
   return true;
37 35
 }
38 36
 
39 37
 //if anything else, you must use the encrypt/decrypt pair.
40 38
 ```
41
-In other words, two successive applications of ROT13 restore the original text (in mathematics, this is sometimes called an involution; in cryptography, a reciprocal cipher).
39
+In other words, two successive applications of ROT13 restore the original Crypto.src.main.ROT13(in mathematics, this is sometimes called an involution; in cryptography, a reciprocal cipher).
42 40
 
43 41
 The transformation can be done using a lookup table, such as the following:
44 42
 
45 43
 ```
46 44
 // for ROT13('a', 'n')
47
-Input	ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz
45
+Input	ABCDECrypto.src.main.ROT13KLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz
48 46
 Output	NOPQRSTUVWXYZABCDEFGHIJKLM nopqrstuvwxyzabcdefghijklm
49 47
 
50 48
 // for ROT13('a', 'd')
51
-Input	ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz
49
+Input	ABCDECrypto.src.main.ROT13KLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz
52 50
 Output	DEFGHIJKLMNOPQRSTUVWXYZABC defghijklmnopqrstuvwxyzabc
53 51
 ```
54 52
 For example, in the following joke, the punchline has been obscured by ROT13:
55 53
 
56 54
 ```
57
-Why did the chicken cross the road?
55
+Why did the chiCrypto.src.main.ROT13cross the road?
58 56
 Gb trg gb gur bgure fvqr!
59 57
 ```
60
-Transforming the entire text via ROT13 form, the answer to the joke is revealed:
58
+Transforming the entire text via ROT13 form, the answer to tCrypto.src.main.ROT13ke is revealed:
61 59
 ```
62 60
 Jul qvq gur puvpxra pebff gur ebnq?
63 61
 To get to the other side!

+ 15
- 0
SimpleCrypt.iml Просмотреть файл

@@ -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$" 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>

+ 14
- 1
pom.xml Просмотреть файл

@@ -8,5 +8,18 @@
8 8
     <artifactId>SimpleCrypt</artifactId>
9 9
     <version>1.0-SNAPSHOT</version>
10 10
 
11
-
11
+    <dependencies>
12
+        <!-- https://mvnrepository.com/artifact/junit/junit -->
13
+        <dependency>
14
+            <groupId>junit</groupId>
15
+            <artifactId>junit</artifactId>
16
+            <version>4.12</version>
17
+            <scope>test</scope>
18
+        </dependency>
19
+        <dependency>
20
+            <groupId>junit</groupId>
21
+            <artifactId>junit</artifactId>
22
+            <version>4.12</version>
23
+        </dependency>
24
+    </dependencies>
12 25
 </project>