-
Notifications
You must be signed in to change notification settings - Fork 79
Expand file tree
/
Copy pathAESTest.java
More file actions
49 lines (45 loc) · 1.44 KB
/
Copy pathAESTest.java
File metadata and controls
49 lines (45 loc) · 1.44 KB
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
package aes;
import java.io.*;
import java.security.*;
import javax.crypto.*;
/**
* This program tests the AES cipher. Usage:<br>
* java aes.AESTest -genkey keyfile<br>
* java aes.AESTest -encrypt plaintext encrypted keyfile<br>
* java aes.AESTest -decrypt encrypted decrypted keyfile<br>
* @author Cay Horstmann
* @version 1.01 2012-06-10
*/
public class AESTest
{
public static void main(String[] args)
throws IOException, GeneralSecurityException, ClassNotFoundException
{
if (args[0].equals("-genkey"))
{
KeyGenerator keygen = KeyGenerator.getInstance("AES");
SecureRandom random = new SecureRandom();
keygen.init(random);
SecretKey key = keygen.generateKey();
try (ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(args[1])))
{
out.writeObject(key);
}
}
else
{
int mode;
if (args[0].equals("-encrypt")) mode = Cipher.ENCRYPT_MODE;
else mode = Cipher.DECRYPT_MODE;
try (ObjectInputStream keyIn = new ObjectInputStream(new FileInputStream(args[3]));
InputStream in = new FileInputStream(args[1]);
OutputStream out = new FileOutputStream(args[2]))
{
Key key = (Key) keyIn.readObject();
Cipher cipher = Cipher.getInstance("AES");
cipher.init(mode, key);
Util.crypt(in, out, cipher);
}
}
}
}