1 1 import static java.
lang.
System.
out;
2 +import static java.
nio.
file.
StandardOpenOption.
CREATE_NEW;
3 +import static java.
nio.
file.
StandardOpenOption.
READ;
4 +import static java.
nio.
file.
StandardOpenOption.
WRITE;
3 6 import java.
nio.
file.
*;
4 7 import java.
nio.
charset.
*;
9 11 import java.
security.
*;
10 -import java.
security.
spec.
*;
12 13 import javax.
crypto.
*;
13 14 import javax.
crypto.
spec.
*;
16 - enum Command {
ENCRYPT,
DECRYPT };
17 21 static Cipher AES_GCM_PKCS5;
18 - static byte[] SALT = new byte[] {
116,
104,
101,
32,
109,
111,
111,
110,
32,
105,
115,
32,
104,
105,
100,
100,
101,
110,
32,
105,
110,
32,
112,
108,
97,
105,
110,
32,
118,
105,
101,
119 };
22 + static byte[] SALT = new byte[] {
116,
104,
101,
32,
109,
111,
111,
110,
32,
105,
115,
32,
104,
105,
100,
100,
101,
23 + 110,
32,
105,
110,
32,
112,
108,
97,
105,
110,
32,
118,
105,
101,
119 };
19 24 static SecureRandom RANDOM = new SecureRandom();
23 28 static int GCM_IV_SIZE = 12;
27 33 static int READ_BUFFER_SIZE = 128;
32 39 static int GCM_AUTH_TAG_SIZE = 96;
34 41 public static void main(
String[] arguments)
throws Throwable {
35 - if(arguments.length
== 0){
42 + if (arguments.length
== 0) {
40 47 var input_command
= arguments[
0];
42 - if (
"encrypt".startsWith(input_command)) { command
= Command.
ENCRYPT; }
43 - else if (
"decrypt".startsWith(input_command)) { command
= Command.
DECRYPT; }
44 - else {
throw fail(
"unknown command %s given%n", emphasize_global(input_command)); }
49 + if (
"encrypt".startsWith(input_command)) {
50 + command
= Command.
ENCRYPT;
51 + }
else if (
"decrypt".startsWith(input_command)) {
52 + command
= Command.
DECRYPT;
54 + throw fail(
"unknown command %s given%n", emphasize_global(input_command));
46 57 AES_GCM_PKCS5 = Cipher.getInstance(
"AES/GCM/NoPadding");
48 59 var console
= System.console();
53 - if(console
== null){
54 - if(arguments.length
!= 3 || !"--password-file".equals(arguments[
1])) {
65 + if (console
== null) {
66 + if (arguments.length
!= 3 || !"--password-file".equals(arguments[
1])) {
56 - throw fail(
"When running %s non-interactively, the %s flag is required.%nOtherwise there is no way to securely obtain the password.%n",emphasize_local(
"aes"), emphasize_global(
"--password-file"));
69 + "When running %s non-interactively, the %s flag is required.%nOtherwise there is no way to securely obtain the password.%n",
70 + emphasize_local(
"aes"), emphasize_global(
"--password-file"));
58 72 var password_file
= Path.of(arguments[
2]);
59 - if(
!Files.exists(password_file)) {
throw fail(
"the given password file %s does not exist%n", emphasize_local(password_file.toString())); }
73 + if (
!Files.exists(password_file)) {
74 + throw fail(
"the given password file %s does not exist%n", emphasize_local(password_file.toString()));
60 76 var password
= toChars(
Files.readAllBytes(password_file));
63 79 encrypt(password,
System.in,
System.out);
66 82 decrypt(password,
System.in,
System.out);
71 - if(arguments.length
<= 1) {
86 + if (arguments.length
<= 1) {
73 88 throw fail(
"not enough arguments given%n");
78 - if(
"--password-file".equals(arguments[
1])) {
79 - if(arguments.length
!= 5) {
93 + if (
"--password-file".equals(arguments[
1])) {
94 + if (arguments.length
!= 5) {
81 96 throw fail(
"the flag %s requires a path%n", emphasize_local(
"--password-file"));
83 98 var password_file
= Path.of(arguments[
2]);
84 - if(
!Files.exists(password_file)) {
throw fail(
"the given password file %s does not exist%n", emphasize_local(password_file.toString())); }
99 + if (
!Files.exists(password_file)) {
100 + throw fail(
"the given password file %s does not exist%n", emphasize_local(password_file.toString()));
85 102 password
= toChars(
Files.readAllBytes(password_file));
86 103 input
= arguments[
3];
87 104 output
= arguments[
4];
90 - if(arguments.length
!= 3) {
106 + if (arguments.length
!= 3) {
92 108 throw fail(
"when encrypting/decrypting a file, an output path is required%n");
94 - password
= console.readPassword(
"[%s]:",emphasize_global(
"Password"));
110 + password
= console.readPassword(
"[%s]:", emphasize_global(
"Password"));
95 111 input
= arguments[
1];
96 112 output
= arguments[
2];
99 115 var input_path
= Path.of(input);
100 - if(
!Files.exists(input_path)) {
throw fail(
"the given input path %s does not exist%n", emphasize_global(input_path.toString())); }
116 + if (
!Files.exists(input_path)) {
117 + throw fail(
"the given input path %s does not exist%n", emphasize_global(input_path.toString()));
101 119 var output_path
= Path.of(output);
102 - if(
Files.exists(output_path)) {
throw fail(
"the output path %s already exists%n", emphasize_global(output_path.toString())); }
120 + if (
Files.exists(output_path)) {
121 + throw fail(
"the output path %s already exists%n", emphasize_global(output_path.toString()));
104 - var input_stream
= Files.newInputStream(input_path,
StandardOpenOption.
READ);
105 - var output_stream
= Files.newOutputStream(output_path,
StandardOpenOption.
CREATE_NEW,
StandardOpenOption.
WRITE);
109 - encrypt(password, input_stream, output_stream);
112 - decrypt(password, input_stream, output_stream);
124 + try (
var input_stream
= Files.newInputStream(input_path,
READ);
125 + var output_stream
= Files.newOutputStream(output_path,
CREATE_NEW,
WRITE);) {
128 + encrypt(password, input_stream, output_stream);
131 + decrypt(password, input_stream, output_stream);
122 142 var initialization_vector
= new byte[
GCM_IV_SIZE];
123 143 RANDOM.nextBytes(initialization_vector);
125 146 output.write(initialization_vector);
126 147 var secret
= toSecretKey(password);
127 148 var parameter_spec
= new GCMParameterSpec(
GCM_AUTH_TAG_SIZE, initialization_vector);
130 151 var read_buffer
= new byte[
READ_BUFFER_SIZE];
131 152 var aes_buffer
= new byte[
READ_BUFFER_SIZE];
132 153 int read_bytes_count
= 0;
133 - while((read_bytes_count
= input.read(read_buffer))
!= -1){
134 - var aes_bytes_count
= AES_GCM_PKCS5.update(read_buffer,
0, read_bytes_count, aes_buffer);
135 - output.write(aes_buffer,
0, aes_bytes_count);
154 + while ((read_bytes_count
= input.read(read_buffer))
!= -1) {
155 + var aes_bytes_count
= AES_GCM_PKCS5.update(read_buffer,
0, read_bytes_count, aes_buffer);
156 + output.write(aes_buffer,
0, aes_bytes_count);
137 158 output.write(
AES_GCM_PKCS5.doFinal());
140 161 static void decrypt(
char[] password,
InputStream input,
OutputStream output) throws
Exception {
142 163 var initialization_vector
= new byte[
GCM_IV_SIZE];
143 164 input.read(initialization_vector);
144 165 var parameter_spec
= new GCMParameterSpec(
GCM_AUTH_TAG_SIZE, initialization_vector);
148 169 var read_buffer
= new byte[
READ_BUFFER_SIZE];
149 170 var aes_buffer
= new byte[
READ_BUFFER_SIZE];
150 171 int read_bytes_count
= 0;
151 - while((read_bytes_count
= input.read(read_buffer))
!= -1){
152 - var aes_bytes_count
= AES_GCM_PKCS5.update(read_buffer,
0, read_bytes_count, aes_buffer);
153 - output.write(aes_buffer,
0, aes_bytes_count);
172 + while ((read_bytes_count
= input.read(read_buffer))
!= -1) {
173 + var aes_bytes_count
= AES_GCM_PKCS5.update(read_buffer,
0, read_bytes_count, aes_buffer);
174 + output.write(aes_buffer,
0, aes_bytes_count);
160 182 output.write(
AES_GCM_PKCS5.doFinal());
164 187 static SecretKey toSecretKey(
char[] password) throws
Exception {
165 188 var factory
= SecretKeyFactory.getInstance(
"PBKDF2WithHmacSHA256");
166 189 var spec
= new PBEKeySpec(password,
SALT,
65536,
256);
167 - return new SecretKeySpec(factory.generateSecret(spec).getEncoded() ,
"AES");
190 + return new SecretKeySpec(factory.generateSecret(spec).getEncoded(),
"AES");
170 193 static char[] toChars(
byte[] bytes) {
176 199 static void usage() {
177 200 out.printf(
"%s encrypts/decrypts data.%n", emphasize_local(
"Symmetrically "));
178 - out.printf(
"%s: java aes.java <e(ncrypt)|d(ecrypt)> [--password-file <path>] <input path> <output path>%n", emphasize_global(
"Usage for files"));
179 - out.printf(
"%s: java aes.java <e(ncrypt)|d(ecrypt)> --password-file <path>%n", emphasize_global(
"Usage for stdin/stdout"));
201 + out.printf(
"%s: java aes.java <e(ncrypt)|d(ecrypt)> [--password-file <path>] <input path> <output path>%n",
202 + emphasize_global(
"Usage for files"));
203 + out.printf(
"%s: java aes.java <e(ncrypt)|d(ecrypt)> --password-file <path>%n",
204 + emphasize_global(
"Usage for stdin/stdout"));
182 207 static Throwable exit() {
183 208 out.printf(
"%s!%n", emphasize_local(
"Done"));
186 - return new Throwable();
211 + return new Throwable();
189 214 static String emphasize_global(
String message) {
197 222 static Throwable fail(
String message,
Object... arguments) {
198 223 out.printf(message, arguments);
201 - return new Throwable();
226 + return new Throwable();
204 229 static Throwable fail() {
207 - return new Throwable();
232 + return new Throwable();