Skip to content

Commit 290055e

Browse files
Concurrent AES Encryption
1 parent 6bbb690 commit 290055e

File tree

2 files changed

+112
-0
lines changed

2 files changed

+112
-0
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package com.howtodoinjava.core.security.concurrent;
2+
3+
import java.security.SecureRandom;
4+
import java.security.spec.KeySpec;
5+
import java.util.Base64;
6+
import java.util.concurrent.Callable;
7+
import javax.crypto.Cipher;
8+
import javax.crypto.SecretKey;
9+
import javax.crypto.SecretKeyFactory;
10+
import javax.crypto.spec.IvParameterSpec;
11+
import javax.crypto.spec.PBEKeySpec;
12+
import javax.crypto.spec.SecretKeySpec;
13+
14+
public class AES256EncryptionTask implements Callable<String> {
15+
16+
private static final int KEY_LENGTH = 256;
17+
private static final int ITERATION_COUNT = 65536;
18+
19+
private String strToEncrypt;
20+
private String secretKey;
21+
private String salt;
22+
23+
public AES256EncryptionTask(String strToEncrypt, String secretKey, String salt) {
24+
this.strToEncrypt = strToEncrypt;
25+
this.secretKey = secretKey;
26+
this.salt = salt;
27+
}
28+
29+
@Override
30+
public String call() throws Exception {
31+
return encrypt(strToEncrypt, secretKey, salt);
32+
}
33+
34+
public String encrypt(String strToEncrypt, String secretKey, String salt) {
35+
36+
try {
37+
38+
SecureRandom secureRandom = new SecureRandom();
39+
byte[] iv = new byte[16];
40+
secureRandom.nextBytes(iv);
41+
IvParameterSpec ivspec = new IvParameterSpec(iv);
42+
43+
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256");
44+
KeySpec spec = new PBEKeySpec(secretKey.toCharArray(), salt.getBytes(), ITERATION_COUNT,
45+
KEY_LENGTH);
46+
SecretKey tmp = factory.generateSecret(spec);
47+
SecretKeySpec secretKeySpec = new SecretKeySpec(tmp.getEncoded(), "AES");
48+
49+
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
50+
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivspec);
51+
52+
byte[] cipherText = cipher.doFinal(strToEncrypt.getBytes("UTF-8"));
53+
byte[] encryptedData = new byte[iv.length + cipherText.length];
54+
System.arraycopy(iv, 0, encryptedData, 0, iv.length);
55+
System.arraycopy(cipherText, 0, encryptedData, iv.length, cipherText.length);
56+
57+
return Base64.getEncoder().encodeToString(encryptedData);
58+
} catch (Exception e) {
59+
// Handle the exception properly
60+
e.printStackTrace();
61+
return null;
62+
}
63+
}
64+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.howtodoinjava.core.security.concurrent;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import java.util.Random;
6+
import java.util.concurrent.ExecutorService;
7+
import java.util.concurrent.Executors;
8+
import java.util.concurrent.Future;
9+
import java.util.stream.Stream;
10+
11+
public class Demo {
12+
13+
public static void main(String[] args) {
14+
15+
// Define your secret key and salt (keep these secure and don't hardcode in production)
16+
String secretKey = "MySecretKey";
17+
String salt = "MySalt";
18+
19+
Random rand = new Random();
20+
List<String> dataLines = Stream
21+
.generate(() -> "Token Value : " + rand.nextInt(100))
22+
.limit(100)
23+
.toList();
24+
25+
int numberOfThreads = 4; // Adjust based on your system capabilities
26+
ExecutorService executor = Executors.newFixedThreadPool(numberOfThreads);
27+
28+
List<Future<String>> futures = new ArrayList<>();
29+
for (String dataLine : dataLines) {
30+
// Create new AES256 instance for each line or data chunk
31+
AES256EncryptionTask task = new AES256EncryptionTask(dataLine, secretKey, salt);
32+
futures.add(executor.submit(task));
33+
}
34+
35+
// Wait for all tasks to complete and collect results
36+
for (Future<String> future : futures) {
37+
try {
38+
String encryptedData = future.get();
39+
System.out.println(encryptedData);
40+
// Process the encrypted data, e.g., write to file
41+
} catch (Exception e) {
42+
e.printStackTrace();
43+
}
44+
}
45+
46+
executor.shutdown();
47+
}
48+
}

0 commit comments

Comments
 (0)