diff --git a/.gitignore b/.gitignore index 5f96b60..e2ebfae 100644 --- a/.gitignore +++ b/.gitignore @@ -22,3 +22,30 @@ hs_err_pid* .idea/ + +# Apple files +*.DS_Store +.AppleDouble +.LSOverride + +/target/ +.classpath +.project +.settings + + +# User-specific stuff: +*.iml +.idea/** +.idea/**/workspace.xml +.idea/**/tasks.xml +.idea/dictionaries + +# Sensitive or high-churn files: +.idea/**/dataSources/ +.idea/**/dataSources.ids +.idea/**/dataSources.xml +.idea/**/dataSources.local.xml +.idea/**/sqlDataSources.xml +.idea/**/dynamic.xml +.idea/**/uiDesigner.xml \ No newline at end of file diff --git a/Crypto/src/ROT13.java b/Crypto/src/ROT13.java deleted file mode 100644 index 1c58731..0000000 --- a/Crypto/src/ROT13.java +++ /dev/null @@ -1,32 +0,0 @@ -import static java.lang.Character.isLowerCase; -import static java.lang.Character.isUpperCase; -import static java.lang.Character.toLowerCase; - -public class ROT13 { - - ROT13(Character cs, Character cf) { - } - - ROT13() { - } - - - public String crypt(String text) throws UnsupportedOperationException { - - return ""; - } - - public String encrypt(String text) { - return text; - } - - public String decrypt(String text) { - return text; - } - - public static String rotate(String s, Character c) { - - return ""; - } - -} diff --git a/Crypto/src/main/java/ROT13.java b/Crypto/src/main/java/ROT13.java new file mode 100644 index 0000000..34b486b --- /dev/null +++ b/Crypto/src/main/java/ROT13.java @@ -0,0 +1,88 @@ +import java.sql.SQLOutput; +import java.util.Map; +import java.util.TreeMap; + +public class ROT13 { + + Map upperCaseMap = new TreeMap<>(); + Map lowerCaseMap = new TreeMap<>(); + + ROT13(Character cs, Character cf) { + populateMap(); + } + + ROT13() { + new ROT13('a', 'n'); + } + + + public String crypt(String text) throws UnsupportedOperationException { + + return encrypt(text); + } + + public String encrypt(String text) { + char[] sTemp = text.toCharArray(); + String result = ""; + + + for (int i = 0; i < sTemp.length; i++) { + if (getCharNumericValue(sTemp[i]) > 64 && getCharNumericValue(sTemp[i]) < 91) { + //upper case + result = result.concat(upperCaseMap.get(Character.toString(sTemp[i]))); + } else if (getCharNumericValue(sTemp[i]) > 96 && getCharNumericValue(sTemp[i]) < 122) { + //lower case + result = result.concat(lowerCaseMap.get(Character.toString(sTemp[i]))); + } else { + result = result.concat(getCharacterValue(getCharNumericValue(sTemp[i])).toString()); + } + } + return result; + } + + public String decrypt(String text) { + String result = encrypt(text); + return result; + } + + public static String rotate(String s, Character c) { + String[] temp = s.split(""); + String result = ""; + int location = s.indexOf(c); + + for (int i = 0; i < temp.length; i++) { + if (i < location) { + result = result.concat(temp[location + i]); + + } else { + result = result.concat(temp[Math.abs(i - location)]); + } + } + return result; + } + + private void populateMap() { + String s1 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; + String s2 = s1.toLowerCase(); + + String[] tempUpper = s1.split(""); + String[] rotatedUpper = rotate(s1, 'N').split(""); + + String[] tempLower = s2.split(""); + String[] rotatedLower = rotate(s2, 'n').split(""); + + for (int i = 0; i < tempUpper.length; i++) { + this.upperCaseMap.put(tempUpper[i], rotatedUpper[i]); + this.lowerCaseMap.put(tempLower[i], rotatedLower[i]); + } + + } + + public Integer getCharNumericValue(final char character) { + return (int) character; + } + + public Character getCharacterValue(final int ascii) { + return (char) ascii; + } +} diff --git a/Crypto/src/ROT13Test.java b/Crypto/src/test/java/ROT13Test.java similarity index 99% rename from Crypto/src/ROT13Test.java rename to Crypto/src/test/java/ROT13Test.java index 400c38b..a04e8f4 100644 --- a/Crypto/src/ROT13Test.java +++ b/Crypto/src/test/java/ROT13Test.java @@ -46,6 +46,7 @@ public void rotateStringTest2() { System.out.println(actual); // Then assertTrue(actual.equals(s2)); + } @Test @@ -88,4 +89,5 @@ public void cryptTest2() { assertTrue(actual.equals(Q1)); } + } \ No newline at end of file diff --git a/SimpleCrypt.iml b/SimpleCrypt.iml new file mode 100644 index 0000000..cc4985d --- /dev/null +++ b/SimpleCrypt.iml @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml index 4021c2d..206a707 100644 --- a/pom.xml +++ b/pom.xml @@ -8,5 +8,19 @@ SimpleCrypt 1.0-SNAPSHOT + + + junit + junit + 4.12 + test + + + junit + junit + 4.12 + test + +