mirror of
https://github.com/LucasVbr/mini-chat.git
synced 2026-05-13 17:21:54 +00:00
Envoi avec Message Crypté
This commit is contained in:
Generated
+15
-52
@@ -2,15 +2,13 @@
|
|||||||
<project version="4">
|
<project version="4">
|
||||||
<component name="ChangeListManager">
|
<component name="ChangeListManager">
|
||||||
<list default="true" id="79e91d13-7d5f-4b9a-954b-9bc7cde074fc" name="Changes" comment="">
|
<list default="true" id="79e91d13-7d5f-4b9a-954b-9bc7cde074fc" name="Changes" comment="">
|
||||||
<change afterPath="$PROJECT_DIR$/src/reseau/AES.java" afterDir="false" />
|
<change afterPath="$PROJECT_DIR$/README.md" afterDir="false" />
|
||||||
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
|
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" 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/client/Client.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/client/Client.java" afterDir="false" />
|
||||||
|
<change beforePath="$PROJECT_DIR$/src/reseau/AES.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/reseau/AES.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/serveur/Serveur.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/serveur/Serveur.java" afterDir="false" />
|
||||||
</list>
|
</list>
|
||||||
<list id="84fd72d2-dba6-4275-83d3-958fab491b17" name="Changes by burgu" comment="">
|
<list id="84fd72d2-dba6-4275-83d3-958fab491b17" name="Changes by burgu" comment="" />
|
||||||
<change beforePath="$PROJECT_DIR$/src/client/Client.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/client/Client.java" afterDir="false" />
|
|
||||||
<change beforePath="$PROJECT_DIR$/src/serveur/Serveur.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/serveur/Serveur.java" afterDir="false" />
|
|
||||||
</list>
|
|
||||||
<option name="SHOW_DIALOG" value="false" />
|
<option name="SHOW_DIALOG" value="false" />
|
||||||
<option name="HIGHLIGHT_CONFLICTS" value="true" />
|
<option name="HIGHLIGHT_CONFLICTS" value="true" />
|
||||||
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
|
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
|
||||||
@@ -26,42 +24,6 @@
|
|||||||
<component name="Git.Settings">
|
<component name="Git.Settings">
|
||||||
<option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$" />
|
<option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$" />
|
||||||
</component>
|
</component>
|
||||||
<component name="LineStatusTrackerManager">
|
|
||||||
<file path="$PROJECT_DIR$/src/serveur/Serveur.java">
|
|
||||||
<ranges>
|
|
||||||
<range start1="5" end1="5" start2="5" end2="6" changelist="79e91d13-7d5f-4b9a-954b-9bc7cde074fc" />
|
|
||||||
<range start1="7" end1="7" start2="8" end2="12" changelist="79e91d13-7d5f-4b9a-954b-9bc7cde074fc" />
|
|
||||||
<range start1="9" end1="16" start2="14" end2="14" changelist="79e91d13-7d5f-4b9a-954b-9bc7cde074fc" />
|
|
||||||
<range start1="17" end1="17" start2="15" end2="17" changelist="79e91d13-7d5f-4b9a-954b-9bc7cde074fc" />
|
|
||||||
<range start1="18" end1="19" start2="18" end2="20" changelist="79e91d13-7d5f-4b9a-954b-9bc7cde074fc" />
|
|
||||||
<range start1="20" end1="21" start2="21" end2="22" changelist="79e91d13-7d5f-4b9a-954b-9bc7cde074fc" />
|
|
||||||
<range start1="24" end1="24" start2="25" end2="26" changelist="79e91d13-7d5f-4b9a-954b-9bc7cde074fc" />
|
|
||||||
<range start1="25" end1="26" start2="27" end2="29" changelist="79e91d13-7d5f-4b9a-954b-9bc7cde074fc" />
|
|
||||||
<range start1="28" end1="30" start2="31" end2="32" changelist="84fd72d2-dba6-4275-83d3-958fab491b17" />
|
|
||||||
<range start1="31" end1="31" start2="33" end2="35" changelist="79e91d13-7d5f-4b9a-954b-9bc7cde074fc" />
|
|
||||||
<range start1="32" end1="35" start2="36" end2="37" changelist="84fd72d2-dba6-4275-83d3-958fab491b17" />
|
|
||||||
<range start1="36" end1="37" start2="38" end2="42" changelist="79e91d13-7d5f-4b9a-954b-9bc7cde074fc" />
|
|
||||||
<range start1="38" end1="47" start2="43" end2="49" changelist="79e91d13-7d5f-4b9a-954b-9bc7cde074fc" />
|
|
||||||
<range start1="48" end1="48" start2="50" end2="51" changelist="84fd72d2-dba6-4275-83d3-958fab491b17" />
|
|
||||||
<range start1="49" end1="50" start2="52" end2="59" changelist="79e91d13-7d5f-4b9a-954b-9bc7cde074fc" />
|
|
||||||
<range start1="55" end1="56" start2="64" end2="64" changelist="79e91d13-7d5f-4b9a-954b-9bc7cde074fc" />
|
|
||||||
</ranges>
|
|
||||||
</file>
|
|
||||||
<file path="$PROJECT_DIR$/src/client/Client.java">
|
|
||||||
<ranges>
|
|
||||||
<range start1="8" end1="8" start2="8" end2="9" changelist="79e91d13-7d5f-4b9a-954b-9bc7cde074fc" />
|
|
||||||
<range start1="10" end1="10" start2="11" end2="14" changelist="79e91d13-7d5f-4b9a-954b-9bc7cde074fc" />
|
|
||||||
<range start1="11" end1="12" start2="15" end2="16" changelist="79e91d13-7d5f-4b9a-954b-9bc7cde074fc" />
|
|
||||||
<range start1="14" end1="14" start2="18" end2="19" changelist="84fd72d2-dba6-4275-83d3-958fab491b17" />
|
|
||||||
<range start1="15" end1="15" start2="20" end2="21" changelist="84fd72d2-dba6-4275-83d3-958fab491b17" />
|
|
||||||
<range start1="16" end1="17" start2="22" end2="23" changelist="79e91d13-7d5f-4b9a-954b-9bc7cde074fc" />
|
|
||||||
<range start1="18" end1="20" start2="24" end2="26" changelist="79e91d13-7d5f-4b9a-954b-9bc7cde074fc" />
|
|
||||||
<range start1="28" end1="35" start2="34" end2="47" changelist="79e91d13-7d5f-4b9a-954b-9bc7cde074fc" />
|
|
||||||
<range start1="36" end1="36" start2="48" end2="50" changelist="84fd72d2-dba6-4275-83d3-958fab491b17" />
|
|
||||||
<range start1="38" end1="40" start2="52" end2="53" changelist="79e91d13-7d5f-4b9a-954b-9bc7cde074fc" />
|
|
||||||
</ranges>
|
|
||||||
</file>
|
|
||||||
</component>
|
|
||||||
<component name="MarkdownSettingsMigration">
|
<component name="MarkdownSettingsMigration">
|
||||||
<option name="stateVersion" value="1" />
|
<option name="stateVersion" value="1" />
|
||||||
</component>
|
</component>
|
||||||
@@ -73,18 +35,18 @@
|
|||||||
<option name="hideEmptyMiddlePackages" value="true" />
|
<option name="hideEmptyMiddlePackages" value="true" />
|
||||||
<option name="showLibraryContents" value="true" />
|
<option name="showLibraryContents" value="true" />
|
||||||
</component>
|
</component>
|
||||||
<component name="PropertiesComponent"><![CDATA[{
|
<component name="PropertiesComponent">{
|
||||||
"keyToString": {
|
"keyToString": {
|
||||||
"RunOnceActivity.OpenProjectViewOnStart": "true",
|
"RunOnceActivity.OpenProjectViewOnStart": "true",
|
||||||
"RunOnceActivity.ShowReadmeOnStart": "true",
|
"RunOnceActivity.ShowReadmeOnStart": "true",
|
||||||
"WebServerToolWindowFactoryState": "false",
|
"WebServerToolWindowFactoryState": "false",
|
||||||
"codeWithMe.voiceChat.enabledByDefault": "false",
|
"codeWithMe.voiceChat.enabledByDefault": "false",
|
||||||
"project.structure.last.edited": "Project",
|
"project.structure.last.edited": "Project",
|
||||||
"project.structure.proportion": "0.0",
|
"project.structure.proportion": "0.0",
|
||||||
"project.structure.side.proportion": "0.0"
|
"project.structure.side.proportion": "0.0"
|
||||||
}
|
}
|
||||||
}]]></component>
|
}</component>
|
||||||
<component name="RunManager" selected="Application.AES">
|
<component name="RunManager" selected="Application.Client">
|
||||||
<configuration name="AES" type="Application" factoryName="Application" temporary="true" nameIsGenerated="true">
|
<configuration name="AES" type="Application" factoryName="Application" temporary="true" nameIsGenerated="true">
|
||||||
<option name="MAIN_CLASS_NAME" value="reseau.AES" />
|
<option name="MAIN_CLASS_NAME" value="reseau.AES" />
|
||||||
<module name="projet-mini-chat" />
|
<module name="projet-mini-chat" />
|
||||||
@@ -177,6 +139,7 @@
|
|||||||
<updated>1668515473737</updated>
|
<updated>1668515473737</updated>
|
||||||
<workItem from="1668515476841" duration="6049000" />
|
<workItem from="1668515476841" duration="6049000" />
|
||||||
<workItem from="1669119772164" duration="6834000" />
|
<workItem from="1669119772164" duration="6834000" />
|
||||||
|
<workItem from="1669724625302" duration="5444000" />
|
||||||
</task>
|
</task>
|
||||||
<servers />
|
<servers />
|
||||||
</component>
|
</component>
|
||||||
|
|||||||
@@ -0,0 +1,16 @@
|
|||||||
|
# Question 6
|
||||||
|
|
||||||
|
## Avant la communication
|
||||||
|
Utiliser le RSA, avant toute communication.
|
||||||
|
Le client et le serveur génèrent 2 clés (une publique et une privées)
|
||||||
|
Ils s'échangent leurs clé publiques.
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
## Pendant la communication
|
||||||
|
Lors qu'un utilisateur veut envoyer un message, il crypte son
|
||||||
|
message avec la clé pulique de son interlocuteur et lui envoie.
|
||||||
|
La personne qui reçois le message, le décrypte le message avec
|
||||||
|
sa propre clé privée.
|
||||||
|
|
||||||
|

|
||||||
+37
-21
@@ -1,29 +1,41 @@
|
|||||||
package client;
|
package client;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
import java.io.*;
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.InputStreamReader;
|
|
||||||
import java.io.PrintWriter;
|
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.rmi.UnknownHostException;
|
import java.rmi.UnknownHostException;
|
||||||
|
import java.security.Key;
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.Scanner;
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
import reseau.AES;
|
||||||
|
|
||||||
public class Client {
|
public class Client {
|
||||||
|
|
||||||
public static final Scanner scan = new Scanner(System.in);
|
public static final Scanner scan = new Scanner(System.in);
|
||||||
|
|
||||||
public static void main(String[] args) throws IOException {
|
public static void main(String[] args) {
|
||||||
Socket serverSocket = null;
|
Socket serverSocket = null;
|
||||||
PrintWriter out = null;
|
ObjectOutputStream out = null;
|
||||||
BufferedReader in = null;
|
ObjectInputStream in = null;
|
||||||
boolean ecoute = false;
|
|
||||||
|
// Clé de chiffrage
|
||||||
|
Key key = AES.genererCle();
|
||||||
|
AES.sauvegarderCle(key);
|
||||||
|
|
||||||
// Création des Sockets
|
// Création des Sockets
|
||||||
try {
|
try {
|
||||||
serverSocket = new Socket("localhost", 4444);
|
serverSocket = new Socket("localhost", 4444);
|
||||||
System.out.println("Connecté au serveur");
|
System.out.println("Connecté au serveur");
|
||||||
out = new PrintWriter(serverSocket.getOutputStream(), true);
|
out = new ObjectOutputStream(serverSocket.getOutputStream());
|
||||||
in = new BufferedReader(new InputStreamReader(serverSocket.getInputStream()));
|
in = new ObjectInputStream(serverSocket.getInputStream());
|
||||||
|
|
||||||
|
// On envoie la clé de chiffrage
|
||||||
|
out.writeObject(key);
|
||||||
|
|
||||||
|
System.out.println("Clé envoyé");
|
||||||
|
System.out.println(Arrays.toString(key.getEncoded()));
|
||||||
|
|
||||||
} catch (UnknownHostException e) {
|
} catch (UnknownHostException e) {
|
||||||
System.out.println("Destination unknown");
|
System.out.println("Destination unknown");
|
||||||
System.exit(-1);
|
System.exit(-1);
|
||||||
@@ -32,24 +44,28 @@ public class Client {
|
|||||||
System.exit(-1);
|
System.exit(-1);
|
||||||
}
|
}
|
||||||
|
|
||||||
String message = null;
|
// Communication
|
||||||
|
String message;
|
||||||
|
byte[] messageCrypte;
|
||||||
|
try {
|
||||||
do {
|
do {
|
||||||
//ecoute d'un message
|
// Envoi du message
|
||||||
if (ecoute) {
|
|
||||||
message = in.readLine();
|
|
||||||
System.out.printf("serveur > %s\n", message);
|
|
||||||
} else {
|
|
||||||
System.out.print("client > ");
|
System.out.print("client > ");
|
||||||
message = scan.nextLine();
|
message = scan.nextLine();
|
||||||
out.println(message);
|
messageCrypte = AES.encrypter(message, key);
|
||||||
}
|
out.writeObject(messageCrypte);
|
||||||
ecoute = !ecoute;
|
|
||||||
|
// Reception du message
|
||||||
|
messageCrypte = (byte[]) in.readObject();
|
||||||
|
message = AES.decrypter(messageCrypte, key);
|
||||||
|
System.out.printf("serveur > %s -> %s\n", new String(messageCrypte, StandardCharsets.UTF_8), message);
|
||||||
} while (!message.equals("bye"));
|
} while (!message.equals("bye"));
|
||||||
|
|
||||||
//deconnexion
|
|
||||||
//fermeture des Sockets
|
|
||||||
out.close();
|
out.close();
|
||||||
in.close();
|
in.close();
|
||||||
serverSocket.close();
|
serverSocket.close();
|
||||||
|
}catch (IOException | ClassNotFoundException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+25
-24
@@ -1,39 +1,30 @@
|
|||||||
package reseau;
|
package reseau;
|
||||||
|
|
||||||
import javax.crypto.*;
|
import javax.crypto.*;
|
||||||
import java.nio.file.Files;
|
import java.io.*;
|
||||||
import java.nio.file.Path;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.security.*;
|
import java.security.*;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
|
||||||
public class AES {
|
public class AES {
|
||||||
|
|
||||||
public static byte[] genererCle() {
|
public static Key genererCle() {
|
||||||
byte[] data;
|
Key key = null;
|
||||||
byte[] result;
|
|
||||||
byte[] original = null;
|
|
||||||
try {
|
try {
|
||||||
KeyGenerator kg = KeyGenerator.getInstance("DES");
|
KeyGenerator keyGenerator = KeyGenerator.getInstance("DES");
|
||||||
Key key = kg.generateKey();
|
key = keyGenerator.generateKey();
|
||||||
return key.getEncoded();
|
|
||||||
// Cipher cipher = Cipher.getInstance("DES") ;
|
|
||||||
// cipher.init(Cipher.ENCRYPT_MODE, key) ;
|
|
||||||
// data = "Hello World".getBytes() ;
|
|
||||||
// result = cipher.doFinal(data) ;
|
|
||||||
// cipher.init(Cipher.DECRYPT_MODE, key) ;
|
|
||||||
// original = cipher.doFinal(result) ;
|
|
||||||
// System.out.println("Decrypted data :" + Arrays.toString(original)) ;
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
return original;
|
return key;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static byte[] decrypter(byte[] msg, Key key) {
|
public static String decrypter(byte[] msg, Key key) {
|
||||||
try {
|
try {
|
||||||
Cipher cipher = Cipher.getInstance("DES") ;
|
Cipher cipher = Cipher.getInstance("DES") ;
|
||||||
cipher.init(Cipher.DECRYPT_MODE, key);
|
cipher.init(Cipher.DECRYPT_MODE, key);
|
||||||
return cipher.doFinal(msg);
|
return new String(cipher.doFinal(msg), StandardCharsets.UTF_8);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
@@ -51,12 +42,22 @@ public class AES {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void sauvegarderFichier(Key key) {
|
public static void sauvegarderCle(Key key) {
|
||||||
Path fileName = Path.of("./cle.txt");
|
|
||||||
Files.writeString(fileName, new String(key));
|
try (ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream("key.ser"))) {
|
||||||
|
objectOutputStream.writeObject(key);
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static Key chargerCle() {
|
||||||
|
Key key = null;
|
||||||
|
try (ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream("key.ser"))) {
|
||||||
|
key = (Key) objectInputStream.readObject();
|
||||||
|
} catch (IOException | ClassNotFoundException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
return key;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+30
-20
@@ -1,8 +1,13 @@
|
|||||||
package serveur;
|
package serveur;
|
||||||
|
|
||||||
|
import reseau.AES;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.net.ServerSocket;
|
import java.net.ServerSocket;
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.security.Key;
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.Scanner;
|
import java.util.Scanner;
|
||||||
|
|
||||||
public class Serveur {
|
public class Serveur {
|
||||||
@@ -13,6 +18,7 @@ public class Serveur {
|
|||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
ServerSocket serverSocket = null;
|
ServerSocket serverSocket = null;
|
||||||
Socket clientSocket = null;
|
Socket clientSocket = null;
|
||||||
|
Key clientKey;
|
||||||
|
|
||||||
// Connexion
|
// Connexion
|
||||||
try {
|
try {
|
||||||
@@ -26,30 +32,34 @@ public class Serveur {
|
|||||||
// Un client a été trouvé
|
// Un client a été trouvé
|
||||||
System.out.println("Client connecté");
|
System.out.println("Client connecté");
|
||||||
|
|
||||||
boolean connectee = true;
|
ObjectInputStream in;
|
||||||
boolean reception = true;
|
ObjectOutputStream out;
|
||||||
|
|
||||||
BufferedReader in = null;
|
|
||||||
|
|
||||||
PrintWriter out = null;
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
|
in = new ObjectInputStream(clientSocket.getInputStream());
|
||||||
|
out = new ObjectOutputStream(clientSocket.getOutputStream());
|
||||||
|
|
||||||
out = new PrintWriter(clientSocket.getOutputStream(), true);
|
// On récupère la clé du client
|
||||||
|
clientKey = (Key) in.readObject();
|
||||||
|
|
||||||
String message = null;
|
System.out.println("Clé reçue");
|
||||||
|
System.out.println(Arrays.toString(clientKey.getEncoded()));
|
||||||
|
|
||||||
|
// Communication
|
||||||
|
String message;
|
||||||
|
byte[] messageCrypte;
|
||||||
do {
|
do {
|
||||||
if (reception) {
|
// Envoi du message
|
||||||
message = in.readLine();
|
messageCrypte = (byte[]) in.readObject();
|
||||||
System.out.printf("client > %s\n", message);
|
message = AES.decrypter(messageCrypte, clientKey);
|
||||||
} else {
|
System.out.printf("client > %s -> %s\n", new String(messageCrypte, StandardCharsets.UTF_8), message);
|
||||||
|
|
||||||
|
|
||||||
|
// Reception du message
|
||||||
System.out.print("serveur > ");
|
System.out.print("serveur > ");
|
||||||
message = scan.nextLine();
|
message = scan.nextLine();
|
||||||
out.println(message);
|
messageCrypte = AES.encrypter(message, clientKey);
|
||||||
}
|
out.writeObject(messageCrypte);
|
||||||
|
|
||||||
reception = !reception;
|
|
||||||
} while (!message.equals("bye"));
|
} while (!message.equals("bye"));
|
||||||
|
|
||||||
in.close();
|
in.close();
|
||||||
@@ -59,8 +69,8 @@ public class Serveur {
|
|||||||
|
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
} catch (ClassNotFoundException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user