#13 Jason Gibbs, Jase256

Открыто
JaseG256 хочет смерджить 2 коммит(ов) из JaseG256/ZCW-SimpleCrypt0:master в master
5 измененных файлов: 155 добавлений и 16 удалений
  1. 1
    0
      BogusFile
  2. 91
    13
      Crypto/src/ROT13.java
  3. 30
    2
      Crypto/src/ROT13Test.java
  4. 19
    0
      SimpleCrypt.iml
  5. 14
    1
      pom.xml

+ 1
- 0
BogusFile Просмотреть файл

@@ -0,0 +1 @@
1
+Guvf svyr arrqf gb or ernq naq rapelcgrq. Vg pna nyfb or hfrq sbe svyr ernqvat cenpgvpr!

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

@@ -1,32 +1,110 @@
1
+import java.io.*;
2
+import java.util.ArrayList;
3
+import java.util.logging.Logger;
4
+
1 5
 import static java.lang.Character.isLowerCase;
2 6
 import static java.lang.Character.isUpperCase;
3 7
 import static java.lang.Character.toLowerCase;
4 8
 
5
-public class ROT13  {
9
+public class ROT13 {
6 10
 
7
-    ROT13(Character cs, Character cf) {
8
-    }
11
+    private int shifter;
9 12
 
10
-    ROT13() {
11
-    }
13
+    public ROT13(Character cs, Character cf) { shifter = cf - cs; }
12 14
 
15
+    public ROT13() { this.shifter = 13; }
13 16
 
14
-    public String crypt(String text) throws UnsupportedOperationException {
17
+    public String crypt(String text) throws UnsupportedOperationException
18
+    {
19
+        StringBuilder sb = new StringBuilder();
15 20
 
16
-        return "";
21
+        for (int i = 0; i < text.length(); i++)
22
+        {
23
+            sb = (!Character.isLetter(text.charAt(i))) ? sb.append(text.charAt(i)) :
24
+                    (Character.isUpperCase(text.charAt(i))) ?
25
+                            sb.append((char) (((int) text.charAt(i) + shifter - 65) % 26 + 65)) :
26
+                            sb.append((char) (((int) text.charAt(i) + shifter - 97) % 26 + 97));
27
+        }
28
+        return sb.toString();
17 29
     }
18 30
 
19
-    public String encrypt(String text) {
20
-        return text;
31
+
32
+    public String encrypt(String text) { return crypt(text); }
33
+
34
+    public String decrypt(String text) { return crypt(text); }
35
+
36
+    public static String rotate(String s, Character c)
37
+    {
38
+        String rotated = s.substring(s.indexOf(c)) + s.substring(0, s.indexOf(c)); return rotated;
21 39
     }
22 40
 
23
-    public String decrypt(String text) {
24
-        return text;
41
+    public String encryptFile(File fileIn, String fileOut)
42
+    {
43
+        String fileInput;  StringBuilder builder = new StringBuilder();  BufferedReader reader = null;
44
+
45
+        try { reader = new BufferedReader(new FileReader(fileIn));
46
+
47
+            while ((fileInput = reader.readLine()) != null) { builder.append(crypt(fileInput)); } }
48
+
49
+          catch (FileNotFoundException e) { System.out.println("Unable to read file " + fileIn); }
50
+          catch (IOException e) { e.printStackTrace(); }
51
+
52
+        finally { if (reader != null) { try { reader.close(); } catch (IOException e) { e.printStackTrace(); } } }
53
+
54
+        BufferedWriter writer = null;
55
+
56
+        try { writer = new BufferedWriter(new FileWriter(new File(fileOut))); writer.write(builder.toString()); }
57
+
58
+            catch (IOException e) { e.printStackTrace(); }
59
+
60
+            finally { try { if (writer != null) { writer.close(); } } catch (IOException e) { e.printStackTrace(); }
61
+        }
62
+        return builder.toString();
25 63
     }
26 64
 
27
-    public static String rotate(String s, Character c) {
65
+    class EncryptOffset {
66
+
67
+        private int offset;
68
+        private boolean isOffset;
28 69
 
29
-        return "";
70
+        public EncryptOffset(Character offset) {
71
+            super();
72
+            this.offset = offset;
73
+            isOffset = false;
74
+        }
75
+
76
+        public String encryptWithOffset(String text) {
77
+            StringBuilder sb = new StringBuilder();
78
+
79
+            for (int i = 0; i < text.length(); i++)
80
+            {
81
+                if (isOffset = false) {
82
+                    sb = (!Character.isLetter(text.charAt(i))) ? sb.append(text.charAt(i)) :
83
+                            (Character.isUpperCase(text.charAt(i))) ?
84
+                                    sb.append((char) (((int) text.charAt(i) + shifter - 65) % 26 + 65)) :
85
+                                    sb.append((char) (((int) text.charAt(i) + shifter - 97) % 26 + 97));
86
+                    isOffset = true;
87
+                } else if (isOffset = true){
88
+                    sb = (!Character.isLetter(text.charAt(i))) ? sb.append(text.charAt(i)) :
89
+                            (Character.isUpperCase(text.charAt(i))) ?
90
+                                    sb.append((char) ((((int) text.charAt(i) + shifter - 65) - offset) % 26 + 65)) :
91
+                                    sb.append((char) ((((int) text.charAt(i) + shifter - 97) - offset) % 26 + 97));
92
+                    isOffset = false;
93
+                }
94
+            }
95
+            return sb.toString();
96
+        }
30 97
     }
31 98
 
99
+    public static void main(String[] args)
100
+    {
101
+        ROT13 rot13 = new ROT13();
102
+        File file = new File("/Users/jasong/Labs/ZCW-ORM-SimpleAccount/.idea/EnCryptMe.txt");
103
+       // rot13.encryptFile(file, "/Users/jasong/Labs/ZCW-ORM-SimpleAccount/.idea/IamEncrypted.txt");
104
+        ROT13.EncryptOffset encryptOffset =  rot13.new EncryptOffset('b');
105
+        System.out.println(encryptOffset.encryptWithOffset
106
+                ("This file needs to be read and encrypted. It can also be used for file reading practice!"));
107
+    }
108
+
109
+
32 110
 }

+ 30
- 2
Crypto/src/ROT13Test.java Просмотреть файл

@@ -1,6 +1,13 @@
1
-import org.junit.Test;
2 1
 
3
-import static org.junit.Assert.*;
2
+import org.junit.jupiter.api.Test;
3
+
4
+import java.io.BufferedReader;
5
+import java.io.File;
6
+import java.io.FileNotFoundException;
7
+import java.io.FileReader;
8
+
9
+import static org.junit.jupiter.api.Assertions.assertEquals;
10
+import static org.junit.jupiter.api.Assertions.assertTrue;
4 11
 
5 12
 public class ROT13Test {
6 13
 
@@ -88,4 +95,25 @@ public class ROT13Test {
88 95
         assertTrue(actual.equals(Q1));
89 96
     }
90 97
 
98
+    @Test
99
+    public void encryptFileTest1() {
100
+        ROT13 rot13 = new ROT13();
101
+//        rot13.encryptFile(new File("/Users/jasong/Labs/ZCW-ORM-SimpleAccount/.idea/EnCryptMe.txt"));
102
+        String expected = rot13.encrypt("This file needs to be read and encrypted. " +
103
+                "It can also be used for file reading practice!");
104
+        String actual = rot13.encryptFile(new File("/Users/jasong/Labs/ZCW-ORM-SimpleAccount/.idea/EnCryptMe.txt"), "BogusFile");
105
+        assertEquals(expected, actual);
106
+    }
107
+
108
+    @Test
109
+    public void encryptFileTest2() {
110
+        ROT13 rot13 = new ROT13();
111
+        File file = new File("/Users/jasong/Labs/ZCW-ORM-SimpleAccount/.idea/EnCryptMe.txt");
112
+//        rot13.encryptFile(new File("/Users/jasong/Labs/ZCW-ORM-SimpleAccount/.idea/EnCryptMe.txt"));
113
+        String expected = rot13.encrypt("This file needs to be read and encrypted. " +
114
+                                "It can also be used for file reading practice!");
115
+        String actual = rot13.encryptFile(file, "BogusFile");
116
+        assertEquals(expected, actual);
117
+    }
118
+
91 119
 }

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

@@ -0,0 +1,19 @@
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" scope="TEST" name="Maven: junit:junit:4.12" level="project" />
13
+    <orderEntry type="library" scope="TEST" name="Maven: org.hamcrest:hamcrest-core:1.3" level="project" />
14
+    <orderEntry type="library" name="Maven: org.junit.jupiter:junit-jupiter-api:5.3.0-M1" level="project" />
15
+    <orderEntry type="library" name="Maven: org.apiguardian:apiguardian-api:1.0.0" level="project" />
16
+    <orderEntry type="library" name="Maven: org.opentest4j:opentest4j:1.1.0" level="project" />
17
+    <orderEntry type="library" name="Maven: org.junit.platform:junit-platform-commons:1.3.0-M1" level="project" />
18
+  </component>
19
+</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><!-- https://mvnrepository.com/artifact/junit/junit -->
12
+    <dependency>
13
+        <groupId>junit</groupId>
14
+        <artifactId>junit</artifactId>
15
+        <version>4.12</version>
16
+        <scope>test</scope>
17
+    </dependency>
18
+    <dependency>
19
+        <groupId>org.junit.jupiter</groupId>
20
+        <artifactId>junit-jupiter-api</artifactId>
21
+        <version>RELEASE</version>
22
+        <scope>compile</scope>
23
+    </dependency>
24
+</dependencies>
12 25
 </project>