· 5 years ago · May 14, 2020, 04:18 AM
1 public Cliente() {
2 try {
3 key = Seguridad.llaves();
4 certif = Seguridad.getCertificado(key).getEncoded();
5 } catch (CertificateEncodingException e) {
6 System.out.println("No se creó la llave");
7 e.printStackTrace();
8 }
9 }
10
11
12
13 public static void main(String[] args) {
14
15 try {
16 Scanner sn = new Scanner(System.in);
17 InetAddress addr = InetAddress.getByName("localhost");
18 System.out.println("Iniciando Cliente con seguridad\nIngrese el puerto del servidor: ");
19 int port = Integer.parseInt(sn.nextLine());
20 SocketAddress sockaddr = new InetSocketAddress(addr, port);
21 Socket sc = new Socket();
22
23 sc.connect(sockaddr);
24 Cliente cl = new Cliente();
25
26 // Iniciación
27 PrintWriter pw = new PrintWriter(sc.getOutputStream(), true);
28 BufferedReader bf = new BufferedReader(new InputStreamReader(sc.getInputStream()));
29
30
31 System.out.println("Comienza la conexion");
32 // HOLA
33 String consola;
34
35 pw.println("HOLA");
36 String lectura = bf.readLine();
37 if (!lectura.equals("OK")) {
38 System.out.println("Recibio " + lectura);
39 pw.println("ERROR");
40 sc.close();
41 }
42 // ALGS
43 System.out.println("Escriba los algoritmos que va a usar:");
44 String a1 = sn.nextLine();
45 String a2 = sn.nextLine();
46 String a3 = sn.nextLine();
47 consola = "ALGORITMOS" + ":" + a1 + ":" + a2 + ":" + a3;
48 pw.println(consola);
49 System.out.println(consola);
50 lectura = bf.readLine();
51 if (!lectura.equals("OK")) {
52 System.out.println("Recibio " + lectura);
53 pw.println("ERROR");
54 sc.close();
55 }
56 System.out.println("Algoritmos OK");
57 // CERTIFICADOS
58
59 pw.println(cl.toHexString(cl.certif));
60
61 lectura = bf.readLine();
62 if (!lectura.equals("OK")) {
63 System.out.println("Recibio " + lectura);
64 pw.println("ERROR");
65 sc.close();
66 }
67
68 lectura = bf.readLine();
69 byte[] certfBytes = new byte['?'];
70 certfBytes = cl.toByteArray(lectura);
71 CertificateFactory factory = CertificateFactory.getInstance("X.509");
72 InputStream in = new ByteArrayInputStream(certfBytes);
73 X509Certificate certificadoS = (X509Certificate) factory.generateCertificate(in);
74 pw.println("OK");
75 System.out.println("Certificados OK");
76
77 // Llave simetrica
78 lectura = bf.readLine();
79 byte[] simByEnc = new byte['?'];
80 simByEnc = cl.toByteArray(lectura);
81 byte[] simB = Seguridad.desencriptarAsimetrico(simByEnc, cl.key.getPrivate(), a2);
82 SecretKey sim = new SecretKeySpec(simB, a1);
83
84 lectura = bf.readLine();
85 if (!lectura.equals("OK")) {
86 System.out.println("Recibio " + lectura);
87 }
88 System.out.println("Llave OK");
89
90 // Reto
91 byte[] reto = Seguridad.desencriptarSimetrico(toByteArray(lectura), sim, a1);
92
93 String dice = toHexString(reto);
94
95 byte[] retoB = toByteArray(dice);
96
97 System.out.println("Aquí está el reto: "+dice);
98
99 byte[] retoPub = Seguridad.encriptarAsimetrico(retoB, certificadoS.getPublicKey(), a2);
100
101 pw.println(toHexString(retoPub));
102
103 lectura = bf.readLine();
104 if (!lectura.equals("OK")) {
105 System.out.println("Recibio " + lectura);
106 pw.println("ERROR");
107 sc.close();
108 }
109 String idUsuario = "1234";
110 byte[] id = Seguridad.encriptarSimetrico(toByteArray(idUsuario), sim, a1);
111 System.out.println("Envio id " + lectura);
112 pw.println(toHexString(id));
113
114 // Hora militar
115 lectura = bf.readLine();
116 byte[] horaCif = Seguridad.desencriptarSimetrico(toByteArray(lectura), sim, a1);
117 String hora = toHexString(horaCif);
118
119 System.out.println("Hora recibida: " + hora );
120
121 pw.println("OK");
122 System.out.println("Final OK");
123 // Final
124 sc.close();
125 sn.close();
126 pw.close();
127 bf.close();
128 } catch (IOException | CertificateException | InvalidKeyException | NoSuchAlgorithmException
129 | NoSuchPaddingException | IllegalBlockSizeException | BadPaddingException e) {
130 e.printStackTrace();
131 }
132 }
133
134 public static String toHexString(byte[] array) {
135 return DatatypeConverter.printBase64Binary(array);
136 }
137
138 public static byte[] toByteArray(String s) {
139 return DatatypeConverter.parseBase64Binary(s);
140 }
141
142 private int numeroCuenta() {
143 return (int) (Math.random() * (9999 - 1000)) + 1000;
144 }
145
146
147 public void comenzarTransaccion(String ip, int id, int puerto, String algo1, String algo2, String algo3) throws Exception {
148 //Scanner sn = new Scanner(System.in);
149 InetAddress addr = InetAddress.getByName(ip);
150 //System.out.println("Iniciando Cliente sin seguridad\nIngrese el puerto del servidor: ");
151 //int port = Integer.parseInt(sn.nextLine());
152 int port = puerto;
153 SocketAddress sockaddr = new InetSocketAddress(addr, port);
154 Socket sc = new Socket();
155
156 sc.connect(sockaddr);
157 Cliente cl = new Cliente();
158
159 // Iniciación
160 PrintWriter pw = new PrintWriter(sc.getOutputStream(), true);
161 BufferedReader bf = new BufferedReader(new InputStreamReader(sc.getInputStream()));
162
163
164 System.out.println("Comienza la conexion con: " + ip + ":" + puerto);
165 // HOLA
166 String consola;
167
168 pw.println("HOLA");
169
170 //------------------------------------
171 //START CONSULTA
172 //------------------------------------
173
174
175 String lectura = bf.readLine();
176 if (!lectura.equals("OK")) {
177 System.out.println("Recibio " + lectura);
178 pw.println("ERROR");
179 sc.close();
180 throw new Exception("ERROR");
181 }
182
183 // ALGS
184 //System.out.println("¿Que algoritmos desea usar ?");
185 // String a1 = sn.nextLine();
186 // String a2 = sn.nextLine();
187 // String a3 = sn.nextLine();
188 String a1 = algo1;
189 String a2 = algo2;
190 String a3 = algo3;
191 consola = "ALGORITMOS" + ":" + a1 + ":" + a2 + ":" + a3;
192 pw.println(consola);
193 //System.out.println(consola);
194 lectura = bf.readLine();
195 if (!lectura.equals("OK")) {
196 System.out.println("Recibio " + lectura);
197 pw.println("ERROR");
198 sc.close();
199 throw new Exception("ERROR");
200 }
201 //System.out.println("Algoritmos OK");
202 // CERTIFICADOS
203 startLectura = System.nanoTime();
204 pw.println(cl.toHexString(cl.certif));
205
206 lectura = bf.readLine();
207 if (!lectura.equals("OK")) {
208 System.out.println("Recibio " + lectura);
209 pw.println("ERROR");
210 sc.close();
211 throw new Exception("ERROR");
212 }
213
214 lectura = bf.readLine();
215
216 //------------------------------------
217 //START VERIFICACION
218 //------------------------------------
219
220 startVer = System.nanoTime();
221
222 byte[] certfBytes = new byte['?'];
223 certfBytes = cl.toByteArray(lectura);
224 CertificateFactory factory = CertificateFactory.getInstance("X.509");
225 InputStream in = new ByteArrayInputStream(certfBytes);
226 X509Certificate certificadoS = (X509Certificate) factory.generateCertificate(in);
227 pw.println("OK");
228 //System.out.println("Certificados OK");
229
230
231
232 // LLAVE SIMETRICA
233 lectura = bf.readLine();
234 byte[] simByEnc = new byte['?'];
235 simByEnc = cl.toByteArray(lectura);
236 byte[] simB = Seguridad.desencriptarAsimetrico(simByEnc, cl.key.getPrivate(), a2);
237 SecretKey sim = new SecretKeySpec(simB, a1);
238
239 byte[] simBE2 = Seguridad.encriptarAsimetrico(sim.getEncoded(), certificadoS.getPublicKey(), a2);
240
241 lectura = bf.readLine();
242 if (!lectura.equals("OK")) {
243 System.out.println("Recibio " + lectura);
244 pw.println("ERROR");
245 sc.close();
246 throw new Exception("ERROR");
247 }
248 //System.out.println("Llave OK");
249
250 //------------------------------------
251 //END VERIFICACION
252 //------------------------------------
253
254
255 endVer = System.nanoTime();
256
257 // Reto
258 byte[] reto = Seguridad.desencriptarSimetrico(toByteArray(lectura), sim, a1);
259
260 String dice = toHexString(reto);
261
262 byte[] retoB = toByteArray(dice);
263
264 System.out.println("Aquí está el reto: "+dice);
265
266 byte[] retoPub = Seguridad.encriptarAsimetrico(reto, certificadoS.getPublicKey(), a2);
267
268 pw.println(toHexString(retoPub));
269
270 lectura = bf.readLine();
271 if (!lectura.equals("OK")) {
272 System.out.println("Recibio " + lectura);
273 pw.println("ERROR");
274 sc.close();
275 }
276 String idUsuario = "1234";
277 byte[] idU = Seguridad.encriptarSimetrico(toByteArray(idUsuario), sim, a1);
278 System.out.println("Envio id " + lectura);
279 pw.println(toHexString(idU));
280
281 // Hora militar
282 lectura = bf.readLine();
283 byte[] horaCif = Seguridad.desencriptarSimetrico(toByteArray(lectura), sim, a1);
284 String hora = toHexString(horaCif);
285
286 System.out.println("Hora recibida: " + hora );
287
288 pw.println("OK");
289 System.out.println("Final OK");
290 // Final
291 sc.close();
292 //sn.close();
293 pw.close();
294 bf.close();
295 //------------------------------------
296 //END LECTURA
297 //------------------------------------
298 endLectura = System.nanoTime();
299
300 System.out.println("Final OK");
301
302
303
304
305 // Final
306 sc.close();
307 //sn.close();
308 pw.close();
309 bf.close();
310
311 }