· 7 years ago · Apr 23, 2018, 06:14 AM
1package TestParallel;
2
3import java.io.File;
4import java.io.FileInputStream;
5import java.io.FileOutputStream;
6import java.io.ObjectOutputStream;
7import java.util.concurrent.ExecutorService;
8import java.util.concurrent.Executors;
9import java.util.concurrent.TimeUnit;
10import java.util.logging.Level;
11import java.util.logging.Logger;
12import javax.crypto.Cipher;
13import javax.crypto.CipherOutputStream;
14import javax.crypto.KeyGenerator;
15import javax.crypto.SecretKey;
16
17/**
18 *
19 * @author Sohel Rana
20 */
21public class Executor {
22
23 public void encrypt(File fname) throws Exception {
24 KeyGenerator keyGen = KeyGenerator.getInstance("AES");
25 keyGen.init(256); //using AES-256
26 SecretKey key = keyGen.generateKey(); //generating key
27 // System.out.println("Key = " + bytesToHex(key.getEncoded()));
28 Cipher aesCipher = Cipher.getInstance("AES"); //getting cipher for AES
29 aesCipher.init(Cipher.ENCRYPT_MODE, key); //initializing cipher for encryption with key
30
31 //creating file output stream to write to file
32 try (FileOutputStream fos = new FileOutputStream(fname + ".aes")) {
33 //creating object output stream to write objects to file
34 ObjectOutputStream oos = new ObjectOutputStream(fos);
35 oos.writeObject(key); //saving key to file for use during decryption
36
37 //creating file input stream to read contents for encryption
38 try (FileInputStream fis = new FileInputStream(fname)) {
39 //creating cipher output stream to write encrypted contents
40 try (CipherOutputStream cos = new CipherOutputStream(fos, aesCipher)) {
41 int read;
42 byte buf[] = new byte[4096];
43 while ((read = fis.read(buf)) != -1) //reading from file
44 {
45 cos.write(buf, 0, read); //encrypting and writing to file
46 }
47 }
48 }
49 // fname.delete();
50 }
51
52 }
53
54 public static void main(final String[] args) throws InterruptedException {
55 final ExecutorService pool = Executors.newFixedThreadPool(4);
56
57 File file1 = new File("C:\Users\Sohel Rana\Desktop\test\33 - Overflow Menu.mp4");
58 File file2 = new File("C:\Users\Sohel Rana\Desktop\test\Java Cryptography Tutorials 1 AES Encryption and Decryption using Java.mp4");
59 File file3 = new File("C:\Users\Sohel Rana\Desktop\test\30 - Dank Meme Bro.mp4");
60 File file4 = new File("C:\Users\Sohel Rana\Desktop\test\How to change API level Android Studio.mp4");
61
62 Executor ex = new Executor();
63 long startTime = System.currentTimeMillis();
64 pool.execute(() -> {
65 try {
66 ex.encrypt(file1);
67 ex.encrypt(file2);
68 ex.encrypt(file3);
69 ex.encrypt(file4);
70 } catch (Exception ex1) {
71 Logger.getLogger(Executor.class.getName()).log(Level.SEVERE, null, ex1);
72 }
73 });
74
75 pool.shutdown();
76
77 if (!pool.awaitTermination(1, TimeUnit.DAYS)) {
78 System.err.println("Pool did not terminate.");
79 }
80 long endTime = System.currentTimeMillis();
81
82 System.out.println("Paralle took time " + (endTime - startTime)
83 + " milliseconds.");
84
85 }
86}