很基础的例子client链接上server之后发送一个字符串,然后server把字符串变成大小,再返回给client。
- server端代码
import java.io.*;
import java.security.*;
import java.security.cert.CertificateException;
import javax.net.ssl.*;
public class SSLServer {
private KeyStore loadKeyStore(InputStream inputStream, String type, char[] passphrase) throws KeyStoreException, NoSuchAlgorithmException, CertificateException, FileNotFoundException, IOException {
KeyStore ks = KeyStore.getInstance(type);
ks.load(inputStream, passphrase);
return ks;
}
public static void main(String[] args){
SSLServer s = new SSLServer();
try {
s.run();
} catch (Exception e) {
e.printStackTrace();
}
}
public void run() throws KeyStoreException, NoSuchAlgorithmException, CertificateException, FileNotFoundException, IOException, NoSuchProviderException, UnrecoverableKeyException, KeyManagementException {
// Setup Trust Manager
KeyStore ts = loadKeyStore(new FileInputStream("certificate/truststore.p12"), "PKCS12", "abcd1234".toCharArray());
TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
tmf.init(ts);
TrustManager tm[] = tmf.getTrustManagers();
// Setup Key Manager
KeyStore ks = loadKeyStore(new FileInputStream("certificate/keystore.p12"), "PKCS12", "abcd1234".toCharArray());
KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
kmf.init(ks, "abcd1234".toCharArray());
KeyManager km[] = kmf.getKeyManagers(); // X509KeyManager
// Setup SSL Context
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(km, tm, null);
SSLServerSocketFactory serverSocketFactory = sslContext.getServerSocketFactory();
SSLServerSocket serverSocket = (SSLServerSocket) serverSocketFactory.createServerSocket(8333);
serverSocket.setEnabledProtocols(new String[] { "TLSv1.2" });
//serverSocket.setEnabledCipherSuites(setCiphers); // String[] setCiphers
//serverSocket.setNeedClientAuth(true);
SSLSocket socket = (SSLSocket) serverSocket.accept();
// InputStream and OutputStream Stuff
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String dataline;
while ((dataline = in.readLine()) != null) {
System.out.println(dataline);
out.println(dataline.toUpperCase());
}
}
}
- client端例子
import java.io.*;
import java.security.*;
import java.security.cert.CertificateException;
import javax.net.ssl.*;
public class SSLClient {
private KeyStore loadKeyStore(InputStream inputStream, String type, char[] passphrase) throws KeyStoreException, NoSuchAlgorithmException, CertificateException, FileNotFoundException, IOException {
KeyStore ks = KeyStore.getInstance(type);
ks.load(inputStream, passphrase);
return ks;
}
public static void main(String[] args){
SSLClient s = new SSLClient();
try {
s.run();
} catch (Exception e) {
e.printStackTrace();
}
}
public void run() throws KeyStoreException, NoSuchAlgorithmException, CertificateException, FileNotFoundException, IOException, NoSuchProviderException, UnrecoverableKeyException, KeyManagementException {
// Setup Trust Manager
KeyStore ts = loadKeyStore(new FileInputStream("certificate/truststore.p12"), "PKCS12", "abcd1234".toCharArray());
TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
tmf.init(ts);
TrustManager tm[] = tmf.getTrustManagers();
// Setup Key Manager
KeyStore ks = loadKeyStore(new FileInputStream("certificate/keystore.p12"), "PKCS12", "abcd1234".toCharArray());
KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
kmf.init(ks, "abcd1234".toCharArray());
KeyManager km[] = kmf.getKeyManagers(); // X509KeyManager
// Setup SSL Context
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(km, tm, null);
SSLSocketFactory socketFactory = sslContext.getSocketFactory();
SSLSocket socket = (SSLSocket) socketFactory.createSocket("127.0.0.1", 8333);
socket.setEnabledProtocols(new String[] { "TLSv1.2" });
//socket.setEnabledCipherSuites(setCiphers); // String[] setCiphers
// InputStream and OutputStream Stuff
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String dataline = "hello";
out.println(dataline);
dataline = in.readLine();
System.out.println(dataline);
}
}