· 5 years ago · Oct 12, 2020, 06:10 PM
1package com.company;
2
3import javax.crypto.*;
4import javax.crypto.spec.SecretKeySpec;
5import java.io.*;
6import java.security.InvalidKeyException;
7import java.security.NoSuchAlgorithmException;
8import java.time.Instant;
9import java.util.Base64;
10
11public class Main {
12
13 public static File filePath;
14 public static File keyPath = new File("key.data");
15 public static File errorLog = new File("logs/error.log");
16 public static File infoLog = new File("logs/info.log");
17
18 public static void encrypt() throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, IOException, BadPaddingException, IllegalBlockSizeException {
19 logger("Encrypting file " + filePath.getPath(), 1);
20 Cipher cipher = Cipher.getInstance("AES");
21 SecretKey secretKey = KeyGenerator.getInstance("AES").generateKey();
22 cipher.init(Cipher.ENCRYPT_MODE, secretKey);
23 writeToFileUsingCipher(readFile(filePath), cipher);
24 String key = Base64.getEncoder().encodeToString(secretKey.getEncoded());
25 writeToFile(key, keyPath, false);
26 logger("Encryption has been finished", 1);
27 }
28
29 public static void decrypt() throws NoSuchPaddingException, NoSuchAlgorithmException, IOException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
30 logger("Decrypting file " + filePath.getPath(), 1);
31 Cipher cipher = Cipher.getInstance("AES");
32 byte[] decodedKey = Base64.getDecoder().decode(readFile(keyPath));
33 SecretKey secretKey = new SecretKeySpec(decodedKey, 0, decodedKey.length, "AES");
34 cipher.init(Cipher.DECRYPT_MODE, secretKey);
35 writeToFileUsingCipher(readFile(filePath), cipher);
36 keyPath.delete();
37 logger("Decryption has been finished", 1);
38 }
39
40 public static void writeToFile(String text, File file, boolean append) throws IOException {
41 FileOutputStream outStream = new FileOutputStream(file, append);
42 outStream.write(text.getBytes());
43 outStream.close();
44 }
45
46 public static void writeToFileUsingCipher(byte[] text, Cipher cipher) throws IOException, BadPaddingException, IllegalBlockSizeException {
47 FileOutputStream outStream = new FileOutputStream(filePath);
48 outStream.write(cipher.doFinal(text));
49 outStream.close();
50 }
51
52 public static byte[] readFile(File file) throws IOException {
53 FileInputStream inStream = new FileInputStream(file);
54 byte[] inBytes = new byte[(int) file.length()];
55 inStream.read(inBytes);
56 inStream.close();
57 return inBytes;
58 }
59
60 public static void logger(String text, int type) throws IOException {
61 text = "[" + Instant.now().toString() + "] - " + text + "\n";
62 if(type == 0) {
63 writeToFile(text, errorLog, true);
64 } else
65 writeToFile(text, infoLog, true);
66 }
67
68
69 public static void main(String[] args) throws IOException {
70 if(args.length < 2) {
71 System.out.print("Argument is missing!\nFirst argument must be keyword \"decrypt\" or \"encrypt\".\nSecond argument is path to the file.\n");
72 System.exit(0);
73 }
74 filePath = new File(args[1]);
75 if(!filePath.isFile()) {
76 System.out.println("The path you passed is not a file.");
77 System.exit(0);
78 }
79 if(args[0].equals("encrypt")) {
80 try {
81 encrypt();
82 System.out.println("File has been successfuly encrypted.");
83 System.out.println("If you want to decrypt your file in the future I strongly recommend you to store your key located in: " + keyPath.getPath());
84 } catch (Exception e) {
85 logger(e.getMessage(), 0);
86 }
87 } else if(args[0].equals("decrypt")) {
88 try {
89 if(!keyPath.isFile()) {
90 logger("Key must be in the root directory.", 0);
91 System.exit(0);
92 }
93 decrypt();
94 System.out.println("File has been successfuly decrypted.");
95 } catch (Exception e) {
96 logger(e.getMessage(), 0);
97 }
98 }
99 }
100}
101