-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.java
More file actions
160 lines (139 loc) · 4.34 KB
/
Server.java
File metadata and controls
160 lines (139 loc) · 4.34 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.*;
public class Server {
private Socket sk;
private BufferedReader in;
private PrintWriter out;
private int e, n;
private int d, cn;
public Server() throws IOException {
System.out.println("Waiting for client connection...");
ServerSocket ss = new ServerSocket(7796);
sk = ss.accept();
System.out.println("Client connected.");
in = new BufferedReader(new InputStreamReader(sk.getInputStream()));
out = new PrintWriter(sk.getOutputStream(), true);
initRSAKeys();
exchangeKeys();
readMsg();
writeMsg();
}
private void initRSAKeys() {
Random rnd = new Random();
int p = genPrime(rnd);
int q = genPrime(rnd);
cn = p * q;
int phi = lcm(p - 1, q - 1);
List<Integer> coPrimes = new ArrayList<>();
for (int i = 3; i < phi; i++) {
if (gcd(i, phi) == 1) coPrimes.add(i);
}
e = coPrimes.get(rnd.nextInt(coPrimes.size()));
d = modInv(e, phi);
}
private void exchangeKeys() throws IOException {
String[] cKey = in.readLine().split(",");
int clientE = Integer.parseInt(cKey[0]);
int clientN = Integer.parseInt(cKey[1]);
out.println(e + "," + cn);
e = clientE;
n = clientN;
}
private void readMsg() {
new Thread(() -> {
System.out.println("Reader ready.");
while (true) {
try {
String encMsg = in.readLine();
if (encMsg == null || encMsg.equalsIgnoreCase("exit")) {
System.out.println("Chat closed.");
break;
}
String decMsg = decrypt(encMsg);
System.out.println("Client: " + decMsg);
} catch (IOException ex) {
ex.printStackTrace();
}
}
}).start();
}
private void writeMsg() {
new Thread(() -> {
System.out.println("Writer ready.");
try (BufferedReader userIn = new BufferedReader(new InputStreamReader(System.in))) {
while (true) {
String msg = userIn.readLine();
String encMsg = encrypt(msg);
out.println(encMsg);
out.flush();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}).start();
}
private String encrypt(String msg) {
StringBuilder enc = new StringBuilder();
for (char c : msg.toCharArray()) {
enc.append(modExp(c, e, n)).append(" ");
}
return enc.toString().trim();
}
private String decrypt(String encMsg) {
StringBuilder dec = new StringBuilder();
for (String part : encMsg.split(" ")) {
dec.append((char) modExp(Integer.parseInt(part), d, cn));
}
return dec.toString();
}
public static void main(String[] args) {
System.out.println("Server started...");
try {
new Server();
} catch (IOException ex) {
ex.printStackTrace();
}
}
private int modExp(int base, int exp, int mod) {
long res = 1, pow = base % mod;
while (exp > 0) {
if ((exp & 1) == 1) res = (res * pow) % mod;
pow = (pow * pow) % mod;
exp >>= 1;
}
return (int) res;
}
private int modInv(int a, int m) {
int m0 = m, x = 1, y = 0;
while (a > 1) {
int q = a / m, t = m;
m = a % m;
a = t;
t = y;
y = x - q * y;
x = t;
}
return x < 0 ? x + m0 : x;
}
private int gcd(int a, int b) {
return b == 0 ? a : gcd(b, a % b);
}
private int lcm(int a, int b) {
return (a * b) / gcd(a, b);
}
private boolean isPrime(int n) {
if (n <= 1) return false;
for (int i = 2; i <= Math.sqrt(n); i++) {
if (n % i == 0) return false;
}
return true;
}
private int genPrime(Random rnd) {
while (true) {
int num = rnd.nextInt(100) + 50;
if (isPrime(num)) return num;
}
}
}