diff --git a/Crypto/src/ROT13.java b/Crypto/src/ROT13.java index 1c58731..aaa883e 100644 --- a/Crypto/src/ROT13.java +++ b/Crypto/src/ROT13.java @@ -1,32 +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 int shift; ROT13(Character cs, Character cf) { + this.shift = cs - cf; } ROT13() { + shift = 13; } - public String crypt(String text) throws UnsupportedOperationException { - - return ""; + 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 text; + public static String encrypt(String text) { + return crypt(text); } public String decrypt(String text) { - return text; + return crypt(text); } public static String rotate(String s, Character c) { + //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); + } + public static String textFile(String filename){ + StringBuilder sb = new StringBuilder(); - return ""; + 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 400c38b..a507015 100644 --- a/Crypto/src/ROT13Test.java +++ b/Crypto/src/ROT13Test.java @@ -1,5 +1,9 @@ +package src; +import org.junit.Assert; import org.junit.Test; +import java.io.File; + import static org.junit.Assert.*; public class ROT13Test { @@ -61,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)); @@ -70,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)); } @@ -87,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); + } +} 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