-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGreetingServer.java
More file actions
129 lines (105 loc) · 4.76 KB
/
GreetingServer.java
File metadata and controls
129 lines (105 loc) · 4.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import java.net.*;
import java.util.*;
import java.io.*;
public class GreetingServer {
static double clientkey;
static int modu(int a, int e, int n) {
int r = 1;
for (int i = 1; i <= e; i++)
r = ((r % n) * (a % n)) % n;
return r;
}
public static void main(String[] args) throws IOException {
try {
int port = 8088;
// Server Key private key
int privateKey = (new Random()).nextInt(6);
// prime
int p = 23;
// alpha
int g = 10;
// public for server//
int serverPublicKey = ((int) (Math.pow(g, privateKey)) % p); // calculation of server public key
// Client p, g, and key
int clientPublicKey, clientG, clientF;
int secretKey;
// Established the Connection
while (true) {
ServerSocket serverSocket = new ServerSocket(port);
System.out.println("Waiting for client on port " + serverSocket.getLocalPort() + "...");
Socket server = serverSocket.accept();
DataInputStream in = new DataInputStream(server.getInputStream());
// seding public key to client//
OutputStream outToclient = server.getOutputStream();
DataOutputStream out = new DataOutputStream(outToclient);
out.writeUTF(String.valueOf(serverPublicKey)); // Sending server public key
clientPublicKey = Integer.parseInt(in.readUTF());// receiving client Public
// generate secret key/
secretKey = ((int) (Math.pow(clientPublicKey, privateKey) % p)); // calculation of Bdash
System.out.println("Secret key " + secretKey);
// get f and g from client//
clientF = Integer.parseInt(in.readUTF()); // to accept f
System.out.println("From Client : f = " + clientF);
clientG = Integer.parseInt(in.readUTF()); // to accept g
System.out.println("From Client : G = " + clientG);
// find e and n//
int e = clientF - secretKey;
int n = clientG - secretKey;
System.out.print("Enter the message: ");
Scanner scan = new Scanner(System.in);
String message = scan.nextLine();
System.out.println("");
String chipher = "";
// encrypt this message//
for (int i = 0; i < message.length(); i++) {
int m = message.charAt(i);
// System.out.println(m);
char ch = (char) GreetingServer.modu(m, e, n);
chipher += ch;
}
// sending ecrypted message//
System.out.println("Encrypted:-" + chipher);
out.writeUTF(chipher);
// find e,n//
/*
* System.out.println("Just connected to " + server.getRemoteSocketAddress());
*
* // Server's Private Key System.out.println("From Server : Private Key = " +
* b);
*
* // Accepts the data from client DataInputStream in = new
* DataInputStream(server.getInputStream());
*
* clientP = Integer.parseInt(in.readUTF()); // to accept p
* System.out.println("From Client : P = " + clientP);
*
* clientG = Integer.parseInt(in.readUTF()); // to accept g
* System.out.println("From Client : G = " + clientG);
*
* clientA = Double.parseDouble(in.readUTF()); // to accept A
*
* System.out.println("From Client : Public Key = " + clientA);
*
* B = ((Math.pow(clientG, b)) % clientP); // calculation of B Bstr =
* Double.toString(B);
*
* // Sends data to client // Value of B OutputStream outToclient =
* server.getOutputStream(); DataOutputStream out = new
* DataOutputStream(outToclient);
*
* out.writeUTF(Bstr); // Sending B
*
* Bdash = ((Math.pow(clientA, b)) % clientP); // calculation of Bdash
*
* clientkey = Bdash;
* System.out.println("Secret Key to perform Symmetric Encryption = " + Bdash);
* // server.close();
*/
}
}
catch (SocketTimeoutException s) {
System.out.println("Socket timed out!");
} catch (IOException e) {
}
}
}