· 7 years ago · Nov 15, 2018, 02:32 PM
1package com.company;
2//Serwer
3
4import javafx.application.Application;
5import javafx.stage.Stage;
6import java.io.*;
7import java.math.BigInteger;
8import java.net.ServerSocket;
9import java.net.Socket;
10import java.security.SecureRandom;
11
12
13public class Main extends Application {
14
15 @Override
16 public void start(Stage primaryStage) throws Exception{
17 }
18
19 public static void main(String[] args)throws Exception {
20
21 System.out.println("Serwer bangla! :D");
22
23 ServerSocket socket = new ServerSocket(8080);
24 Socket connectionSocket = socket.accept();
25 ObjectOutputStream outToClient = new ObjectOutputStream(connectionSocket.getOutputStream());
26 ObjectInputStream inFromClient = new ObjectInputStream(connectionSocket.getInputStream());
27
28 //DH
29 BigInteger p = (BigInteger)inFromClient.readObject();
30 BigInteger g = (BigInteger)inFromClient.readObject();
31 BigInteger b= new BigInteger(90,new SecureRandom());
32 BigInteger B= g.modPow(b,p);
33 outToClient.writeObject(B);
34 BigInteger A = (BigInteger)inFromClient.readObject();
35 BigInteger s = A.modPow(b,p);
36 //System.out.println(s);
37
38 final String secretKey = "pw"+s;
39 String binary = (String)inFromClient.readObject();
40 socket.close();
41 String encry = AES.decrypt(binary, secretKey);
42 System.out.println("Takie czary: " + binary);
43 String text = new String(new BigInteger(encry, 2).toByteArray());
44 System.out.println("Odczarowano: " + text);
45 }
46}