mirror of
https://github.com/LucasVbr/mini-chat.git
synced 2026-05-14 01:31:54 +00:00
Chat client serveur non securisé
This commit is contained in:
+26
-13
@@ -6,18 +6,24 @@ import java.io.InputStreamReader;
|
||||
import java.io.PrintWriter;
|
||||
import java.net.Socket;
|
||||
import java.rmi.UnknownHostException;
|
||||
import java.util.Scanner;
|
||||
|
||||
public class Client {
|
||||
|
||||
public static final Scanner scan = new Scanner(System.in);
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
Socket echoSocket = null;
|
||||
Socket serverSocket = null;
|
||||
PrintWriter out = null;
|
||||
BufferedReader in = null;
|
||||
boolean ecoute = false;
|
||||
|
||||
// Création des Sockets
|
||||
try {
|
||||
echoSocket = new Socket("localhost", 4444);
|
||||
serverSocket = new Socket("localhost", 4444);
|
||||
System.out.println("Connecté au serveur");
|
||||
out = new PrintWriter(echoSocket.getOutputStream(), true);
|
||||
in = new BufferedReader(new InputStreamReader(echoSocket.getInputStream()));
|
||||
out = new PrintWriter(serverSocket.getOutputStream(), true);
|
||||
in = new BufferedReader(new InputStreamReader(serverSocket.getInputStream()));
|
||||
} catch (UnknownHostException e) {
|
||||
System.out.println("Destination unknown");
|
||||
System.exit(-1);
|
||||
@@ -26,17 +32,24 @@ public class Client {
|
||||
System.exit(-1);
|
||||
}
|
||||
|
||||
BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
|
||||
String userInput;
|
||||
|
||||
while ((userInput = stdIn.readLine()) != null) {
|
||||
out.println(userInput);
|
||||
System.out.println("echo: " + userInput);
|
||||
}
|
||||
String message = null;
|
||||
do {
|
||||
//ecoute d'un message
|
||||
if (ecoute) {
|
||||
message = in.readLine();
|
||||
System.out.printf("serveur > %s\n", message);
|
||||
} else {
|
||||
System.out.print("client > ");
|
||||
message = scan.nextLine();
|
||||
out.println(message);
|
||||
}
|
||||
ecoute = !ecoute;
|
||||
} while (!message.equals("bye"));
|
||||
|
||||
//deconnexion
|
||||
//fermeture des Sockets
|
||||
out.close();
|
||||
in.close();
|
||||
stdIn.close();
|
||||
echoSocket.close();
|
||||
serverSocket.close();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,62 @@
|
||||
package reseau;
|
||||
|
||||
import javax.crypto.*;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.security.*;
|
||||
import java.util.Arrays;
|
||||
|
||||
public class AES {
|
||||
|
||||
public static byte[] genererCle() {
|
||||
byte[] data;
|
||||
byte[] result;
|
||||
byte[] original = 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)) ;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return original;
|
||||
}
|
||||
|
||||
public static byte[] decrypter(byte[] msg, Key key) {
|
||||
try {
|
||||
Cipher cipher = Cipher.getInstance("DES") ;
|
||||
cipher.init(Cipher.DECRYPT_MODE, key);
|
||||
return cipher.doFinal(msg);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static byte[] encrypter(String msg, Key key){
|
||||
try {
|
||||
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 void sauvegarderFichier(Key key) {
|
||||
Path fileName = Path.of("./cle.txt");
|
||||
Files.writeString(fileName, new String(key));
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
}
|
||||
}
|
||||
+35
-27
@@ -3,56 +3,64 @@ package serveur;
|
||||
import java.io.*;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
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;
|
||||
try {
|
||||
serverSocket = new ServerSocket(4444);
|
||||
} catch (IOException e) {
|
||||
System.out.println("Could not listen on port 4444");
|
||||
System.exit(-1);
|
||||
}
|
||||
|
||||
Socket clientSocket = null;
|
||||
|
||||
// Connexion
|
||||
try {
|
||||
clientSocket = serverSocket.accept();
|
||||
serverSocket = new ServerSocket(PORT); // Crée le serveur
|
||||
clientSocket = serverSocket.accept(); // On recherche un client
|
||||
} catch (IOException e) {
|
||||
System.out.println("Accept failed on port 4444");
|
||||
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é");
|
||||
boolean quitte = false;
|
||||
|
||||
boolean connectee = true;
|
||||
boolean reception = true;
|
||||
|
||||
DataOutputStream envoi = null;
|
||||
BufferedReader recevoir = null;
|
||||
BufferedReader in = null;
|
||||
|
||||
PrintWriter out = null;
|
||||
|
||||
try {
|
||||
OutputStream outputStream = clientSocket.getOutputStream();
|
||||
envoi = new DataOutputStream(outputStream);
|
||||
recevoir = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
|
||||
in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
|
||||
|
||||
while (!quitte) {
|
||||
out = new PrintWriter(clientSocket.getOutputStream(), true);
|
||||
|
||||
String message = null;
|
||||
do {
|
||||
if (reception) {
|
||||
String message = null;
|
||||
while (message == null){
|
||||
message = recevoir.readLine();
|
||||
}
|
||||
while(message != null){
|
||||
System.out.println(message);
|
||||
message= recevoir.readLine();
|
||||
}
|
||||
|
||||
message = in.readLine();
|
||||
System.out.printf("client > %s\n", message);
|
||||
} else {
|
||||
System.out.print("serveur > ");
|
||||
message = scan.nextLine();
|
||||
out.println(message);
|
||||
}
|
||||
|
||||
reception = !reception;
|
||||
}
|
||||
} while (!message.equals("bye"));
|
||||
|
||||
in.close();
|
||||
out.close();
|
||||
clientSocket.close();
|
||||
serverSocket.close();
|
||||
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user