FEAT: Multiclient + Crypté + Pseudo

This commit is contained in:
Lucàs
2022-12-05 13:03:14 +01:00
parent 6466284b1b
commit 75e873d44f
14 changed files with 525 additions and 196 deletions
+124
View File
@@ -0,0 +1,124 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Palette2">
<group name="Swing">
<item class="com.intellij.uiDesigner.HSpacer" tooltip-text="Horizontal Spacer" icon="/com/intellij/uiDesigner/icons/hspacer.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="1" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="com.intellij.uiDesigner.VSpacer" tooltip-text="Vertical Spacer" icon="/com/intellij/uiDesigner/icons/vspacer.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="1" anchor="0" fill="2" />
</item>
<item class="javax.swing.JPanel" icon="/com/intellij/uiDesigner/icons/panel.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3" />
</item>
<item class="javax.swing.JScrollPane" icon="/com/intellij/uiDesigner/icons/scrollPane.svg" removable="false" auto-create-binding="false" can-attach-label="true">
<default-constraints vsize-policy="7" hsize-policy="7" anchor="0" fill="3" />
</item>
<item class="javax.swing.JButton" icon="/com/intellij/uiDesigner/icons/button.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="0" fill="1" />
<initial-values>
<property name="text" value="Button" />
</initial-values>
</item>
<item class="javax.swing.JRadioButton" icon="/com/intellij/uiDesigner/icons/radioButton.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="RadioButton" />
</initial-values>
</item>
<item class="javax.swing.JCheckBox" icon="/com/intellij/uiDesigner/icons/checkBox.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="CheckBox" />
</initial-values>
</item>
<item class="javax.swing.JLabel" icon="/com/intellij/uiDesigner/icons/label.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="8" fill="0" />
<initial-values>
<property name="text" value="Label" />
</initial-values>
</item>
<item class="javax.swing.JTextField" icon="/com/intellij/uiDesigner/icons/textField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JPasswordField" icon="/com/intellij/uiDesigner/icons/passwordField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JFormattedTextField" icon="/com/intellij/uiDesigner/icons/formattedTextField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JTextArea" icon="/com/intellij/uiDesigner/icons/textArea.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTextPane" icon="/com/intellij/uiDesigner/icons/textPane.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JEditorPane" icon="/com/intellij/uiDesigner/icons/editorPane.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JComboBox" icon="/com/intellij/uiDesigner/icons/comboBox.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="2" anchor="8" fill="1" />
</item>
<item class="javax.swing.JTable" icon="/com/intellij/uiDesigner/icons/table.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JList" icon="/com/intellij/uiDesigner/icons/list.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="2" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTree" icon="/com/intellij/uiDesigner/icons/tree.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTabbedPane" icon="/com/intellij/uiDesigner/icons/tabbedPane.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSplitPane" icon="/com/intellij/uiDesigner/icons/splitPane.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSpinner" icon="/com/intellij/uiDesigner/icons/spinner.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSlider" icon="/com/intellij/uiDesigner/icons/slider.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSeparator" icon="/com/intellij/uiDesigner/icons/separator.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3" />
</item>
<item class="javax.swing.JProgressBar" icon="/com/intellij/uiDesigner/icons/progressbar.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="javax.swing.JToolBar" icon="/com/intellij/uiDesigner/icons/toolbar.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1">
<preferred-size width="-1" height="20" />
</default-constraints>
</item>
<item class="javax.swing.JToolBar$Separator" icon="/com/intellij/uiDesigner/icons/toolbarSeparator.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="0" fill="1" />
</item>
<item class="javax.swing.JScrollBar" icon="/com/intellij/uiDesigner/icons/scrollbar.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="0" anchor="0" fill="2" />
</item>
</group>
</component>
</project>
+64 -30
View File
@@ -2,10 +2,18 @@
<project version="4">
<component name="ChangeListManager">
<list default="true" id="79e91d13-7d5f-4b9a-954b-9bc7cde074fc" name="Changes" comment="">
<change afterPath="$PROJECT_DIR$/src/client/ListenThread.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/serveur/Server.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/serveur/ThreadServer.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/test/utils/ResolutionDeNomTest.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/key.ser" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/projet-mini-chat.iml" beforeDir="false" afterPath="$PROJECT_DIR$/projet-mini-chat.iml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/client/Client.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/client/Client.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/reseau/RSA.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/reseau/RSA.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/serveur/Serveur.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/serveur/Serveur.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/reseau/AES.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/utils/AES.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/reseau/RSA.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/utils/RSA.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/reseau/ResolutionDeNom.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/utils/ResolutionDeNom.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/serveur/Serveur.java" beforeDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
@@ -15,6 +23,8 @@
<component name="FileTemplateManagerImpl">
<option name="RECENT_TEMPLATES">
<list>
<option value="JUnit5 Test Class" />
<option value="Interface" />
<option value="Class" />
</list>
</option>
@@ -33,24 +43,47 @@
<option name="hideEmptyMiddlePackages" value="true" />
<option name="showLibraryContents" value="true" />
</component>
<component name="PropertiesComponent">{
&quot;keyToString&quot;: {
&quot;RunOnceActivity.OpenProjectViewOnStart&quot;: &quot;true&quot;,
&quot;RunOnceActivity.ShowReadmeOnStart&quot;: &quot;true&quot;,
&quot;WebServerToolWindowFactoryState&quot;: &quot;false&quot;,
&quot;codeWithMe.voiceChat.enabledByDefault&quot;: &quot;false&quot;,
&quot;project.structure.last.edited&quot;: &quot;Project&quot;,
&quot;project.structure.proportion&quot;: &quot;0.0&quot;,
&quot;project.structure.side.proportion&quot;: &quot;0.0&quot;
<component name="PropertiesComponent"><![CDATA[{
"keyToString": {
"Downloaded.Files.Path.Enabled": "false",
"Repository.Attach.Annotations": "false",
"Repository.Attach.JavaDocs": "false",
"Repository.Attach.Sources": "false",
"RunOnceActivity.OpenProjectViewOnStart": "true",
"RunOnceActivity.ShowReadmeOnStart": "true",
"WebServerToolWindowFactoryState": "false",
"codeWithMe.voiceChat.enabledByDefault": "false",
"com.intellij.testIntegration.createTest.CreateTestDialog.defaultLibrary": "JUnit5",
"com.intellij.testIntegration.createTest.CreateTestDialog.defaultLibrarySuperClass.JUnit5": "",
"create.test.in.the.same.root": "true",
"last_opened_file_path": "/Users/lucas/Documents/GitHub/projet-mini-chat/src/serveur",
"project.structure.last.edited": "Modules",
"project.structure.proportion": "0.15",
"project.structure.side.proportion": "0.2",
"settings.editor.selected.configurable": "preferences.lookFeel"
}
}</component>
}]]></component>
<component name="RecentsManager">
<key name="CopyFile.RECENT_KEYS">
<recent name="$PROJECT_DIR$/src/serveur" />
</key>
<key name="MoveFile.RECENT_KEYS">
<recent name="$PROJECT_DIR$/src/client" />
</key>
<key name="CreateTestDialog.Recents.Supers">
<recent name="" />
</key>
<key name="CreateTestDialog.RecentsKey">
<recent name="utils" />
</key>
</component>
<component name="RunManager" selected="Application.Client">
<configuration name="AES" type="Application" factoryName="Application" temporary="true" nameIsGenerated="true">
<option name="MAIN_CLASS_NAME" value="reseau.AES" />
<configuration name="Client (1)" type="Application" factoryName="Application" nameIsGenerated="true">
<option name="MAIN_CLASS_NAME" value="client.Client" />
<module name="projet-mini-chat" />
<extension name="coverage">
<pattern>
<option name="PATTERN" value="reseau.*" />
<option name="PATTERN" value="client.*" />
<option name="ENABLED" value="true" />
</pattern>
</extension>
@@ -71,8 +104,8 @@
<option name="Make" enabled="true" />
</method>
</configuration>
<configuration name="Client2" type="Application" factoryName="Application" temporary="true" nameIsGenerated="true">
<option name="MAIN_CLASS_NAME" value="client.Client2" />
<configuration name="Client" type="Application" factoryName="Application" nameIsGenerated="true">
<option name="MAIN_CLASS_NAME" value="client.Client" />
<module name="projet-mini-chat" />
<extension name="coverage">
<pattern>
@@ -84,8 +117,8 @@
<option name="Make" enabled="true" />
</method>
</configuration>
<configuration name="Serveur" type="Application" factoryName="Application" temporary="true" nameIsGenerated="true">
<option name="MAIN_CLASS_NAME" value="serveur.Serveur" />
<configuration name="Server" type="Application" factoryName="Application" temporary="true" nameIsGenerated="true">
<option name="MAIN_CLASS_NAME" value="serveur.Server" />
<module name="projet-mini-chat" />
<extension name="coverage">
<pattern>
@@ -97,33 +130,33 @@
<option name="Make" enabled="true" />
</method>
</configuration>
<configuration name="Serveur2" type="Application" factoryName="Application" temporary="true" nameIsGenerated="true">
<option name="MAIN_CLASS_NAME" value="serveur.Serveur2" />
<configuration name="ResolutionDeNomTest.getIPAddress" type="JUnit" factoryName="JUnit" temporary="true" nameIsGenerated="true">
<module name="projet-mini-chat" />
<extension name="coverage">
<pattern>
<option name="PATTERN" value="serveur.*" />
<option name="PATTERN" value="utils.*" />
<option name="ENABLED" value="true" />
</pattern>
</extension>
<option name="PACKAGE_NAME" value="utils" />
<option name="MAIN_CLASS_NAME" value="utils.ResolutionDeNomTest" />
<option name="METHOD_NAME" value="getIPAddress" />
<option name="TEST_OBJECT" value="method" />
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
<list>
<item itemvalue="Application.AES" />
<item itemvalue="Application.Client2" />
<item itemvalue="Application.Serveur2" />
<item itemvalue="Application.Client" />
<item itemvalue="Application.Serveur" />
<item itemvalue="Application.Client (1)" />
<item itemvalue="Application.Server" />
<item itemvalue="JUnit.ResolutionDeNomTest.getIPAddress" />
</list>
<recent_temporary>
<list>
<item itemvalue="Application.Server" />
<item itemvalue="Application.Client" />
<item itemvalue="Application.Serveur" />
<item itemvalue="Application.AES" />
<item itemvalue="Application.Client2" />
<item itemvalue="Application.Serveur2" />
<item itemvalue="JUnit.ResolutionDeNomTest.getIPAddress" />
</list>
</recent_temporary>
</component>
@@ -138,6 +171,7 @@
<workItem from="1668515476841" duration="6049000" />
<workItem from="1669119772164" duration="6834000" />
<workItem from="1669724625302" duration="6630000" />
<workItem from="1670230395395" duration="11270000" />
</task>
<servers />
</component>
BIN
View File
Binary file not shown.
+17
View File
@@ -4,8 +4,25 @@
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/test" isTestSource="true" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="module-library" scope="TEST">
<library name="JUnit5.8.1">
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter/5.8.1/junit-jupiter-5.8.1.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-api/5.8.1/junit-jupiter-api-5.8.1.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/opentest4j/opentest4j/1.2.0/opentest4j-1.2.0.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/junit/platform/junit-platform-commons/1.8.1/junit-platform-commons-1.8.1.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/apiguardian/apiguardian-api/1.1.2/apiguardian-api-1.1.2.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-params/5.8.1/junit-jupiter-params-5.8.1.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-engine/5.8.1/junit-jupiter-engine-5.8.1.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/junit/platform/junit-platform-engine/1.8.1/junit-platform-engine-1.8.1.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
</component>
</module>
+72 -51
View File
@@ -1,71 +1,92 @@
package client;
import java.io.*;
import utils.RSA;
import utils.ResolutionDeNom;
import java.io.EOFException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ConnectException;
import java.net.Socket;
import java.rmi.UnknownHostException;
import java.security.KeyPair;
import java.security.PublicKey;
import java.util.Objects;
import java.util.Scanner;
import reseau.RSA;
public class Client {
public static final Scanner scan = new Scanner(System.in);
private final String SERVER_IP;
private final int SERVER_PORT;
private final Scanner scanner;
private ObjectInputStream inputStream;
private ObjectOutputStream outputStream;
private KeyPair clientKeys;
private PublicKey serverKey;
public static void main(String[] args) {
Socket serverSocket = null;
ObjectOutputStream out = null;
ObjectInputStream in = null;
private String pseudo;
// Clé de chiffrage
KeyPair clientKeyPair = RSA.genererCle();
PublicKey serverKey = null;
// Création des Sockets
try {
serverSocket = new Socket("localhost", 4444);
System.out.println("Connecté au serveur");
out = new ObjectOutputStream(serverSocket.getOutputStream());
in = new ObjectInputStream(serverSocket.getInputStream());
// On envoie la clé de chiffrage
out.writeObject(clientKeyPair.getPublic());
serverKey = (PublicKey) in.readObject();
} catch (UnknownHostException e) {
System.out.println("Destination unknown");
System.exit(-1);
} catch (IOException e) {
System.out.println("now to investigate this IO issue");
System.exit(-1);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
public Client(String ip, int port) {
this.SERVER_IP = ResolutionDeNom.getIPAddress(ip);
this.SERVER_PORT = port;
this.scanner = new Scanner(System.in);
this.clientKeys = RSA.genererCle();
}
// Communication
public void start() {
System.out.print("Saisir un pseudo: ");
pseudo = scanner.nextLine();
try (Socket socket = new Socket(SERVER_IP, SERVER_PORT)) {
inputStream = new ObjectInputStream(socket.getInputStream());
outputStream = new ObjectOutputStream(socket.getOutputStream());
exchangeKeys();
sendMessage(pseudo);
// Ecoute du serveur
ListenThread threadClient = new ListenThread(this);
threadClient.start();
// Ecoute de l'entrée du clavier
System.out.println("Tappez 'bye' pour quitter\n");
String message;
byte[] messageCrypte;
try {
do {
// Envoi du message
System.out.print("client > ");
message = scan.nextLine();
messageCrypte = RSA.encrypter(message, serverKey);
out.writeObject(messageCrypte);
message = scanner.nextLine();
sendMessage(message);
} while (!Objects.equals(message, "bye"));
// Reception du message
messageCrypte = (byte[]) in.readObject();
message = RSA.decrypter(messageCrypte, clientKeyPair.getPrivate());
System.out.printf("serveur > %s\n", message);
} while (!message.equals("bye"));
out.close();
in.close();
serverSocket.close();
}catch (IOException | ClassNotFoundException e) {
} catch (ConnectException e) {
System.err.println("Serveur non trouvé");
}catch (EOFException e) {
System.err.println("Connexion perdue");
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
}
public void exchangeKeys() throws IOException, ClassNotFoundException {
// Envoie sa clé
outputStream.writeObject(clientKeys.getPublic());
// Attend la clé du serveur
serverKey = (PublicKey) inputStream.readObject();
}
public void sendMessage(String message) throws IOException {
byte[] messageCrypte = RSA.encrypter(message, serverKey);
outputStream.writeObject(messageCrypte);
}
public String getMessage() throws IOException, ClassNotFoundException {
byte[] messageCrypte = (byte[]) inputStream.readObject();
return RSA.decrypter(messageCrypte, clientKeys.getPrivate());
}
public static void main(String[] args) {
Client client = new Client("localhost", 4444);
client.start();
}
}
+35
View File
@@ -0,0 +1,35 @@
package client;
import java.io.EOFException;
import java.io.IOException;
import java.net.SocketException;
/**
* Thread for clients
*/
public class ListenThread extends Thread {
private final Client client;
public ListenThread(Client client) {
this.client = client;
}
@Override
public void run() {
try {
String message;
while (true) {
message = client.getMessage();
System.out.println(message);
}
} catch (SocketException ignored) {
System.out.println("Vous avez quitté le salon");
} catch (EOFException ignored) {
System.err.println("Connexion perdue");
}catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
}
}
-27
View File
@@ -1,27 +0,0 @@
package reseau;
import java.net.InetAddress;
import java.net.UnknownHostException;
public class ResolutionDeNom {
public static InetAddress getAddress(String host) {
InetAddress address;
try {
address = InetAddress.getByName(host);
return address;
} catch (UnknownHostException e) {
e.printStackTrace();
}
return null;
}
public static void main(String[] args) {
// String input = args[0];
String input = "www.google.com";
InetAddress address = getAddress(input);
if (address != null) {
System.out.printf("%s : %s\n", input, address.getHostAddress());
}
}
}
+38
View File
@@ -0,0 +1,38 @@
package serveur;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
public class Server {
private final int PORT;
private final ArrayList<ThreadServer> threads;
public Server(int port) {
this.PORT = port;
this.threads = new ArrayList<>();
}
public void start() {
try (ServerSocket serversocket = new ServerSocket(PORT)) {
System.out.println("Server is started...");
while (true) {
Socket socket = serversocket.accept();
System.out.printf("Nouvelle connexion : %s\n", socket);
ThreadServer thread = new ThreadServer(socket, threads);
threads.add(thread);
thread.start();
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
Server serveur = new Server(4444);
serveur.start();
}
}
-75
View File
@@ -1,75 +0,0 @@
package serveur;
import reseau.RSA;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.security.KeyPair;
import java.security.PublicKey;
import java.util.Scanner;
public class Serveur {
private static final int PORT = 4444;
private static final Scanner scan = new Scanner(System.in);
public static void main(String[] args) {
ServerSocket serverSocket = null;
Socket clientSocket = null;
KeyPair serverKeyPairs = RSA.genererCle();
PublicKey clientKey;
// Connexion
try {
serverSocket = new ServerSocket(PORT); // Crée le serveur
clientSocket = serverSocket.accept(); // On recherche un client
} catch (IOException e) {
System.out.printf("Erreur de connexion sur le port: %d\n", PORT);
System.exit(-1);
}
// Un client a été trouvé
System.out.println("Client connecté");
ObjectInputStream in;
ObjectOutputStream out;
try {
in = new ObjectInputStream(clientSocket.getInputStream());
out = new ObjectOutputStream(clientSocket.getOutputStream());
// On récupère la clé du client
clientKey = (PublicKey) in.readObject();
out.writeObject(serverKeyPairs.getPublic());
// Communication
String message;
byte[] messageCrypte;
do {
// Envoi du message
messageCrypte = (byte[]) in.readObject();
message = RSA.decrypter(messageCrypte, serverKeyPairs.getPrivate());
System.out.printf("client > %s\n", message);
// Reception du message
System.out.print("serveur > ");
message = scan.nextLine();
messageCrypte = RSA.encrypter(message, clientKey);
out.writeObject(messageCrypte);
} while (!message.equals("bye"));
in.close();
out.close();
clientSocket.close();
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}
}
+83
View File
@@ -0,0 +1,83 @@
package serveur;
import utils.RSA;
import java.io.*;
import java.net.Socket;
import java.security.KeyPair;
import java.security.PublicKey;
import java.util.ArrayList;
import java.util.Objects;
public class ThreadServer extends Thread {
private final ArrayList<ThreadServer> threads;
private final ObjectInputStream inputStream;
private final ObjectOutputStream outputStream;
private final KeyPair serverKeys;
private PublicKey clientKey;
private String clientPseudo;
public ThreadServer(Socket socket, ArrayList<ThreadServer> threads) throws IOException {
this.threads = threads;
this.outputStream = new ObjectOutputStream(socket.getOutputStream());
this.inputStream = new ObjectInputStream(socket.getInputStream());
this.serverKeys = RSA.genererCle();
}
@Override
public void run() {
try {
exchangeKeys();
// Récupère le pseudo
this.clientPseudo = getMessage();
sendMessageToEveryone(String.format("** %s viens de rejoindre le salon **", clientPseudo), true);
String message;
do {
message = getMessage();
String reply = String.format("%s : %s", clientPseudo, message);
sendMessageToEveryone(reply, true);
} while (!Objects.equals(message, "bye"));
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
} finally {
threads.remove(this);
try {
sendMessageToEveryone(String.format("** %s viens de quitter le salon **", clientPseudo), true);
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void exchangeKeys() throws IOException, ClassNotFoundException {
// Attend la clé du client
clientKey = (PublicKey) inputStream.readObject();
// Envoie sa clé
outputStream.writeObject(serverKeys.getPublic());
}
public String getMessage() throws IOException, ClassNotFoundException {
byte[] messageCrypte = (byte[]) inputStream.readObject();
return RSA.decrypter(messageCrypte, serverKeys.getPrivate());
}
public void sendMessage(String message, boolean log) throws IOException {
byte[] messageCrypte = RSA.encrypter(message, clientKey);
outputStream.writeObject(messageCrypte);
if (log) System.out.println(message);
}
public void sendMessageToEveryone(String message, boolean log) throws IOException {
for (ThreadServer thread : threads) {
thread.sendMessage(message, false);
}
if (log) System.out.println(message);
}
}
+44 -13
View File
@@ -1,4 +1,4 @@
package reseau;
package utils;
import javax.crypto.*;
import java.io.*;
@@ -7,6 +7,13 @@ import java.security.*;
public class AES {
private static final String SAVE_FILE = "key.sav";
/**
* TODO
*
* @return
*/
public static Key genererCle() {
Key key = null;
@@ -19,40 +26,64 @@ public class AES {
return key;
}
public static String decrypter(byte[] msg, Key key) {
/**
* TODO
*
* @param msg
* @param key
* @return
*/
public static byte[] encrypter(String msg, Key key) {
try {
Cipher cipher = Cipher.getInstance("DES") ;
cipher.init(Cipher.DECRYPT_MODE, key);
return new String(cipher.doFinal(msg), StandardCharsets.UTF_8);
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.ENCRYPT_MODE, key);
return cipher.doFinal(msg.getBytes());
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static byte[] encrypter(String msg, Key key){
/**
* TODO
*
* @param message
* @param cle
* @return
*/
public static String decrypter(byte[] message, Key cle) {
try {
Cipher cipher = Cipher.getInstance("DES") ;
cipher.init(Cipher.ENCRYPT_MODE, key) ;
return cipher.doFinal(msg.getBytes());
} catch(Exception e){
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.DECRYPT_MODE, cle);
return new String(cipher.doFinal(message), StandardCharsets.UTF_8);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* TODO
*
* @param key
*/
public static void sauvegarderCle(Key key) {
try (ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream("key.ser"))) {
try (ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream(SAVE_FILE))) {
objectOutputStream.writeObject(key);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
/**
* TODO
*
* @return
*/
public static Key chargerCle() {
Key key = null;
try (ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream("key.ser"))) {
Key key;
try (ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream(SAVE_FILE))) {
key = (Key) objectInputStream.readObject();
} catch (IOException | ClassNotFoundException e) {
throw new RuntimeException(e);
+1 -1
View File
@@ -1,4 +1,4 @@
package reseau;
package utils;
import javax.crypto.Cipher;
import java.nio.charset.StandardCharsets;
+23
View File
@@ -0,0 +1,23 @@
package utils;
import java.net.InetAddress;
import java.net.UnknownHostException;
public class ResolutionDeNom {
/**
* TODO
*
* @param host
* @return
*/
public static String getIPAddress(String host) {
String address = null;
try {
address = InetAddress.getByName(host).getHostAddress();
} catch (UnknownHostException e) {
e.printStackTrace();
}
return address;
}
}
+25
View File
@@ -0,0 +1,25 @@
package utils;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class ResolutionDeNomTest {
@Test
void getIPAddress() {
final int EXPECTED = 1;
final int ACTUAL = 0;
final String[][] FIXTURE = {
{"localhost", "127.0.0.1"},
{"www.univ-jfc.fr", "194.57.185.14"},
};
for (String[] test : FIXTURE) {
assertEquals(test[EXPECTED], ResolutionDeNom.getIPAddress(test[ACTUAL]));
System.out.printf("%s -> %s : OK\n", test[ACTUAL], test[EXPECTED]);
}
}
}