-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainServerImpl.java
169 lines (115 loc) · 3.73 KB
/
MainServerImpl.java
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
161
162
163
164
165
166
167
168
169
package server;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.rmi.MarshalledObject;
import java.rmi.RemoteException;
import java.rmi.activation.Activatable;
import java.rmi.activation.ActivationID;
//import java.util.ArrayList;
import javax.rmi.ssl.SslRMIClientSocketFactory;
import javax.rmi.ssl.SslRMIServerSocketFactory;
/**
* Implementazione del server centrale.
*
* @author Pietro Musoni
* @author Carlo Tacchella
*/
public class MainServerImpl extends Activatable implements MainServerIIOP,
MainServerJRMP {
/**
*
*/
private static final long serialVersionUID = 1L;
private BigInteger[] PublicKey = new BigInteger[2];
public MainServerImpl(ActivationID id, MarshalledObject<?> data)
throws Exception {
super(id, 35000, new SslRMIClientSocketFactory(),
new SslRMIServerSocketFactory());
System.out.println("\nMainServer esportato.");
}
/* controllare che l'array risultante da getArrayPrimi
sia vuoto per ogni client dell'hashmap, altrimenti abbiamo ottenuto p e q*/
private BigInteger[] getPublicKey(BigInteger[] ArrayPQ) throws RemoteException{
System.out.println("è entrato in getPublic...");
final BigInteger[] PublicKey = new BigInteger[2];
final BigInteger temp1 = ArrayPQ[0].subtract(BigInteger.ONE);
final BigInteger temp2 = ArrayPQ[1].subtract(BigInteger.ONE);
final BigInteger temp = temp1.multiply(temp2);
PublicKey[0] = ArrayPQ[0].multiply(ArrayPQ[1]);
BigInteger Random = BigInteger.valueOf(2); //random grande ma non troppo!!!
while (!Euclide(Random, temp) && Random.compareTo(PublicKey[0]) == 1) {
Random.add(BigInteger.ONE);
}
PublicKey[1] = Random;
return PublicKey;
}
public String[] LeggiChiave() throws IOException {
String[] chiave = new String[2];
//String givenPassword = CharBuffer.wrap(passwordCercata).toString();
try {
FileReader f;
f=new FileReader(".listaChiavi.txt");
BufferedReader b;
b=new BufferedReader(f);
chiave[0]=b.readLine();
chiave[1]=b.readLine();
b.close();
f.close();
} catch(FileNotFoundException e) {
return null;
}
return chiave;
}
public void scriviChiave(BigInteger[] Primi) throws IOException{
PublicKey = getPublicKey(Primi);
try {
newFileMsg();
FileOutputStream fos = new FileOutputStream (".listaChiavi.txt", true); // append
PrintWriter pw = new PrintWriter (fos);
pw.print (PublicKey[0]+"\n" +PublicKey[1]);
pw.close ();
System.out.println("Hai fatto il file con la chiave\n");
} catch (FileNotFoundException e) {
System.out.println("Impossibile aggiungere il messaggio.");
e.printStackTrace();
} catch (RemoteException e) {
System.out.println("Impossibile ricevere la chiave pubblica.");
}
}
public void newFileMsg() throws RemoteException {
String path = ".listaChiavi.txt";
try {
File file = new File(path);
if (file.exists()){
file.delete();
file.createNewFile();
}
else {
file.createNewFile();
System.out.println("Il file " + path + " è stato creato");
}
}
catch (IOException e) {
e.printStackTrace();
}
}
private static boolean Euclide(BigInteger Random, BigInteger temp) {
BigInteger r;
r = Random.mod(temp);
while(!r.equals(BigInteger.ZERO)) {
Random = temp;
temp = r;
r = Random.mod(temp);
}
if(temp.equals(BigInteger.ONE))
return true;
else
return false;
}
}