This commit is contained in:
Vu Tuan Minh
2025-03-04 12:24:22 +01:00
parent e67d44a4e0
commit d28fff89e4
3 changed files with 143 additions and 11 deletions

View File

@@ -1,14 +1,24 @@
package fr.istic.chiffrement; package fr.istic.chiffrement;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
public class Client { public class Client {
private static char[] PASSWORD = "654321".toCharArray(); private static char[] PASSWORD = "654321".toCharArray();
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
// Création d'un SSLContext comme pour le serveur try{
// avec les caractéristiques suivantes : // Création d'un SSLContext comme pour le serveur
// + protocole utilisé : TLS // avec les caractéristiques suivantes :
// + format de clef PKCS12 // + protocole utilisé : TLS
// + format de clef PKCS12
SSLSocketFactory sslsocketfactory = (SSLSocketFactory) SSLSocketFactory.getDefault();
SSLSocket sslsocket = (SSLSocket) sslsocketfactory.createSocket("www.google.fr", 443);
}catch (Exception e){
e.printStackTrace();
}
// + mot de passe store et clef : variable PASSWORD plus haut (ou null) // + mot de passe store et clef : variable PASSWORD plus haut (ou null)
// + keystore : clientstore.keys // + keystore : clientstore.keys

View File

@@ -1,17 +1,50 @@
package fr.istic.chiffrement; package fr.istic.chiffrement;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
import java.io.IOException;
import java.security.cert.Certificate;
public class TestSite { public class TestSite {
public static void main(String[] args) { public static void main(String[] args) {
// connecteEtAffiche("www.google.fr",443);
// connecteEtAffiche("nextinpact.com",443);
connecteEtAffiche("istic.univ-rennes1.fr", 443);
} }
public static void connecteEtAffiche(String host, int port) { public static void connecteEtAffiche(String host, int port) {
// Creation de la socket SSLSocket sslsocket = null;
// Choix de la cipher suite si nécessaire (sinon laisser defaut). try {
// Connexion // Creation de la socket
SSLSocketFactory sslsocketfactory = (SSLSocketFactory) SSLSocketFactory.getDefault();
// Affichage des infos demandées : sslsocket = (SSLSocket) sslsocketfactory.createSocket(host, port);
// Choix de la cipher suite si nécessaire (sinon laisser defaut).
// Vous pouvez changer les suites autorisées ici dans exercice 2.
sslsocket.setEnabledCipherSuites(sslsocket.getSupportedCipherSuites());
// Connexion
SSLSession ss = sslsocket.getSession();
// Affichage des infos demandées :
System.out.println("session creation = " + ss.getCreationTime());
System.out.println("protocol utilisé = " + ss.getProtocol());
System.out.println("cypher suite = " + ss.getCipherSuite());
System.out.println("identité serveur = " + ss.getPeerHost());
System.out.println("Certificat ");
for (Certificate certificate : ss.getPeerCertificates()) {
System.out.println(certificate.toString());
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (sslsocket != null) {
try {
sslsocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
} }
} }

View File

@@ -1,8 +1,97 @@
package fr.istic.nio; package fr.istic.nio;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.Set;
import java.util.Iterator;
public class PingPong { public class PingPong {
public static void main(String[] args) { private static final int PORT_NUMBER = 6969;
// Phrase ACCEPT
private static void accept(SocketChannel sc, SelectionKey key) throws IOException {
sc.configureBlocking(false);
ByteBuffer buffer = ByteBuffer.allocate(64);
sc.register(key.selector(), SelectionKey.OP_READ, buffer);
}
// Phrase READ
private static void read(SelectionKey key) throws IOException {
SocketChannel client = (SocketChannel) key.channel();
ByteBuffer buffer = (ByteBuffer) key.attachment();
if (client.read(buffer) < 0) {
client.close();
return;
} else {
buffer.flip();
byte[] bytes = new byte[buffer.remaining()];
buffer.get(bytes, 0, buffer.remaining());
String message = new String(bytes);
String[] splited = message.split("\\s+");
if (splited[0].equals("PING") && splited.length == 2) {
// On récupère le nombre pour bien le traiter sinon
// message.remplace est plus vite que ça mais c'est ptet un PING STRING
int number = Integer.parseInt(splited[1]);
buffer.clear();
// C'est plus facile quand on a déjà le nombre ici que le transferer
// dans la partie write, donc on le fait buffer write ici
String pong = "PONG " + number;
buffer.put(pong.getBytes());
// flip pour envoyer le buffer
buffer.flip();
key.interestOps(SelectionKey.OP_WRITE);
}
}
}
// Phrase WRITE
private static void write(SelectionKey key) throws IOException {
SocketChannel client = (SocketChannel) key.channel();
ByteBuffer buffer = (ByteBuffer) key.attachment();
client.write(buffer);
buffer.clear();
key.interestOps(SelectionKey.OP_READ);
}
public static void main(String[] args) throws IOException {
ServerSocketChannel server = ServerSocketChannel.open();
server.socket().bind(new InetSocketAddress(PORT_NUMBER));
server.socket().setReuseAddress(true);
// Mode non bloquant
server.configureBlocking(false);
// Création du sélecteur
Selector selector = Selector.open();
server.register(selector, SelectionKey.OP_ACCEPT);
while (true) {
int channelcount = selector.select();
if (channelcount > 0) {
Set<SelectionKey> keys = selector.selectedKeys();
Iterator<SelectionKey> iterator = keys.iterator();
while (iterator.hasNext()) {
SelectionKey key = iterator.next();
iterator.remove();
if (!key.isValid()) {
continue;
}
if (key.isAcceptable()) {
SocketChannel client = server.accept();
accept(client, key);
} else if (key.isReadable()) {
read(key);
} else if (key.isWritable()) {
write(key);
}
}
}
}
} }
} }