D45Hub
1 year ago
10 changed files with 299 additions and 0 deletions
-
75pom.xml
-
22src/main/java/de/denisthiessen/AlgorithmFactory.java
-
25src/main/java/de/denisthiessen/App.java
-
24src/main/java/de/denisthiessen/algorithms/CaesarAlgorithm.java
-
20src/main/java/de/denisthiessen/algorithms/HashingAlgorithm.java
-
7src/main/java/de/denisthiessen/algorithms/IAlgorithm.java
-
13src/main/java/de/denisthiessen/algorithms/NoEncryptionAlgorithm.java
-
28src/main/java/de/denisthiessen/algorithms/Rot13Algorithm.java
-
24src/main/java/de/denisthiessen/algorithms/XORAlgorithm.java
-
61src/test/java/de/denisthiessen/AppTest.java
@ -0,0 +1,75 @@ |
|||
<?xml version="1.0" encoding="UTF-8"?> |
|||
|
|||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
|||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> |
|||
<modelVersion>4.0.0</modelVersion> |
|||
|
|||
<groupId>de.denisthiessen</groupId> |
|||
<artifactId>simpleencryptor</artifactId> |
|||
<version>1.0-SNAPSHOT</version> |
|||
|
|||
<name>simpleencryptor</name> |
|||
<!-- FIXME change it to the project's website --> |
|||
<url>http://www.example.com</url> |
|||
|
|||
<properties> |
|||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> |
|||
<maven.compiler.source>1.7</maven.compiler.source> |
|||
<maven.compiler.target>1.7</maven.compiler.target> |
|||
</properties> |
|||
|
|||
<dependencies> |
|||
<dependency> |
|||
<groupId>junit</groupId> |
|||
<artifactId>junit</artifactId> |
|||
<version>4.11</version> |
|||
<scope>test</scope> |
|||
</dependency> |
|||
</dependencies> |
|||
|
|||
<build> |
|||
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --> |
|||
<plugins> |
|||
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle --> |
|||
<plugin> |
|||
<artifactId>maven-clean-plugin</artifactId> |
|||
<version>3.1.0</version> |
|||
</plugin> |
|||
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging --> |
|||
<plugin> |
|||
<artifactId>maven-resources-plugin</artifactId> |
|||
<version>3.0.2</version> |
|||
</plugin> |
|||
<plugin> |
|||
<artifactId>maven-compiler-plugin</artifactId> |
|||
<version>3.8.0</version> |
|||
</plugin> |
|||
<plugin> |
|||
<artifactId>maven-surefire-plugin</artifactId> |
|||
<version>2.22.1</version> |
|||
</plugin> |
|||
<plugin> |
|||
<artifactId>maven-jar-plugin</artifactId> |
|||
<version>3.0.2</version> |
|||
</plugin> |
|||
<plugin> |
|||
<artifactId>maven-install-plugin</artifactId> |
|||
<version>2.5.2</version> |
|||
</plugin> |
|||
<plugin> |
|||
<artifactId>maven-deploy-plugin</artifactId> |
|||
<version>2.8.2</version> |
|||
</plugin> |
|||
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle --> |
|||
<plugin> |
|||
<artifactId>maven-site-plugin</artifactId> |
|||
<version>3.7.1</version> |
|||
</plugin> |
|||
<plugin> |
|||
<artifactId>maven-project-info-reports-plugin</artifactId> |
|||
<version>3.0.0</version> |
|||
</plugin> |
|||
</plugins> |
|||
</pluginManagement> |
|||
</build> |
|||
</project> |
@ -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(); |
|||
} |
|||
} |
|||
} |
@ -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); |
|||
} |
|||
} |
@ -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(); |
|||
} |
|||
|
|||
} |
@ -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); |
|||
} |
|||
} |
@ -0,0 +1,7 @@ |
|||
package de.denisthiessen.algorithms; |
|||
|
|||
public interface IAlgorithm { |
|||
|
|||
public String executeAlgorithm(String sourceString); |
|||
|
|||
} |
@ -0,0 +1,13 @@ |
|||
package de.denisthiessen.algorithms; |
|||
|
|||
public class NoEncryptionAlgorithm implements IAlgorithm { |
|||
|
|||
public NoEncryptionAlgorithm() { |
|||
|
|||
} |
|||
|
|||
@Override |
|||
public String executeAlgorithm(String sourceString) { |
|||
return sourceString; |
|||
} |
|||
} |
@ -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(); |
|||
} |
|||
} |
@ -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(); |
|||
} |
|||
} |
@ -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")); |
|||
} |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue