diff --git a/src/main/java/pl/nightdev701/OpenAPI.java b/src/main/java/pl/nightdev701/OpenAPI.java index 5e154a7..414747c 100644 --- a/src/main/java/pl/nightdev701/OpenAPI.java +++ b/src/main/java/pl/nightdev701/OpenAPI.java @@ -20,9 +20,10 @@ import pl.nightdev701.logger.AbstractLogger; import pl.nightdev701.logger.standard.DefaultLogger; import pl.nightdev701.manager.CryptManager; +import pl.nightdev701.manager.KeyGeneratorManager; import pl.nightdev701.network.http.HttpRequestHandler; import pl.nightdev701.network.tcp.ProxyAdapter; -import pl.nightdev701.util.crypto.CryptType; +import pl.nightdev701.util.CryptType; import pl.nightdev701.util.stream.OpenPrintStream; import java.io.FileNotFoundException; @@ -34,6 +35,25 @@ public class OpenAPI { + /** + * generate keys + * + * @param type + */ + public static KeyGeneratorManager getKeyGeneratorManager(CryptType type) { + return getKeyGeneratorManager(type, new DefaultLogger()); + } + + /** + * generate keys + * + * @param type + * @param logger + */ + public static KeyGeneratorManager getKeyGeneratorManager(CryptType type, AbstractLogger logger) { + return new KeyGeneratorManager(type, logger); + } + /** * encrypt and decrypt strings * @@ -65,11 +85,11 @@ public static DatabaseConnector getDatabaseManager(DatabaseFormular formular, Da return getDatabaseManager(formular, type, port, new DefaultLogger()); } - public static ScorpionFileReader getFileReader(String path, AbstractLogger logger){ + public static ScorpionFileReader getFileReader(String path, AbstractLogger logger) { return new ScorpionFileReader(path, logger); } - public static ScorpionFileReader getFileReader(String path){ + public static ScorpionFileReader getFileReader(String path) { return getFileReader(path, new DefaultLogger()); } diff --git a/src/main/java/pl/nightdev701/crypto/BlowFishCrypto.java b/src/main/java/pl/nightdev701/crypto/BlowFishCrypto.java index 3dba3fd..7eb7249 100644 --- a/src/main/java/pl/nightdev701/crypto/BlowFishCrypto.java +++ b/src/main/java/pl/nightdev701/crypto/BlowFishCrypto.java @@ -1,7 +1,6 @@ package pl.nightdev701.crypto; import pl.nightdev701.logger.AbstractLogger; -import pl.nightdev701.logger.standard.DefaultLogger; import pl.nightdev701.util.crypto.CryptoForm; import javax.crypto.Cipher; @@ -16,10 +15,6 @@ public class BlowFishCrypto implements CryptoForm { private final String encryptionKey; private final AbstractLogger logger; - public BlowFishCrypto(String encryptionKey) { - this(encryptionKey, new DefaultLogger()); - } - public BlowFishCrypto(String encryptionKey, AbstractLogger logger) { this.encryptionKey = encryptionKey; this.logger = logger; diff --git a/src/main/java/pl/nightdev701/crypto/CryptoAes.java b/src/main/java/pl/nightdev701/crypto/CryptoAes.java index 6b942f4..1bfc83d 100644 --- a/src/main/java/pl/nightdev701/crypto/CryptoAes.java +++ b/src/main/java/pl/nightdev701/crypto/CryptoAes.java @@ -11,7 +11,6 @@ */ import pl.nightdev701.logger.AbstractLogger; -import pl.nightdev701.logger.standard.DefaultLogger; import pl.nightdev701.util.crypto.CryptoForm; import javax.crypto.Cipher; @@ -25,10 +24,6 @@ public class CryptoAes implements CryptoForm { private final String encryptionKey; private final AbstractLogger logger; - public CryptoAes(String encryptionKey) { - this(encryptionKey, new DefaultLogger()); - } - public CryptoAes(String encryptionKey, AbstractLogger logger) { this.encryptionKey = encryptionKey; this.logger = logger; diff --git a/src/main/java/pl/nightdev701/crypto/generator/AESKeyGenerator.java b/src/main/java/pl/nightdev701/crypto/generator/AESKeyGenerator.java new file mode 100644 index 0000000..6c42902 --- /dev/null +++ b/src/main/java/pl/nightdev701/crypto/generator/AESKeyGenerator.java @@ -0,0 +1,41 @@ +package pl.nightdev701.crypto.generator; + +import pl.nightdev701.logger.AbstractLogger; +import pl.nightdev701.util.key.CryptKeyGenerator; + +import javax.crypto.KeyGenerator; +import javax.crypto.SecretKey; +import java.security.NoSuchAlgorithmException; +import java.util.logging.Level; + +public class AESKeyGenerator implements CryptKeyGenerator { + + AbstractLogger logger; + + public AESKeyGenerator(AbstractLogger logger) { + this.logger = logger; + } + + @Override + public String generateKey() { + try { + KeyGenerator keyGenerator = KeyGenerator.getInstance("AES"); + + keyGenerator.init(128); + + SecretKey secretKey = keyGenerator.generateKey(); + + byte[] keyBytes = secretKey.getEncoded(); + + logger.log(Level.INFO, "Key generated for algorithms: AES"); + + return java.util.Base64.getEncoder().encodeToString(keyBytes); + + } catch (NoSuchAlgorithmException e) { + e.printStackTrace(); + logger.log(Level.INFO, "Key failed to generate for algorithms: AES"); + } + return null; + } + +} \ No newline at end of file diff --git a/src/main/java/pl/nightdev701/crypto/generator/BlowFishGenerator.java b/src/main/java/pl/nightdev701/crypto/generator/BlowFishGenerator.java new file mode 100644 index 0000000..c2a0737 --- /dev/null +++ b/src/main/java/pl/nightdev701/crypto/generator/BlowFishGenerator.java @@ -0,0 +1,41 @@ +package pl.nightdev701.crypto.generator; + +import pl.nightdev701.logger.AbstractLogger; +import pl.nightdev701.util.key.CryptKeyGenerator; + +import javax.crypto.KeyGenerator; +import javax.crypto.SecretKey; +import java.security.NoSuchAlgorithmException; +import java.util.logging.Level; + +public class BlowFishGenerator implements CryptKeyGenerator { + + private AbstractLogger logger; + + public BlowFishGenerator(AbstractLogger logger) { + this.logger = logger; + } + + @Override + public String generateKey() { + try { + KeyGenerator keyGenerator = KeyGenerator.getInstance("Blowfish"); + + keyGenerator.init(128); + + SecretKey secretKey = keyGenerator.generateKey(); + + byte[] keyBytes = secretKey.getEncoded(); + + logger.log(Level.INFO, "Key generated for algorithms: Blowfish"); + + return java.util.Base64.getEncoder().encodeToString(keyBytes); + + } catch (NoSuchAlgorithmException e) { + e.printStackTrace(); + logger.log(Level.INFO, "Key failed to generate for algorithms: AES"); + } + return null; + } + +} diff --git a/src/main/java/pl/nightdev701/io/ScorpionFileReader.java b/src/main/java/pl/nightdev701/io/ScorpionFileReader.java index be1cffd..7512fc7 100644 --- a/src/main/java/pl/nightdev701/io/ScorpionFileReader.java +++ b/src/main/java/pl/nightdev701/io/ScorpionFileReader.java @@ -23,9 +23,9 @@ public ScorpionFileReader(String filePath, AbstractLogger logger) { bufferedReader = new BufferedReader(fileReader); currentLine = bufferedReader.readLine(); // Lese die erste Zeile } catch (FileNotFoundException e) { - logger.log(Level.WARNING,"Die Datei wurde nicht gefunden: " + e.getMessage()); + logger.log(Level.WARNING, "Die Datei wurde nicht gefunden: " + e.getMessage()); } catch (IOException e) { - logger.log(Level.WARNING,"Fehler beim Lesen der Datei: " + e.getMessage()); + logger.log(Level.WARNING, "Fehler beim Lesen der Datei: " + e.getMessage()); } } @@ -37,7 +37,7 @@ public void readNextLine() { try { currentLine = bufferedReader.readLine(); // Gehe zur nächsten Zeile } catch (IOException e) { - logger.log(Level.WARNING,"Fehler beim Lesen der nächsten Zeile: " + e.getMessage()); + logger.log(Level.WARNING, "Fehler beim Lesen der nächsten Zeile: " + e.getMessage()); } } @@ -47,7 +47,7 @@ public void close() { bufferedReader.close(); } } catch (IOException e) { - logger.log(Level.WARNING,"Fehler beim Schließen des Readers: " + e.getMessage()); + logger.log(Level.WARNING, "Fehler beim Schließen des Readers: " + e.getMessage()); } } diff --git a/src/main/java/pl/nightdev701/manager/CryptManager.java b/src/main/java/pl/nightdev701/manager/CryptManager.java index 0740c7f..21ae545 100644 --- a/src/main/java/pl/nightdev701/manager/CryptManager.java +++ b/src/main/java/pl/nightdev701/manager/CryptManager.java @@ -3,7 +3,7 @@ import pl.nightdev701.crypto.BlowFishCrypto; import pl.nightdev701.crypto.CryptoAes; import pl.nightdev701.logger.AbstractLogger; -import pl.nightdev701.util.crypto.CryptType; +import pl.nightdev701.util.CryptType; import pl.nightdev701.util.crypto.CryptoForm; public class CryptManager { @@ -12,10 +12,10 @@ public class CryptManager { public CryptManager(String key, CryptType type, AbstractLogger logger) { if (type == CryptType.AES) { - this.crypt = new CryptoAes(key); + this.crypt = new CryptoAes(key, logger); } if (type == CryptType.BLOWFISH) { - this.crypt = new BlowFishCrypto(key); + this.crypt = new BlowFishCrypto(key, logger); } } @@ -38,4 +38,5 @@ public String decrypt(String text) { public CryptoForm getCrypt() { return crypt; } + } diff --git a/src/main/java/pl/nightdev701/manager/KeyGeneratorManager.java b/src/main/java/pl/nightdev701/manager/KeyGeneratorManager.java new file mode 100644 index 0000000..708bfd5 --- /dev/null +++ b/src/main/java/pl/nightdev701/manager/KeyGeneratorManager.java @@ -0,0 +1,27 @@ +package pl.nightdev701.manager; + +import pl.nightdev701.crypto.generator.AESKeyGenerator; +import pl.nightdev701.crypto.generator.BlowFishGenerator; +import pl.nightdev701.logger.AbstractLogger; +import pl.nightdev701.util.CryptType; +import pl.nightdev701.util.key.CryptKeyGenerator; + +public class KeyGeneratorManager { + + private CryptKeyGenerator generator; + + public KeyGeneratorManager(CryptType type, AbstractLogger logger) { + + if (type == CryptType.AES) { + this.generator = new AESKeyGenerator(logger); + } + if (type == CryptType.BLOWFISH) { + this.generator = new BlowFishGenerator(logger); + } + } + + public String generateKey() { + return generator.generateKey(); + } + +} diff --git a/src/main/java/pl/nightdev701/util/crypto/CryptType.java b/src/main/java/pl/nightdev701/util/CryptType.java similarity index 59% rename from src/main/java/pl/nightdev701/util/crypto/CryptType.java rename to src/main/java/pl/nightdev701/util/CryptType.java index fad6fb5..e62543c 100644 --- a/src/main/java/pl/nightdev701/util/crypto/CryptType.java +++ b/src/main/java/pl/nightdev701/util/CryptType.java @@ -1,4 +1,4 @@ -package pl.nightdev701.util.crypto; +package pl.nightdev701.util; public enum CryptType { diff --git a/src/main/java/pl/nightdev701/util/key/CryptKeyGenerator.java b/src/main/java/pl/nightdev701/util/key/CryptKeyGenerator.java new file mode 100644 index 0000000..df46a05 --- /dev/null +++ b/src/main/java/pl/nightdev701/util/key/CryptKeyGenerator.java @@ -0,0 +1,7 @@ +package pl.nightdev701.util.key; + +public interface CryptKeyGenerator { + + String generateKey(); + +} diff --git a/src/main/java/pl/nightdev701/util/StringHelper.java b/src/main/java/pl/nightdev701/util/string/StringHelper.java similarity index 94% rename from src/main/java/pl/nightdev701/util/StringHelper.java rename to src/main/java/pl/nightdev701/util/string/StringHelper.java index fb74f3c..d348493 100644 --- a/src/main/java/pl/nightdev701/util/StringHelper.java +++ b/src/main/java/pl/nightdev701/util/string/StringHelper.java @@ -1,4 +1,4 @@ -package pl.nightdev701.util; +package pl.nightdev701.util.string; public class StringHelper {