-
Notifications
You must be signed in to change notification settings - Fork 0
/
KeyManager.java
70 lines (57 loc) · 1.68 KB
/
KeyManager.java
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
import java.util.Random;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import javax.crypto.Cipher;
public class KeyManager {
private String AESKey;
private String AESIV;
private KeyPair RSAUser1;
private KeyPair RSAUser2;
public KeyManager() throws Exception {
AESKey = getRandomHexString(32);
AESIV = getRandomHexString(16);
RSAUser1 = buildKeyPair();
RSAUser2 = buildKeyPair();
}
public static KeyPair buildKeyPair() throws NoSuchAlgorithmException {
final int keySize = 2048;
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
keyPairGenerator.initialize(keySize);
return keyPairGenerator.genKeyPair();
}
public KeyPair getRSAKeyPair(int gebruiker) {
if (gebruiker == 1)
return RSAUser1;
else
return RSAUser2;
}
public PublicKey getRSAKeyPub(int gebruiker) {
if (gebruiker == 1)
return RSAUser1.getPublic();
else
return RSAUser2.getPublic();
}
public PrivateKey getRSAKeyPrivate(int gebruiker) {
if (gebruiker == 1)
return RSAUser1.getPrivate();
else
return RSAUser2.getPrivate();
}
private String getRandomHexString(int numchars){
Random r = new Random();
StringBuffer sb = new StringBuffer();
while(sb.length() < numchars){
sb.append(Integer.toHexString(r.nextInt()));
}
return sb.toString().substring(0, numchars);
}
public String getAESKey() {
return AESKey;
}
public String getAESIV() {
return AESIV;
}
}