From fcbd8b7f9c8eefe860b4a153e036da11685bcbfb Mon Sep 17 00:00:00 2001 From: D45Hub Date: Thu, 9 Nov 2023 09:46:06 +0100 Subject: [PATCH] Initial basic version of project. --- pom.xml | 75 +++++++++++++++++++ .../de/denisthiessen/AlgorithmFactory.java | 22 ++++++ src/main/java/de/denisthiessen/App.java | 25 +++++++ .../algorithms/CaesarAlgorithm.java | 24 ++++++ .../algorithms/HashingAlgorithm.java | 20 +++++ .../denisthiessen/algorithms/IAlgorithm.java | 7 ++ .../algorithms/NoEncryptionAlgorithm.java | 13 ++++ .../algorithms/Rot13Algorithm.java | 28 +++++++ .../algorithms/XORAlgorithm.java | 24 ++++++ src/test/java/de/denisthiessen/AppTest.java | 61 +++++++++++++++ 10 files changed, 299 insertions(+) create mode 100644 pom.xml create mode 100644 src/main/java/de/denisthiessen/AlgorithmFactory.java create mode 100644 src/main/java/de/denisthiessen/App.java create mode 100644 src/main/java/de/denisthiessen/algorithms/CaesarAlgorithm.java create mode 100644 src/main/java/de/denisthiessen/algorithms/HashingAlgorithm.java create mode 100644 src/main/java/de/denisthiessen/algorithms/IAlgorithm.java create mode 100644 src/main/java/de/denisthiessen/algorithms/NoEncryptionAlgorithm.java create mode 100644 src/main/java/de/denisthiessen/algorithms/Rot13Algorithm.java create mode 100644 src/main/java/de/denisthiessen/algorithms/XORAlgorithm.java create mode 100644 src/test/java/de/denisthiessen/AppTest.java diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..581daec --- /dev/null +++ b/pom.xml @@ -0,0 +1,75 @@ + + + + 4.0.0 + + de.denisthiessen + simpleencryptor + 1.0-SNAPSHOT + + simpleencryptor + + http://www.example.com + + + UTF-8 + 1.7 + 1.7 + + + + + junit + junit + 4.11 + test + + + + + + + + + maven-clean-plugin + 3.1.0 + + + + maven-resources-plugin + 3.0.2 + + + maven-compiler-plugin + 3.8.0 + + + maven-surefire-plugin + 2.22.1 + + + maven-jar-plugin + 3.0.2 + + + maven-install-plugin + 2.5.2 + + + maven-deploy-plugin + 2.8.2 + + + + maven-site-plugin + 3.7.1 + + + maven-project-info-reports-plugin + 3.0.0 + + + + + diff --git a/src/main/java/de/denisthiessen/AlgorithmFactory.java b/src/main/java/de/denisthiessen/AlgorithmFactory.java new file mode 100644 index 0000000..1fbd123 --- /dev/null +++ b/src/main/java/de/denisthiessen/AlgorithmFactory.java @@ -0,0 +1,22 @@ +package de.denisthiessen; + +import de.denisthiessen.algorithms.CaesarAlgorithm; +import de.denisthiessen.algorithms.HashingAlgorithm; +import de.denisthiessen.algorithms.IAlgorithm; +import de.denisthiessen.algorithms.NoEncryptionAlgorithm; +import de.denisthiessen.algorithms.Rot13Algorithm; +import de.denisthiessen.algorithms.XORAlgorithm; + +public class AlgorithmFactory { + + public static IAlgorithm getEncryptionAlgorithm(String algorithm) { + + switch(algorithm) { + case "rot13": return new Rot13Algorithm(); + case "hash": return new HashingAlgorithm(); + case "xor": return new XORAlgorithm(); + case "caesar": return new CaesarAlgorithm(); + default : return new NoEncryptionAlgorithm(); + } + } +} diff --git a/src/main/java/de/denisthiessen/App.java b/src/main/java/de/denisthiessen/App.java new file mode 100644 index 0000000..cd35f57 --- /dev/null +++ b/src/main/java/de/denisthiessen/App.java @@ -0,0 +1,25 @@ +package de.denisthiessen; + +import java.util.Scanner; + +import de.denisthiessen.algorithms.IAlgorithm; + +public class App +{ + public static void main( String[] args ) + { + Scanner scanner = new Scanner(System.in); + System.out.print("Enter your encryption algorithm: "); + String algorithm = scanner.nextLine(); + + System.out.print("Enter your source text: "); + String sourceText = scanner.nextLine(); + + scanner.close(); + + IAlgorithm encryptionAlgorithm = AlgorithmFactory.getEncryptionAlgorithm(algorithm); + String encryptedText = encryptionAlgorithm.executeAlgorithm(sourceText); + + System.out.println(encryptedText); + } +} diff --git a/src/main/java/de/denisthiessen/algorithms/CaesarAlgorithm.java b/src/main/java/de/denisthiessen/algorithms/CaesarAlgorithm.java new file mode 100644 index 0000000..bbda296 --- /dev/null +++ b/src/main/java/de/denisthiessen/algorithms/CaesarAlgorithm.java @@ -0,0 +1,24 @@ +package de.denisthiessen.algorithms; + +public class CaesarAlgorithm implements IAlgorithm { + + public CaesarAlgorithm() { + + } + + @Override + public String executeAlgorithm(String sourceString) { + int shift = 42; + StringBuilder encryptedText = new StringBuilder(); + for (char c : sourceString.toCharArray()) { + if (Character.isLetter(c)) { + char base = Character.isLowerCase(c) ? 'a' : 'A'; + encryptedText.append((char) (((c - base + shift) % 26) + base)); + } else { + encryptedText.append(c); + } + } + return encryptedText.toString(); + } + +} diff --git a/src/main/java/de/denisthiessen/algorithms/HashingAlgorithm.java b/src/main/java/de/denisthiessen/algorithms/HashingAlgorithm.java new file mode 100644 index 0000000..67b971a --- /dev/null +++ b/src/main/java/de/denisthiessen/algorithms/HashingAlgorithm.java @@ -0,0 +1,20 @@ +package de.denisthiessen.algorithms; + +public class HashingAlgorithm implements IAlgorithm { + + public HashingAlgorithm() { + + } + + @Override + public String executeAlgorithm(String sourceString) { + int hashCode = 7; + char[] inputCharacters = sourceString.toCharArray(); + + for(char character : inputCharacters) { + hashCode = hashCode*31 + ((int)character); + } + + return String.valueOf(hashCode); + } +} diff --git a/src/main/java/de/denisthiessen/algorithms/IAlgorithm.java b/src/main/java/de/denisthiessen/algorithms/IAlgorithm.java new file mode 100644 index 0000000..900de12 --- /dev/null +++ b/src/main/java/de/denisthiessen/algorithms/IAlgorithm.java @@ -0,0 +1,7 @@ +package de.denisthiessen.algorithms; + +public interface IAlgorithm { + + public String executeAlgorithm(String sourceString); + +} diff --git a/src/main/java/de/denisthiessen/algorithms/NoEncryptionAlgorithm.java b/src/main/java/de/denisthiessen/algorithms/NoEncryptionAlgorithm.java new file mode 100644 index 0000000..b159aa2 --- /dev/null +++ b/src/main/java/de/denisthiessen/algorithms/NoEncryptionAlgorithm.java @@ -0,0 +1,13 @@ +package de.denisthiessen.algorithms; + +public class NoEncryptionAlgorithm implements IAlgorithm { + + public NoEncryptionAlgorithm() { + + } + + @Override + public String executeAlgorithm(String sourceString) { + return sourceString; + } +} diff --git a/src/main/java/de/denisthiessen/algorithms/Rot13Algorithm.java b/src/main/java/de/denisthiessen/algorithms/Rot13Algorithm.java new file mode 100644 index 0000000..1db2de1 --- /dev/null +++ b/src/main/java/de/denisthiessen/algorithms/Rot13Algorithm.java @@ -0,0 +1,28 @@ +package de.denisthiessen.algorithms; + +public class Rot13Algorithm implements IAlgorithm { + + public Rot13Algorithm() { + + } + + @Override + public String executeAlgorithm(String sourceString) { + StringBuilder sb = new StringBuilder(); + + for (int i = 0; i < sourceString.length(); i++) { + char c = sourceString.charAt(i); + if (c >= '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(); + } +} diff --git a/src/main/java/de/denisthiessen/algorithms/XORAlgorithm.java b/src/main/java/de/denisthiessen/algorithms/XORAlgorithm.java new file mode 100644 index 0000000..d445e6c --- /dev/null +++ b/src/main/java/de/denisthiessen/algorithms/XORAlgorithm.java @@ -0,0 +1,24 @@ +package de.denisthiessen.algorithms; + +public class XORAlgorithm implements IAlgorithm { + + public XORAlgorithm() { + + } + + @Override + public String executeAlgorithm(String sourceString) { + + String key = "secretkey"; + StringBuilder encryptedText = new StringBuilder(); + for (int i = 0; i < sourceString.length(); i++) { + char plainChar = sourceString.charAt(i); + char keyChar = key.charAt(i % key.length()); + + // XOR the plaintext character with the corresponding key character + char encryptedChar = (char) (plainChar ^ keyChar); + encryptedText.append(encryptedChar); + } + return encryptedText.toString(); + } +} diff --git a/src/test/java/de/denisthiessen/AppTest.java b/src/test/java/de/denisthiessen/AppTest.java new file mode 100644 index 0000000..d6adb5c --- /dev/null +++ b/src/test/java/de/denisthiessen/AppTest.java @@ -0,0 +1,61 @@ +package de.denisthiessen; + +import static org.junit.Assert.assertTrue; + +import org.junit.Test; + +import de.denisthiessen.algorithms.IAlgorithm; + +/** + * Unit test for simple encryptor. + */ +public class AppTest +{ + @Test + public void testCasesarCipher() + { + IAlgorithm encryptionAlgorithm = AlgorithmFactory.getEncryptionAlgorithm("caesar"); + String encryptedText = encryptionAlgorithm.executeAlgorithm("Helloworld"); + System.out.println(encryptedText); + + assertTrue(encryptedText.equals("Xubbemehbt")); + } + + @Test + public void testXorCipher() + { + IAlgorithm encryptionAlgorithm = AlgorithmFactory.getEncryptionAlgorithm("xor"); + String encryptedText = encryptionAlgorithm.executeAlgorithm("test"); + System.out.println(encryptedText); + String expectedText = new String(new byte[]{7, 0, 16, 6}); + + assertTrue(encryptedText.equals(expectedText)); + } + + @Test + public void testHashingCipher() + { + IAlgorithm encryptionAlgorithm = AlgorithmFactory.getEncryptionAlgorithm("hash"); + String encryptedText = encryptionAlgorithm.executeAlgorithm("Helloworld"); + + assertTrue(encryptedText.equals("775124327")); + } + + @Test + public void testRot13Cipher() + { + IAlgorithm encryptionAlgorithm = AlgorithmFactory.getEncryptionAlgorithm("rot13"); + String encryptedText = encryptionAlgorithm.executeAlgorithm("Helloworld"); + + assertTrue(encryptedText.equals("Uryybjbeyq")); + } + + @Test + public void testNoCipher() + { + IAlgorithm encryptionAlgorithm = AlgorithmFactory.getEncryptionAlgorithm("nocipher"); + String encryptedText = encryptionAlgorithm.executeAlgorithm("Helloworld"); + + assertTrue(encryptedText.equals("Helloworld")); + } +}