From 4b5b951b7b69a26e2af91bb49f3a61e631a724a5 Mon Sep 17 00:00:00 2001 From: blueShard-dev <63594396+blueShard-dev@users.noreply.github.com> Date: Fri, 26 Jun 2020 09:16:25 +0000 Subject: [PATCH] Update Main.java --- src/org/blueshard/cryptogx/Main.java | 139 +++++++++++++++++++-------- 1 file changed, 98 insertions(+), 41 deletions(-) diff --git a/src/org/blueshard/cryptogx/Main.java b/src/org/blueshard/cryptogx/Main.java index 65a9fe7..bc7408b 100644 --- a/src/org/blueshard/cryptogx/Main.java +++ b/src/org/blueshard/cryptogx/Main.java @@ -1,7 +1,10 @@ -/** - * +/* * @author blueShard - * @version 1.11.0 + * @version 1.12.0 + * + * Some @since versions may be not correct, because the @since tag got added in + * version 1.12.0 and I don't have all versions (1.0.0 - 1.11.0), so I cannot see when some methods were added + */ package org.blueshard.cryptogx; @@ -29,28 +32,30 @@ import java.security.spec.InvalidKeySpecException; import java.util.*; import java.util.concurrent.atomic.AtomicReference; +/** + *

Main class

+ * + * @since 1.0.0 + */ public class Main extends Application { - protected static final int NON_PORTABLE = 414729643; - protected static final int PORTABLE = 245714766; + protected static final int NON_PORTABLE = 1; + protected static final int PORTABLE = 0; - protected static final int TYPE = PORTABLE; + protected static final int TYPE = NON_PORTABLE; - protected final static String configDefaultName = ""; - protected final static String configDefaultEncryptHash = ""; protected final static String configDefaultTextKey = ""; protected final static String configDefaultTextSalt = ""; - protected final static String configDefaultTextAlgorithm = "AES"; + protected final static String configDefaultTextAlgorithm = "AES-128"; protected final static String configDefaultFileEnDecryptKey = ""; protected final static String configDefaultFileEnDecryptSalt = ""; - protected final static String configDefaultFileEnDecryptAlgorithm = "AES"; + protected final static String configDefaultFileEnDecryptAlgorithm = "AES-128"; protected final static int configDefaultFileDeleteIterations = 5; protected final static String configDefaultFileOutputPath = ""; protected final static boolean configDefaultRemoveFileFromFileBox = false; protected final static boolean configDefaultLimitNumberOfThreads = true; - protected static ArrayList textAlgorithms = new ArrayList<>(); - protected static ArrayList fileEnDecryptAlgorithms = new ArrayList<>(); + private final static byte[] buffer = new byte[64]; private static Stage mainStage; private double rootWindowX, rootWindowY; @@ -62,6 +67,8 @@ public class Main extends Application { * * @param primaryStage of the GUI * @throws IOException if issues with loading 'mainGUI.fxml' + * + * @since 1.0.0 */ @Override public void start(Stage primaryStage) throws IOException { @@ -74,7 +81,8 @@ public class Main extends Application { primaryStage.setResizable(false); primaryStage.setTitle("cryptoGX"); primaryStage.getIcons().add(new Image(getClass().getResource("resources/cryptoGX.png").toExternalForm())); - Scene scene = new Scene(root, 900, 470); + Scene scene = new Scene(root); + //Scene scene = new Scene(root, 900, 470); scene.setOnMouseDragged(event -> { primaryStage.setX(event.getScreenX() + rootWindowX); @@ -94,7 +102,7 @@ public class Main extends Application { * Can also be used to en- / decrypt text and files or secure delete files without starting GUI

* * @param args from the command line - * @return status + * @return * @throws BadPaddingException * @throws NoSuchAlgorithmException if wrong algorithm is given (command line) * @throws IllegalBlockSizeException if wrong size for key is given (command line) @@ -103,11 +111,24 @@ public class Main extends Application { * @throws InvalidKeySpecException * @throws IOException if files cannot be en- / decrypted or deleted correctly (command line) * @throws InvalidAlgorithmParameterException if wrong algorithm parameters are given (command line) + * + * @since 1.0.0 */ public static void main(String[] args) throws BadPaddingException, NoSuchAlgorithmException, IllegalBlockSizeException, NoSuchPaddingException, InvalidKeyException, InvalidKeySpecException, IOException, InvalidAlgorithmParameterException { - if (Main.TYPE == Main.NON_PORTABLE) { - if (System.getProperty("os.name").toLowerCase().startsWith("windows")) { + if (Main.TYPE == Main.PORTABLE) { + String system = System.getProperty("os.name").toLowerCase(); + if (system.startsWith("windows")) { config = new File("C:\\Users\\" + System.getProperty("user.name") + "\\AppData\\Roaming\\cryptoGX\\cryptoGX.config"); + File directory = new File("C:\\Users\\" + System.getProperty("user.name") + "\\AppData\\Roaming\\cryptoGX"); + if (!directory.isDirectory()) { + directory.mkdir(); + } + } else if (system.startsWith("linux")) { + config = new File(System.getProperty("user.home") + "/.cryptoGX/cryptoGX.config"); + File directory = new File(System.getProperty("user.home") + "/.cryptoGX/"); + if (!directory.isDirectory()) { + directory.mkdir(); + } } else { config = new File("cryptoGX.config"); } @@ -116,19 +137,6 @@ public class Main extends Application { } isConfig = config.isFile(); if (args.length == 0) { - String version = Runtime.class.getPackage().getImplementationVersion(); - if (version.startsWith("1.")) { - if (Integer.parseInt(version.substring(2, 3)) < 8) { - System.out.println("1"); - JOptionPane.showMessageDialog(null, "Please use java 1.8.0_240 to java 10.*", "ERROR", JOptionPane.ERROR_MESSAGE); - } else if (Integer.parseInt(version.substring(6, 9)) < 240) { - JOptionPane.showMessageDialog(null, "Please use java 1.8.0_240 to java 10.*", "ERROR", JOptionPane.ERROR_MESSAGE); - } - } else if (Integer.parseInt(version.substring(0, 2)) > 10) { - JOptionPane.showMessageDialog(null, "Please use java 1.8.0_240 to java 10.*", "ERROR", JOptionPane.ERROR_MESSAGE); - } else { - JOptionPane.showMessageDialog(null, "Please use java 1.8.0_240 to java 10.*", "ERROR", JOptionPane.ERROR_MESSAGE); - } launch(args); } else { args[0] = args[0].replace("-", ""); @@ -140,7 +148,7 @@ public class Main extends Application { " File en- / decryption\n" + " encrypt: AES encrypt \n" + " decrypt: AES decrypt \n\n" + - "File secure delete: "); + "File secure delete: delete "); //for the argument 'default' can be used, which is 5 } else if (args[0].toLowerCase().equals("delete")) { if (args.length > 3) { System.err.println("To many arguments were given, expected 3"); @@ -148,14 +156,28 @@ public class Main extends Application { System.err.println("To few arguments were given, expected 3"); } try { - SecureDelete.deleteFileLineByLine(args[2], Integer.parseInt(args[1])); + if (args[1].equals("default")) { + args[1] = "5"; + } + File deleteFile = new File(args[2]); + if (deleteFile.isFile()) { + SecureDelete.deleteFile(deleteFile, Integer.parseInt(args[1]), buffer); + } else if (deleteFile.isDirectory()) { + SecureDelete.deleteDirectory(args[2], Integer.parseInt(args[1]), buffer); + } else { + System.err.println("Couldn't find file " + args[4]); + System.exit(1); + } } catch (NumberFormatException e) { System.err.println(args[1] + " must be a number\n Error: " + e.getMessage()); } } else if (args[0].toLowerCase().equals("aes")) { - if (args.length < 4) { + if (args.length < 5) { System.err.println("To few arguments were given"); System.exit(1); + } else if (args.length > 6) { + System.err.println("To many arguments were given"); + System.exit(1); } EnDecrypt.AES aes; if (args[2].isEmpty()) { @@ -164,18 +186,44 @@ public class Main extends Application { aes = new EnDecrypt.AES(args[1], args[2].getBytes(StandardCharsets.UTF_8)); } String type = args[3].toLowerCase(); - if (type.equals("encrypt")) { - System.out.println(aes.encrypt(args[4])); - } else if (type.equals("decrypt")) { - System.out.println(aes.decrypt(args[4])); - } else if (type.equals("fileencrypt") || type.equals("encryptfile")) { - aes.encryptFileLineByLine(args[4], args[5]); - } else if (type.equals("filedecrypt") ||type.equals("decryptfile")) { - aes.decryptFileLineByLine(args[4], args[5]); + if (args.length == 5) { + if (type.equals("encrypt")) { + System.out.println(Base64.getEncoder().encodeToString(aes.encrypt(args[4].getBytes(StandardCharsets.UTF_8)))); + } else if (type.equals("decrypt")) { + System.out.println(aes.decrypt(args[4])); + } else { + System.err.println("Couldn't resolve argument " + args[3] + ", expected 'encrypt' or 'decrypt'"); + System.exit(1); + } + } else { + if (type.equals("encrypt")) { + File inputFile = new File(args[4]); + if (inputFile.isFile()) { + aes.encryptFile(new FileInputStream(inputFile), new FileOutputStream(args[5]), new byte[64]); + } else if (inputFile.isDirectory()) { + aes.encryptDirectory(args[4], args[5], ".cryptoGX", new byte[64]); + } else { + System.err.println("Couldn't find file " + args[4]); + System.exit(1); + } + } else if (type.equals("decrypt")) { + File inputFile = new File(args[4]); + if (inputFile.isFile()) { + aes.decryptFile(new FileInputStream(inputFile), new FileOutputStream(args[5]), new byte[64]); + } else if (inputFile.isDirectory()) { + aes.decryptDirectory(args[4], args[5], "@.cryptoGX@", new byte[64]); + } else { + System.err.println("Couldn't find file " + args[4]); + System.exit(1); + } + } else { + System.err.println("Couldn't resolve argument " + args[3] + ", expected 'encrypt' or 'decrypt'"); + System.exit(1); + } } + System.exit(0); } } - System.exit(0); } /** @@ -183,6 +231,8 @@ public class Main extends Application { * * @param thread which called this method * @param throwable of the thread which called the method + * + * @since 1.3.0 */ private static void exceptionAlert(Thread thread, Throwable throwable) { throwable.printStackTrace(); @@ -193,7 +243,9 @@ public class Main extends Application { Alert enDecryptError = new Alert(Alert.AlertType.ERROR, "Error: " + throwable, ButtonType.OK); enDecryptError.initStyle(StageStyle.UNDECORATED); enDecryptError.setTitle("Error"); + enDecryptError.setResizable(true); ((Stage) enDecryptError.getDialogPane().getScene().getWindow()).getIcons().add(new Image(Main.class.getResource("resources/cryptoGX.png").toExternalForm())); + enDecryptError.getDialogPane().setContent(new Label("Error: " + throwable)); Scene window = enDecryptError.getDialogPane().getScene(); @@ -223,7 +275,9 @@ public class Main extends Application { "\nError: " + error, ButtonType.OK); enDecryptError.initStyle(StageStyle.UNDECORATED); enDecryptError.setTitle("Error"); + enDecryptError.setResizable(true); ((Stage) enDecryptError.getDialogPane().getScene().getWindow()).getIcons().add(new Image(Main.class.getResource("resources/cryptoGX.png").toExternalForm())); + enDecryptError.getDialogPane().setContent(new Label(message)); Scene window = enDecryptError.getDialogPane().getScene(); @@ -243,6 +297,8 @@ public class Main extends Application { *

Shows an warning alert window

* * @param message that the alert window will show + * + * @since 1.4.0 */ protected static void warningAlert(String message) { AtomicReference alertX = new AtomicReference<>(Screen.getPrimary().getBounds().getMaxX() / 2); @@ -252,6 +308,7 @@ public class Main extends Application { enDecryptError.initStyle(StageStyle.UNDECORATED); enDecryptError.setTitle("Error"); ((Stage) enDecryptError.getDialogPane().getScene().getWindow()).getIcons().add(new Image(Main.class.getResource("resources/cryptoGX.png").toExternalForm())); + enDecryptError.getDialogPane().setContent(new Label(message)); Scene window = enDecryptError.getDialogPane().getScene();