-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCryptFunctions.java
More file actions
71 lines (65 loc) · 2.86 KB
/
CryptFunctions.java
File metadata and controls
71 lines (65 loc) · 2.86 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import java.security.SecureRandom;
import java.util.Base64;
import javax.crypto.*;
import javax.crypto.spec.*;
/**
* CryptFunctions class includes functions related to encryption and decryption
**/
public class CryptFunctions {
/**
* This function was written to generate secret keys.
* The generated key is converted to Base64 form to be sent to clients via the server.
*
* @param keySize --> Secret key size
* @param method --> Encryption method
**/
public static String secretKeyGenerator(int keySize, String method) throws Exception {
KeyGenerator keyGenerator = KeyGenerator.getInstance(method);
keyGenerator.init(keySize);
SecretKey secretKey = keyGenerator.generateKey();
return Base64.getEncoder().encodeToString(secretKey.getEncoded());
}
/**
* This function was written to create initialization vectors.
* The resulting vector was converted to Base64 form to be sent to clients via the server.
**/
public static String initializationVectorGenerator(int vectorSize) {
byte[] IV = new byte[vectorSize];
SecureRandom random = new SecureRandom();
random.nextBytes(IV);
return Base64.getEncoder().encodeToString(IV);
}
/**
* The plain text is encrypted according to the parameters.
*
* @param plaintext --> Decrypted text
* @param key --> Secret key received from server
* @param IV --> Initialization vector received from server
* @param method --> Encryption method
* @param mode --> Encryption mode
*/
public static byte[] encrypt(byte[] plaintext, SecretKey key, byte[] IV, String method, String mode) throws Exception {
Cipher cipher = Cipher.getInstance(method + "/" + mode + "/PKCS5Padding");
SecretKeySpec keySpec = new SecretKeySpec(key.getEncoded(), method);
IvParameterSpec ivSpec = new IvParameterSpec(IV);
cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivSpec);
return cipher.doFinal(plaintext);
}
/**
* The cipher text is decrypted according to the parameters.
*
* @param cipherText --> Encrypted text
* @param key --> Secret key received from server
* @param IV --> Initialization vector received from server
* @param method --> Encryption method
* @param mode --> Encryption mode
*/
public static String decrypt(byte[] cipherText, SecretKey key, byte[] IV, String method, String mode) throws Exception {
Cipher cipher = Cipher.getInstance(method + "/" + mode + "/PKCS5Padding");
SecretKeySpec keySpec = new SecretKeySpec(key.getEncoded(), method);
IvParameterSpec ivSpec = new IvParameterSpec(IV);
cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);
byte[] decryptedText = cipher.doFinal(cipherText);
return new String(decryptedText);
}
}