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 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
| import java.io.*;
import javax.net.ssl.*;
import java.security.KeyStore; //necessaire pour la gestion des magasins de clé
class Serveur {
public static final int PORT = 15000;
public static final String HOST = "127.0.0.1";
public static final String KEYSTORE_FILE = "server_keystore";
public static final String ALGORITHM = "sunx509";
public static final String PASSWORD = "password";
public static void main(String[] args) {
try {
SSLServerSocket ecoute = null;
KeyManagerFactory kmf;
KeyManager[] km;
KeyStore ks;
TrustManagerFactory tmf;
TrustManager[] tm;
SSLContext sslc;
ks = KeyStore.getInstance("JKS");
ks.load(new FileInputStream(KEYSTORE_FILE), PASSWORD.toCharArray());
kmf = KeyManagerFactory.getInstance(ALGORITHM);
kmf.init(ks, PASSWORD.toCharArray());
km = kmf.getKeyManagers();
tmf = TrustManagerFactory.getInstance(ALGORITHM);
tmf.init(ks);
tm = tmf.getTrustManagers();
sslc = SSLContext.getInstance("TLS");
sslc.init(km, tm, null);
SSLServerSocketFactory ssf = sslc.getServerSocketFactory();
ecoute = (SSLServerSocket) ssf.createServerSocket(PORT);
SSLSocket c = (SSLSocket) ecoute.accept();
BufferedReader entree = new BufferedReader(new InputStreamReader(c.getInputStream()));
while (true) {
try {
System.out.println(entree.readLine());
} catch (IOException e) {
System.out.println("Erreur " + e);
}
}
} catch (Exception e) {
System.out.println(e);
}
}
}
Le client :
import java.io.*;
import javax.net.ssl.*;
import java.security.KeyStore;
class Client {
public static final int PORT = 15000;
public static final String HOST = "127.0.0.1";
public static final String KEYSTORE_FILE = "client_keystore";
public static final String ALGORITHM = "sunx509";
public static final String PASSWORD = "password";
public static void main(String[] args) {
try {
SSLSocket socket = null;
KeyManagerFactory kmf;
KeyStore ks;
TrustManagerFactory tmf;
SSLContext sslc;
String line;
kmf = KeyManagerFactory.getInstance(ALGORITHM);
ks = KeyStore.getInstance("JKS");
ks.load(new FileInputStream(KEYSTORE_FILE), PASSWORD.toCharArray());
kmf.init(ks, PASSWORD.toCharArray());
tmf = TrustManagerFactory.getInstance(ALGORITHM);
tmf.init(ks);
sslc = SSLContext.getInstance("TLS");
sslc.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
SSLSocketFactory sf = sslc.getSocketFactory();
socket = (SSLSocket) sf.createSocket(HOST, PORT);
System.out.println("Client connecte\n");
PrintWriter sortie = new PrintWriter(socket.getOutputStream(), true);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
while (true) {
try {
line = br.readLine();
sortie.println(line);
sortie.flush();
} catch (IOException e) {
System.out.println("Erreur " + e);
}
}
} catch (Exception e) {
System.out.println(e);
}
}
} |
Partager