-
Notifications
You must be signed in to change notification settings - Fork 0
/
Crypto.java
86 lines (83 loc) · 3.77 KB
/
Crypto.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.util.Scanner;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;
/**
* @author Felix Dumbeck
* @see github.com/f-eliks
*/
public class Crypto {
private byte[] readFile(String fileName) throws IOException {
File file = new File(fileName);
return Files.readAllBytes(file.toPath());
}
private byte[] encrypt(byte[] arr, String password) throws NoSuchAlgorithmException, NoSuchPaddingException,
InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
Key key = generateKey(password);
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, key);
return cipher.doFinal(arr);
}
private Key generateKey(String password) {
String key = password;
while (key.length() < 32)
key += key;
key = key.substring(0, 32);
return new SecretKeySpec(key.getBytes(Charset.forName("UTF-8")), "AES");
}
private void storeFile(byte[] arr, String fileName) throws IOException {
InputStream is = new ByteArrayInputStream(arr);
FileOutputStream fos = new FileOutputStream(fileName);
byte[] buffer = new byte[1024];
int length;
while ((length = is.read(buffer)) > 0)
fos.write(buffer, 0, length);
is.close();
fos.close();
}
private byte[] decrypt(byte[] arr, String password) throws InvalidKeyException, IllegalBlockSizeException,BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException {
Key key = generateKey(password);
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, key);
return cipher.doFinal(arr);
}
private void encryptFile(String fileName, String password) throws InvalidKeyException, NoSuchAlgorithmException,NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, IOException {
storeFile(encrypt(readFile(fileName), password), fileName+".crypt");
}
private void decryptFile(String fileName, String password) throws InvalidKeyException, IllegalBlockSizeException,
BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException, IOException {
storeFile(decrypt(readFile(fileName), password), (fileName.split(".crypt"))[0]);
}
public static void main(String[] args) throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException,
IllegalBlockSizeException, BadPaddingException, IOException {
Crypto crypto = new Crypto();
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("Would you like to encrypt (e) or decrypt (d) a file?");
String input = scanner.nextLine();
if (input.equals("e")) {
System.out.println("filename: ");
String fileName = scanner.nextLine();
System.out.println("password");
crypto.encryptFile(fileName, scanner.nextLine());
} else if (input.equals("d")) {
System.out.println("filename: ");
String fileName = scanner.nextLine();
System.out.println("password");
crypto.decryptFile(fileName, scanner.nextLine());
}
}
}
}