diff --git a/.idea/misc.xml b/.idea/misc.xml index a165cb3..0c4841a 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,6 +1,6 @@ - + \ No newline at end of file diff --git a/README.md b/README.md index 10b8dd8..9eff799 100644 --- a/README.md +++ b/README.md @@ -2,4 +2,10 @@ Project: Encryption-Decryption -Go to the *src* directory, change *Crypto.java* and put your *.java* file(s) in it. +Added two methods for encryption/description + +Command line support added + +Work with files added + +New aes algorithm added (13.01.19) \ No newline at end of file diff --git a/protected.txt b/protected.txt new file mode 100644 index 0000000..7ac36d3 --- /dev/null +++ b/protected.txt @@ -0,0 +1 @@ +637E771EB77D5BA14AF2E60452B99CCF88D408880DE930CF81F2E7CD57F6C601 \ No newline at end of file diff --git a/road_to_treasure.txt b/road_to_treasure.txt new file mode 100644 index 0000000..2e056e2 --- /dev/null +++ b/road_to_treasure.txt @@ -0,0 +1 @@ +Welcome to hyperskill! \ No newline at end of file diff --git a/src/crypto/AesCryptoAlgorithm.java b/src/crypto/AesCryptoAlgorithm.java new file mode 100644 index 0000000..9705fc2 --- /dev/null +++ b/src/crypto/AesCryptoAlgorithm.java @@ -0,0 +1,59 @@ +package crypto;import com.sun.org.apache.xerces.internal.impl.dv.util.HexBin; + +import javax.crypto.Cipher; +import javax.crypto.spec.IvParameterSpec; +import javax.crypto.spec.SecretKeySpec; + +public class AesCryptoAlgorithm implements CryptoAlgorithm { + + private static final String SECRET_KEY = "JustSecretString"; + + @Override + public String encryption(String message, String key) { + String encryptedMessage = ""; + try { + if (!isValidKey(key)) { + System.out.println("Error: For the aes algorithm, key length must be 16, 24 or 32 symbols(bytes)"); + } + + IvParameterSpec ivParameter = new IvParameterSpec(SECRET_KEY.getBytes("UTF-8")); + SecretKeySpec secretKey = new SecretKeySpec(key.getBytes("UTF-8"), "AES"); + + Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); + cipher.init(Cipher.ENCRYPT_MODE, secretKey, ivParameter); + + byte[] bytes = cipher.doFinal(message.getBytes("UTF-8")); + encryptedMessage = HexBin.encode(bytes); + } catch (Throwable cause) { + System.out.println("Error: " + cause.getMessage()); + } + return encryptedMessage; + } + + @Override + public String decryption(String encryptedMessage, String key) { + String decryptedMessage = ""; + try { + if (!isValidKey(key)) { + System.out.println("Error: For the aes algorithm, key length must be 16, 24 or 32 symbols(bytes)"); + } + + IvParameterSpec ivParameter = new IvParameterSpec(SECRET_KEY.getBytes("UTF-8")); + SecretKeySpec secretKey = new SecretKeySpec(key.getBytes("UTF-8"), "AES"); + + Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); + cipher.init(Cipher.DECRYPT_MODE, secretKey, ivParameter); + + byte[] bytes = cipher.doFinal(HexBin.decode(encryptedMessage)); + decryptedMessage = new String(bytes); + } catch (Throwable cause) { + System.out.println("Error: " + cause.getMessage()); + cause.printStackTrace(); + } + return decryptedMessage; + } + + private static boolean isValidKey(String key) { + return key.length() == 16 || key.length() == 24 || key.length() == 32; + } +} diff --git a/src/crypto/Crypto.java b/src/crypto/Crypto.java index 51d57b7..8ebcfd8 100644 --- a/src/crypto/Crypto.java +++ b/src/crypto/Crypto.java @@ -1,10 +1,98 @@ package crypto; +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; import java.util.Scanner; public class Crypto { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); - // write your code here + String state = "enc"; + String key = ""; + String algName = ""; + String message = ""; + String result = ""; + String in = ""; + String out = ""; + + for (int i = 0; i < args.length; i++) { + if ("-mode".equals(args[i])) { + state = args[i + 1]; + i++; + } else if ("-key".equals(args[i])) { + key = args[i + 1]; + i++; + }else if ("-alg".equals(args[i])) { + algName = args[i + 1]; + i++; + } else if ("-data".equals(args[i])) { + message = args[i + 1]; + i++; + } else if ("-in".equals(args[i])) { + in = args[i + 1]; + i++; + } else if ("-out".equals(args[i])) { + out = args[i + 1]; + i++; + } + } + + CryptoAlgorithm cryptoAlgorithm = CryptoAlgorithmFactory.getAlgorithm(algName); + + if (key.equals("")) { + System.out.print("Enter key: "); + key = scanner.nextLine(); + } + + if (!in.equals("")) { + File input = new File(in); + if (input.exists()) { + try { + message = new String(Files.readAllBytes(Paths.get(input.getPath()))); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + + if (message.equals("")) { + System.out.print("Enter text: "); + message = scanner.nextLine(); + } + + if ("enc".equals(state)) { + result = cryptoAlgorithm.encryption(message, key); + } + + if ("dec".equals(state)) { + result = cryptoAlgorithm.decryption(message, key); + } + + if (out.equals("")) { + System.out.println(result); + } else { + File output = new File(out); + boolean created = true; + if (!output.exists()) { + try { + created = output.createNewFile(); + } catch (IOException e) { + created = false; + System.out.println("Cannot create the file: " + output.getPath()); + } + } + if (created) { + try { + FileWriter writer = new FileWriter(output); + writer.write(result); + writer.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } } } diff --git a/src/crypto/CryptoAlgorithm.java b/src/crypto/CryptoAlgorithm.java new file mode 100644 index 0000000..8937b4e --- /dev/null +++ b/src/crypto/CryptoAlgorithm.java @@ -0,0 +1,4 @@ +package crypto;public interface CryptoAlgorithm { + String encryption(String message, String key); + String decryption(String encryptedMessage, String key); +} diff --git a/src/crypto/CryptoAlgorithmFactory.java b/src/crypto/CryptoAlgorithmFactory.java new file mode 100644 index 0000000..a543449 --- /dev/null +++ b/src/crypto/CryptoAlgorithmFactory.java @@ -0,0 +1,9 @@ +package crypto;public class CryptoAlgorithmFactory { + public static CryptoAlgorithm getAlgorithm(String algName) { + if (algName.equals("aes")) { + return new AesCryptoAlgorithm(); + } else { + return new SimpleCryptoAlgorithm(); + } + } +} diff --git a/src/crypto/SimpleCryptoAlgorithm.java b/src/crypto/SimpleCryptoAlgorithm.java new file mode 100644 index 0000000..17cf48b --- /dev/null +++ b/src/crypto/SimpleCryptoAlgorithm.java @@ -0,0 +1,47 @@ +package crypto;public class SimpleCryptoAlgorithm implements CryptoAlgorithm { + + @Override + public String encryption(String message, String key) { + String encrypt = ""; + int secretKey = 0; + try { + secretKey = Integer.parseInt(key); + } catch (NumberFormatException ex) { + System.out.println("Error: For the simple algorithm, the key must be a number"); + } + for (int i = 0; i < message.length(); i++) { + int index = message.charAt(i); + + if (index != -1) { + int newIndex = index + secretKey; + char c = (char) newIndex; + encrypt += c; + } else { + encrypt += String.valueOf(index);; + } + } + return encrypt; } + + @Override + public String decryption(String encryptedMessage, String key) { + String encrypt = ""; + int secretKey = 0; + try { + secretKey = Integer.parseInt(key); + } catch (NumberFormatException ex) { + System.out.println("Error: For the simple algorithm, the key must be a number"); + } + for (int i = 0; i < encryptedMessage.length(); i++) { + int index = encryptedMessage.charAt(i); + + if (index != -1) { + int newIndex = index - secretKey; + char c = (char) newIndex; + encrypt += c; + } else { + encrypt += String.valueOf(index);; + } + } + return encrypt; + } +}