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