· 7 years ago · Nov 20, 2018, 03:50 PM
1
2package m09.psp.raul;
3
4import java.security.MessageDigest;
5import java.util.Arrays;
6import java.util.Scanner;
7
8import javax.crypto.Cipher;
9import javax.crypto.SecretKey;
10import javax.crypto.spec.SecretKeySpec;
11
12public class M09_UF1_PT1_MARTINEZ_RAUL {
13 public static void main(String[] args) {
14
15
16
17 Scanner scan = new Scanner(System.in);
18 int numeroIntroducido;
19 String mensajeAencriptar;
20
21 System.out.println("Escribe el mensaje que deseas encriptar");
22 mensajeAencriptar=scan.nextLine();
23
24 System.out.println("Dame un numero para el password del 1 al 99");
25 numeroIntroducido=scan.nextInt();
26
27
28 if(numeroIntroducido>=1 || numeroIntroducido<=99) {
29
30 //passwordKeyGeneration(numeroIntroducido,128);
31
32 try {
33
34 Cipher cipher= Cipher.getInstance("AES");
35 cipher.init(Cipher.ENCRYPT_MODE, passwordKeyGeneration(numeroIntroducido,192));
36
37 byte[] texto = mensajeAencriptar.getBytes("UTF-8");
38 byte[] cipherText = cipher.doFinal(texto);
39
40 System.out.println("El mensaje encriptado es : 'visualizamos la instancia("+cipherText+")'");
41
42
43
44 guessPassword(cipherText);
45
46 //desciframos con Cipher
47// cipher.init(Cipher.DECRYPT_MODE, passwordKeyGeneration(numeroIntroducido,128));
48// System.out.println("***********");
49// byte[] bytes = cipher.doFinal(cipherText);
50// String descifrado = new String(bytes, "UTF-8");
51// System.out.println("Desciframos:"+descifrado);
52
53 }catch(Exception e){
54
55 e.printStackTrace();
56 }
57
58 }
59
60 }
61
62
63 public static SecretKey passwordKeyGeneration(int numeroIntroducido, int keySize) {
64 SecretKey sKey = null;
65 String cadena;
66 if(numeroIntroducido>=1 && numeroIntroducido<=9) {
67 cadena = 0+Integer.toString(numeroIntroducido);
68 }else {
69 cadena = Integer.toString(numeroIntroducido);
70 }
71
72 if ((keySize == 128)||(keySize == 192)||(keySize == 256)) {
73 try {
74 //coge el texto y lo convierte en un array de bytes.
75 byte[] data = cadena.getBytes("UTF-8");
76 MessageDigest md = MessageDigest.getInstance("SHA-256");
77 byte[] hash = md.digest(data); //las siguientes dos lÃnies podemos resumir(comprimir) el texto para aconseguir una cantidad limitada de bytes,
78 byte[] key = Arrays.copyOf(hash, keySize/8); //copyOf genera una array llamada key de 16 posiciones dependiendo del tamaño del keySize(128,192,256)
79 sKey = new SecretKeySpec(key, "AES");
80 } catch (Exception ex) {
81 System.err.println("Error generant la clau:" + ex);
82 }
83 }
84 return sKey;
85 }
86
87
88 public static void guessPassword(byte[] interceptor) {
89 System.out.println("Cercant clau...");
90 //Es generen totes les possibles contrasenyes i es va provant
91 for (int num = 0; num <= 99; num++) {
92 String pass = Integer.toString(num);
93 while (pass.length() < 2) {
94 pass = "0" + pass;
95 }
96 //Generació de la clau a partir de la possible contrasenya
97 SecretKey sKey2 = null;
98 try {
99 byte[] data = pass.getBytes("UTF-8");
100 MessageDigest md = MessageDigest.getInstance("SHA-256");
101 byte[] hash = md.digest(data);
102 byte[] key = Arrays.copyOf(hash, 192/8);
103 sKey2 = new SecretKeySpec(key, "AES");
104 } catch (Exception ex) {
105 System.err.println("No s'ha pogut generar la clau:" + ex);
106 }
107 //Desxifrat AES
108 if (sKey2 != null) {
109 try {
110 Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
111 cipher.init(Cipher.DECRYPT_MODE, sKey2);
112 // byte[] data = cipher.doFinal(enc); ///////////////////////////////
113 byte[] data = cipher.doFinal(interceptor); ///////////////////////////////
114 //S'ha desxifrat alguna cosa, però serà un text llegible?
115 System.out.print("Possible clau trobada: " + pass + " => ");
116 System.out.println(new String(data));
117
118 String descifrado = new String(data, "UTF-8");
119 System.out.println("Desciframos:"+descifrado);
120 } catch (Exception ex) {
121 //Si hi ha error, segur que la clau no val
122
123 }
124 }
125 }
126
127 }
128
129
130}