· 7 years ago · Dec 01, 2018, 06:58 PM
1package apk;
2
3import java.io.FileInputStream;
4import java.io.FileNotFoundException;
5import java.io.FileOutputStream;
6import java.io.IOException;
7import java.io.InputStream;
8import java.io.ObjectInputStream;
9import java.io.ObjectOutputStream;
10import java.io.OutputStream;
11import java.security.GeneralSecurityException;
12import java.security.Key;
13import java.security.NoSuchAlgorithmException;
14import java.security.SecureRandom;
15
16import javax.crypto.Cipher;
17import javax.crypto.KeyGenerator;
18import javax.crypto.SecretKey;
19
20import aes.Util;
21
22public class AES {
23
24 public void generateKey() throws IOException, NoSuchAlgorithmException{
25 KeyGenerator keygen = KeyGenerator.getInstance("AES");
26 SecureRandom random = new SecureRandom();
27 keygen.init(random);
28 SecretKey key = keygen.generateKey();
29 try (ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("key_file")))
30 {
31 out.writeObject(key);
32 }
33 }
34
35 public synchronized void encryptFile() throws FileNotFoundException, IOException, ClassNotFoundException, GeneralSecurityException{
36 int mode = Cipher.ENCRYPT_MODE;
37
38 try (ObjectInputStream keyIn = new ObjectInputStream(new FileInputStream("key_file"));
39 InputStream in = new FileInputStream("input2.txt");
40 OutputStream out = new FileOutputStream("encrypted_file.txt");)
41 {
42 Key key = (Key) keyIn.readObject();
43 Cipher cipher = Cipher.getInstance("AES");
44 cipher.init(mode, key);
45 Util.crypt(in, out, cipher);
46 }
47 }
48
49 public synchronized void decryptFile() throws FileNotFoundException, IOException, ClassNotFoundException, GeneralSecurityException{
50 int mode = Cipher.DECRYPT_MODE;
51
52 try (ObjectInputStream keyIn = getKey();
53 InputStream in = new FileInputStream("encrypted_file.txt");
54 OutputStream out = new FileOutputStream("decrypted_file.txt"))
55 {
56 Key key = (Key) keyIn.readObject();
57 Cipher cipher = Cipher.getInstance("AES");
58 cipher.init(mode, key);
59 Util.crypt(in, out, cipher);
60 }
61 }
62
63 public synchronized ObjectInputStream getKey() throws FileNotFoundException, IOException{
64 return new ObjectInputStream(new FileInputStream("key_file"));
65 }
66}
67
68
69-----------------------------
70package apk;
71
72import java.io.IOException;
73import java.security.GeneralSecurityException;
74import java.security.NoSuchAlgorithmException;
75
76public class AEStest {
77
78 public static void main(String[] args) throws NoSuchAlgorithmException, IOException {
79 test1();
80 }
81
82 public static void test1(){
83 AES aes = new AES();
84
85 try {
86 aes.generateKey();
87 } catch (NoSuchAlgorithmException | IOException e1) {
88 e1.printStackTrace();
89 }
90
91 for (int i = 0; i < 2; i++) {
92 Runnable r = () -> {
93 try {
94 aes.encryptFile();
95 } catch (ClassNotFoundException | IOException | GeneralSecurityException e) {
96 System.out.println(e);
97 }
98 };
99
100 Runnable rr = () -> {
101 try {
102 aes.decryptFile();
103 } catch (ClassNotFoundException | IOException | GeneralSecurityException e) {
104 System.out.println(e);
105 }
106 };
107
108 Thread t = new Thread(r);
109 Thread tt = new Thread(rr);
110
111 t.start();
112 tt.start();
113 }
114 }
115}