-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSSL_in_Java
More file actions
35 lines (30 loc) · 1.02 KB
/
SSL_in_Java
File metadata and controls
35 lines (30 loc) · 1.02 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
import javax.net.ssl.*;
import java.security.*;
public class TLSConfig {
public static SSLContext createSSLContext() throws Exception {
// Crear KeyStore
KeyStore keyStore = KeyStore.getInstance("PKCS12");
keyStore.load(
new FileInputStream("keystore.p12"),
"password".toCharArray()
);
// Configurar KeyManagerFactory
KeyManagerFactory kmf = KeyManagerFactory.getInstance(
KeyManagerFactory.getDefaultAlgorithm()
);
kmf.init(keyStore, "password".toCharArray());
// Configurar TrustManagerFactory
TrustManagerFactory tmf = TrustManagerFactory.getInstance(
TrustManagerFactory.getDefaultAlgorithm()
);
tmf.init(keyStore);
// Crear y configurar SSLContext
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(
kmf.getKeyManagers(),
tmf.getTrustManagers(),
new SecureRandom()
);
return sslContext;
}
}