mirror of
https://github.com/LucasVbr/mini-chat.git
synced 2026-05-14 01:31:54 +00:00
Envoi avec Message Crypté
This commit is contained in:
+42
-26
@@ -1,29 +1,41 @@
|
||||
package client;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.PrintWriter;
|
||||
import java.io.*;
|
||||
import java.net.Socket;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.rmi.UnknownHostException;
|
||||
import java.security.Key;
|
||||
import java.util.Arrays;
|
||||
import java.util.Scanner;
|
||||
|
||||
import reseau.AES;
|
||||
|
||||
public class Client {
|
||||
|
||||
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;
|
||||
PrintWriter out = null;
|
||||
BufferedReader in = null;
|
||||
boolean ecoute = false;
|
||||
ObjectOutputStream out = null;
|
||||
ObjectInputStream in = null;
|
||||
|
||||
// Clé de chiffrage
|
||||
Key key = AES.genererCle();
|
||||
AES.sauvegarderCle(key);
|
||||
|
||||
// Création des Sockets
|
||||
try {
|
||||
serverSocket = new Socket("localhost", 4444);
|
||||
System.out.println("Connecté au serveur");
|
||||
out = new PrintWriter(serverSocket.getOutputStream(), true);
|
||||
in = new BufferedReader(new InputStreamReader(serverSocket.getInputStream()));
|
||||
out = new ObjectOutputStream(serverSocket.getOutputStream());
|
||||
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) {
|
||||
System.out.println("Destination unknown");
|
||||
System.exit(-1);
|
||||
@@ -32,24 +44,28 @@ public class Client {
|
||||
System.exit(-1);
|
||||
}
|
||||
|
||||
String message = null;
|
||||
do {
|
||||
//ecoute d'un message
|
||||
if (ecoute) {
|
||||
message = in.readLine();
|
||||
System.out.printf("serveur > %s\n", message);
|
||||
} else {
|
||||
// Communication
|
||||
String message;
|
||||
byte[] messageCrypte;
|
||||
try {
|
||||
do {
|
||||
// Envoi du message
|
||||
System.out.print("client > ");
|
||||
message = scan.nextLine();
|
||||
out.println(message);
|
||||
}
|
||||
ecoute = !ecoute;
|
||||
} while (!message.equals("bye"));
|
||||
messageCrypte = AES.encrypter(message, key);
|
||||
out.writeObject(messageCrypte);
|
||||
|
||||
//deconnexion
|
||||
//fermeture des Sockets
|
||||
out.close();
|
||||
in.close();
|
||||
serverSocket.close();
|
||||
// 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"));
|
||||
|
||||
out.close();
|
||||
in.close();
|
||||
serverSocket.close();
|
||||
}catch (IOException | ClassNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+25
-24
@@ -1,39 +1,30 @@
|
||||
package reseau;
|
||||
|
||||
import javax.crypto.*;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.io.*;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.*;
|
||||
import java.util.Arrays;
|
||||
|
||||
public class AES {
|
||||
|
||||
public static byte[] genererCle() {
|
||||
byte[] data;
|
||||
byte[] result;
|
||||
byte[] original = null;
|
||||
public static Key genererCle() {
|
||||
Key key = null;
|
||||
|
||||
try {
|
||||
KeyGenerator kg = KeyGenerator.getInstance("DES");
|
||||
Key key = kg.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)) ;
|
||||
KeyGenerator keyGenerator = KeyGenerator.getInstance("DES");
|
||||
key = keyGenerator.generateKey();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return original;
|
||||
return key;
|
||||
}
|
||||
|
||||
public static byte[] decrypter(byte[] msg, Key key) {
|
||||
public static String decrypter(byte[] msg, Key key) {
|
||||
try {
|
||||
Cipher cipher = Cipher.getInstance("DES") ;
|
||||
cipher.init(Cipher.DECRYPT_MODE, key);
|
||||
return cipher.doFinal(msg);
|
||||
return new String(cipher.doFinal(msg), StandardCharsets.UTF_8);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
@@ -51,12 +42,22 @@ public class AES {
|
||||
return null;
|
||||
}
|
||||
|
||||
public static void sauvegarderFichier(Key key) {
|
||||
Path fileName = Path.of("./cle.txt");
|
||||
Files.writeString(fileName, new String(key));
|
||||
public static void sauvegarderCle(Key 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;
|
||||
|
||||
import reseau.AES;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.Key;
|
||||
import java.util.Arrays;
|
||||
import java.util.Scanner;
|
||||
|
||||
public class Serveur {
|
||||
@@ -13,6 +18,7 @@ public class Serveur {
|
||||
public static void main(String[] args) {
|
||||
ServerSocket serverSocket = null;
|
||||
Socket clientSocket = null;
|
||||
Key clientKey;
|
||||
|
||||
// Connexion
|
||||
try {
|
||||
@@ -26,30 +32,34 @@ public class Serveur {
|
||||
// Un client a été trouvé
|
||||
System.out.println("Client connecté");
|
||||
|
||||
boolean connectee = true;
|
||||
boolean reception = true;
|
||||
|
||||
BufferedReader in = null;
|
||||
|
||||
PrintWriter out = null;
|
||||
ObjectInputStream in;
|
||||
ObjectOutputStream out;
|
||||
|
||||
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 {
|
||||
if (reception) {
|
||||
message = in.readLine();
|
||||
System.out.printf("client > %s\n", message);
|
||||
} else {
|
||||
System.out.print("serveur > ");
|
||||
message = scan.nextLine();
|
||||
out.println(message);
|
||||
}
|
||||
// Envoi du message
|
||||
messageCrypte = (byte[]) in.readObject();
|
||||
message = AES.decrypter(messageCrypte, clientKey);
|
||||
System.out.printf("client > %s -> %s\n", new String(messageCrypte, StandardCharsets.UTF_8), message);
|
||||
|
||||
reception = !reception;
|
||||
|
||||
// Reception du message
|
||||
System.out.print("serveur > ");
|
||||
message = scan.nextLine();
|
||||
messageCrypte = AES.encrypter(message, clientKey);
|
||||
out.writeObject(messageCrypte);
|
||||
} while (!message.equals("bye"));
|
||||
|
||||
in.close();
|
||||
@@ -59,8 +69,8 @@ public class Serveur {
|
||||
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} catch (ClassNotFoundException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user