Bläddra i källkod

working decrypt

Nick Satinover 7 år sedan
förälder
incheckning
48bffdbeed
6 ändrade filer med 209 tillägg och 34 borttagningar
  1. 92
    0
      Crypto/src/Main/ROT13.java
  2. 91
    0
      Crypto/src/Main/ROT13A.java
  3. 0
    32
      Crypto/src/ROT13.java
  4. 2
    2
      Crypto/src/Test/ROT13Test.java
  5. 16
    0
      SimpleCrypt.iml
  6. 8
    0
      pom.xml

+ 92
- 0
Crypto/src/Main/ROT13.java Visa fil

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

+ 91
- 0
Crypto/src/Main/ROT13A.java Visa fil

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

+ 0
- 32
Crypto/src/ROT13.java Visa fil

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

Crypto/src/ROT13Test.java → Crypto/src/Test/ROT13Test.java Visa fil

@@ -14,7 +14,7 @@ public class ROT13Test {
14 14
         // When
15 15
         ROT13 cipher = new ROT13();
16 16
         String actual = cipher.rotate(s1, 'A');
17
-
17
+        System.out.println(actual);
18 18
         // Then
19 19
         assertTrue(actual.equals(s2));
20 20
     }
@@ -51,7 +51,7 @@ public class ROT13Test {
51 51
     @Test
52 52
     public void cryptTest1() {
53 53
         // Given
54
-        ROT13 cipher = new ROT13('a', 'n');
54
+        ROT13A cipher = new ROT13A('a', 'n');
55 55
 
56 56
         String Q1 = "Why did the chicken cross the road?";
57 57
         String A1 = "Jul qvq gur puvpxra pebff gur ebnq?";

+ 16
- 0
SimpleCrypt.iml Visa fil

@@ -0,0 +1,16 @@
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 Visa fil

@@ -7,6 +7,14 @@
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
+    </dependencies>
10 18
 
11 19
 
12 20
 </project>