Browse Source

oops, added part2

Allison Ziegler 6 years ago
parent
commit
1996256f0a
4 changed files with 63 additions and 2 deletions
  1. 39
    1
      Crypto/src/ROT13.java
  2. 9
    0
      Crypto/src/ROT13Test.java
  3. 1
    1
      SimpleCrypt.iml
  4. 14
    0
      sonnet18.enc

+ 39
- 1
Crypto/src/ROT13.java View File

@@ -1,3 +1,8 @@
1
+import java.io.File;
2
+import java.io.IOException;
3
+import java.io.PrintWriter;
4
+import java.util.Scanner;
5
+
1 6
 public class ROT13  {
2 7
 
3 8
     private int shift;
@@ -22,7 +27,6 @@ public class ROT13  {
22 27
             else if (Character.isUpperCase((text.charAt(i))))
23 28
                 encrypted.append((char) ('A' + ( (text.charAt(i) - 'A') + shift) %26));
24 29
             else encrypted.append(text.charAt(i));
25
-            System.out.println(encrypted);
26 30
         }
27 31
         return encrypted.toString();
28 32
     }
@@ -46,4 +50,38 @@ public class ROT13  {
46 50
         return beginning + end;
47 51
     }
48 52
 
53
+    public String encryptFile(String readFile, String writeFile) {
54
+        File file = new File(readFile);
55
+        String encrypted = null;
56
+        try(Scanner scanner = new Scanner(file)){
57
+
58
+            String result = fileString(readFile);
59
+            encrypted = encrypt(result);
60
+
61
+            PrintWriter writer = new PrintWriter(writeFile, "UTF-8");
62
+            writer.print(encrypted);
63
+            writer.close();
64
+
65
+        }catch(IOException e){
66
+            e.printStackTrace();
67
+        }
68
+        return encrypted;
69
+    }
70
+    public String fileString (String fileName) {
71
+        File file = new File(fileName);
72
+        StringBuilder result = new StringBuilder("");
73
+        try(Scanner scanner = new Scanner(file)){
74
+            while(scanner.hasNextLine()){
75
+                String line = scanner.nextLine();
76
+                result.append(line).append("\n");
77
+            }
78
+            scanner.close();
79
+            return result.toString();
80
+        }
81
+        catch(IOException e){
82
+            e.printStackTrace();
83
+            return null;
84
+        }
85
+    }
86
+
49 87
 }

+ 9
- 0
Crypto/src/ROT13Test.java View File

@@ -1,3 +1,4 @@
1
+import org.junit.Assert;
1 2
 import org.junit.Test;
2 3
 
3 4
 import static org.junit.Assert.*;
@@ -87,5 +88,13 @@ public class ROT13Test {
87 88
         // Then
88 89
         assertTrue(actual.equals(Q1));
89 90
     }
91
+    @Test
92
+    public void sonnetTest() {
93
+        ROT13 cipher = new ROT13();
94
+        String expected = cipher.fileString("sonnet18.txt");
95
+        cipher.encryptFile("sonnet18.txt", "sonnet18.enc");
96
+        String actual = cipher.encryptFile("sonnet18.enc", "sonnet18.enc");
97
+        Assert.assertEquals(expected, actual);
98
+    }
90 99
 
91 100
 }

+ 1
- 1
SimpleCrypt.iml View File

@@ -1,6 +1,6 @@
1 1
 <?xml version="1.0" encoding="UTF-8"?>
2 2
 <module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
3
-  <component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_5">
3
+  <component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8">
4 4
     <output url="file://$MODULE_DIR$/target/classes" />
5 5
     <output-test url="file://$MODULE_DIR$/target/test-classes" />
6 6
     <content url="file://$MODULE_DIR$">

+ 14
- 0
sonnet18.enc View File

@@ -0,0 +1,14 @@
1
+Shall I compare thee to a summer’s day?
2
+Thou art more lovely and more temperate:
3
+Rough winds do shake the darling buds of May,
4
+And summer’s lease hath all too short a date;
5
+Sometime too hot the eye of heaven shines,
6
+And often is his gold complexion dimm'd;
7
+And every fair from fair sometime declines,
8
+By chance or nature’s changing course untrimm'd;
9
+But thy eternal summer shall not fade,
10
+Nor lose possession of that fair thou ow’st;
11
+Nor shall death brag thou wander’st in his shade,
12
+When in eternal lines to time thou grow’st:
13
+   So long as men can breathe or eyes can see,
14
+   So long lives this, and this gives life to thee.