The SSLEngine
engine class is only needed when you want to access to the low-level SSL features, without the socket wrapping. But if you want to secure a network connection using the SSLSocketFactory
, SSLServerSocketFactory
or even HttpsURLConnection
classes may do the job.
An short example code snippet:
SSLContext sslContext = SSLContext.getInstance("TLS");
SSLSocketFactory sf = sslContext.getSSLSocketFactory();
Socket socket = new Socket();
Socket sslSocket = sf.createSocket(socket, "ssl.example.com", 4443, true);
OutputStream out = sslSocket.getOutputStream();
out.write(/* ... */);
Note that if you are not using the Sun JDK/JRE implementation, the "TLS" algorithm implementation may be absent and the SSLContext.getInstance
may throw a NoSuchAlgorithmException
. In that case you should use the BouncyCastle Security Provider.
You can add the BC provider with calling:
java.security.Security.addProvider(new BouncyCastleProvider());
And request an BC-specific SSLContext
implementation:
SSLContext sslContext = SSLContext.getInstance("TLS", "BC");