From b6bf47a363783003ac07d9cf2f689b2404524199 Mon Sep 17 00:00:00 2001 From: Charlotte Beale Date: Thu, 14 Nov 2019 13:23:37 -0500 Subject: [PATCH 1/3] Started --- Crypto/src/ROT13.java | 28 +++++++++++++++++++++++----- SimpleCrypt.iml | 12 ++++++++++++ 2 files changed, 35 insertions(+), 5 deletions(-) create mode 100644 SimpleCrypt.iml diff --git a/Crypto/src/ROT13.java b/Crypto/src/ROT13.java index 1c58731..4b75cb1 100644 --- a/Crypto/src/ROT13.java +++ b/Crypto/src/ROT13.java @@ -3,17 +3,35 @@ import static java.lang.Character.toLowerCase; public class ROT13 { +private final startUpperCase; +protected String startLower; +protected String startUpper; +protected String registerLower; +protected String registerUpper; +private boolean symmetric = false; ROT13(Character cs, Character cf) { - } + if(toLowerCase(cs) == 'a' && toLowerCase(cf) == 'n') symmetric = true; + + startUpper = ROT13.rotate(uppercaseStart, Character.toUpperCase(cs)); + startLower = ROT13.rotate(lowercaseStart, Character.toLowerCase(cs)); + + registerUpper = ROT13.rotate(uppercaseStart, Character.toUpperCase(cf)); + registerLower = ROT13.rotate(lowercaseStart, Character.toLowerCase(cf)); + + System.out.println(startUpper); + System.out.println(registerUpper); + System.out.println(startLower); + System.out.println(registerLower); + ROT13() { + this.('a', 'n'); } - public String crypt(String text) throws UnsupportedOperationException { - - return ""; + if (this.symmetric != true) throw new Unsupported OperationalException(); + return substituteIn(text, startUpper, registerUpper, startLower, registerLower); } public String encrypt(String text) { @@ -21,7 +39,7 @@ public String encrypt(String text) { } public String decrypt(String text) { - return text; + return substituteIn(registerUpper, startUpper, registerLower, startLower); } public static String rotate(String s, Character c) { diff --git a/SimpleCrypt.iml b/SimpleCrypt.iml new file mode 100644 index 0000000..4fd5057 --- /dev/null +++ b/SimpleCrypt.iml @@ -0,0 +1,12 @@ + + + + + + + + + + + + \ No newline at end of file From 499b4892f447c652c355e986044e972da3f1d6b5 Mon Sep 17 00:00:00 2001 From: Charlotte Beale Date: Fri, 15 Nov 2019 21:51:19 -0500 Subject: [PATCH 2/3] Not Sure how to run test, these are .java files and not classes --- Crypto/src/ROT13.java | 21 ++++++++++++++++++--- Crypto/src/ROT13Test.java | 1 + 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/Crypto/src/ROT13.java b/Crypto/src/ROT13.java index 4b75cb1..50ffdf8 100644 --- a/Crypto/src/ROT13.java +++ b/Crypto/src/ROT13.java @@ -4,6 +4,8 @@ public class ROT13 { private final startUpperCase; +private String lowercaseStart = "abcdefghijqlmnpqrstuvwxyz"; +private String uppercaseStart = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; protected String startLower; protected String startUpper; protected String registerLower; @@ -31,15 +33,15 @@ public class ROT13 { public String crypt(String text) throws UnsupportedOperationException { if (this.symmetric != true) throw new Unsupported OperationalException(); - return substituteIn(text, startUpper, registerUpper, startLower, registerLower); + return substitute(text, startUpper, registerUpper, startLower, registerLower); } public String encrypt(String text) { - return text; + return substitute(text,startUpper, registerUpper, startLower, registerLower); } public String decrypt(String text) { - return substituteIn(registerUpper, startUpper, registerLower, startLower); + return substitute(text,startUpper, registerUpper, startLower, registerLower); } public static String rotate(String s, Character c) { @@ -47,4 +49,17 @@ public static String rotate(String s, Character c) { return ""; } + private String substitute(String text, String startUpper, String registerUpper, String startLower, String registerLower){ + StringBuilder sb = new StringBuilder(); + for(int i = 0; i < text.length(); i++){ + Character ch = text.charAt(i); + Integer position = 0; + if (isUpperCase(ch)) { + position = startUpper.indexOf(ch); + sb.append(registerUpper.charAt(i)); + } + } + + return null; + } } diff --git a/Crypto/src/ROT13Test.java b/Crypto/src/ROT13Test.java index 400c38b..fba1363 100644 --- a/Crypto/src/ROT13Test.java +++ b/Crypto/src/ROT13Test.java @@ -1,3 +1,4 @@ +package src; import org.junit.Test; import static org.junit.Assert.*; From 4318cc72bb10532c06c1348d4289c7dd6916f7e4 Mon Sep 17 00:00:00 2001 From: Charlotte Beale Date: Sat, 21 Dec 2019 14:59:47 -0500 Subject: [PATCH 3/3] Complete --- Crypto/src/ROT13.java | 92 +++++++++++++++++++++------------------ Crypto/src/ROT13Test.java | 34 ++++++++++++++- 2 files changed, 82 insertions(+), 44 deletions(-) diff --git a/Crypto/src/ROT13.java b/Crypto/src/ROT13.java index 50ffdf8..aaa883e 100644 --- a/Crypto/src/ROT13.java +++ b/Crypto/src/ROT13.java @@ -1,65 +1,73 @@ +package src +import java.io.File; +import java.io.FileNotFoundException; +import java.util.Scanner; + import static java.lang.Character.isLowerCase; import static java.lang.Character.isUpperCase; import static java.lang.Character.toLowerCase; public class ROT13 { -private final startUpperCase; -private String lowercaseStart = "abcdefghijqlmnpqrstuvwxyz"; -private String uppercaseStart = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; -protected String startLower; -protected String startUpper; -protected String registerLower; -protected String registerUpper; -private boolean symmetric = false; - + private int shift; ROT13(Character cs, Character cf) { - if(toLowerCase(cs) == 'a' && toLowerCase(cf) == 'n') symmetric = true; - - startUpper = ROT13.rotate(uppercaseStart, Character.toUpperCase(cs)); - startLower = ROT13.rotate(lowercaseStart, Character.toLowerCase(cs)); - - registerUpper = ROT13.rotate(uppercaseStart, Character.toUpperCase(cf)); - registerLower = ROT13.rotate(lowercaseStart, Character.toLowerCase(cf)); - - System.out.println(startUpper); - System.out.println(registerUpper); - System.out.println(startLower); - System.out.println(registerLower); - + this.shift = cs - cf; + } ROT13() { - this.('a', 'n'); + shift = 13; } - public String crypt(String text) throws UnsupportedOperationException { - if (this.symmetric != true) throw new Unsupported OperationalException(); - return substitute(text, startUpper, registerUpper, startLower, registerLower); + + public static String crypt(String text) throws UnsupportedOperationException { + StringBuilder sb = new StringBuilder(); + char [] chars = text.toCharArray(); + for (int i = 0; i = 'a' && c <= 'm') { + c += 13; + }else if (c >= 'A' && c <= 'M') { + c += 13; + } else if (c >= 'n' && c <= 'z') { + c -= 13; + } else if (c >= 'N' && c <= 'Z') { + c -= 13; + } + sb.append(c); + } + return sb.toString(); } - public String encrypt(String text) { - return substitute(text,startUpper, registerUpper, startLower, registerLower); + public static String encrypt(String text) { + return crypt(text); } public String decrypt(String text) { - return substitute(text,startUpper, registerUpper, startLower, registerLower); + return crypt(text); } public static String rotate(String s, Character c) { - - return ""; + //this method will rotate the given String + //by the character placement + String sub = s.substring(0,s.indexOf(c)); + String sub1 = s.substring(s.indexOf(c)); + //then concat the two substrings. + return sub1.concat(sub); } - - private String substitute(String text, String startUpper, String registerUpper, String startLower, String registerLower){ + public static String textFile(String filename){ StringBuilder sb = new StringBuilder(); - for(int i = 0; i < text.length(); i++){ - Character ch = text.charAt(i); - Integer position = 0; - if (isUpperCase(ch)) { - position = startUpper.indexOf(ch); - sb.append(registerUpper.charAt(i)); - } - } - return null; + File file = new File(filename); + try(Scanner sc = new Scanner(file)) { + while(sc.hasNextLine()){ + String line = sc.nextLine(); + String xored = encrypt(line); + sb.append(xored+"\n"); + } + sb.deleteCharAt(sb.length()-1); + } catch (FileNotFoundException e){ + e.printStackTrace(); } + return sb.toString(); + } + } diff --git a/Crypto/src/ROT13Test.java b/Crypto/src/ROT13Test.java index fba1363..a507015 100644 --- a/Crypto/src/ROT13Test.java +++ b/Crypto/src/ROT13Test.java @@ -1,6 +1,9 @@ package src; +import org.junit.Assert; import org.junit.Test; +import java.io.File; + import static org.junit.Assert.*; public class ROT13Test { @@ -62,8 +65,11 @@ public void cryptTest1() { // When String actual = cipher.encrypt(Q1); + System.out.println(actual); + System.out.println(Q1); System.out.println(A1); + // System.out.println(actual); // Then assertTrue(actual.equals(A1)); @@ -71,6 +77,7 @@ public void cryptTest1() { String actual2 = cipher.decrypt(Q2); System.out.println(Q2); System.out.println(A2); + System.out.println(actual2); // Then assertTrue(actual2.equals(A2)); } @@ -88,5 +95,28 @@ public void cryptTest2() { // Then assertTrue(actual.equals(Q1)); } - -} \ No newline at end of file + @Test + public void textEncryptFile(){ + //Given + File file = new File("sonnet18.txt"); + //When + String expected = ROT13.encrypt("Shall I compare thee to a summer’s day?\n" + + "Thou art more lovely and more temperate:\n" + + "Rough winds do shake the darling buds of May,\n" + + "And summer’s lease hath all too short a date;\n" + + "Sometime too hot the eye of heaven shines,\n" + + "And often is his gold complexion dimm'd;\n" + + "And every fair from fair sometime declines,\n" + + "By chance or nature’s changing course untrimm'd;\n" + + "But thy eternal summer shall not fade,\n" + + "Nor lose possession of that fair thou ow’st;\n" + + "Nor shall death brag thou wander’st in his shade,\n" + + "When in eternal lines to time thou grow’st:\n" + + "So long as men can breathe or eyes can see,\n" + + "So long lives this, and this gives life to thee."); + + String actual = ROT13.textFile("sonnet18.txt"); + //Then + Assert.assertEquals(expected,actual); + } +}