· 7 years ago · Oct 25, 2018, 03:00 PM
1package p;
2
3import java.util.concurrent.TimeUnit;
4
5import javax.crypto.*;
6import javax.crypto.spec.*;
7
8public class prueba {
9
10 static String alg = "HmacSHA1";
11
12 public static void main(String[] args) {
13 prueba();
14 }
15
16 public static void prueba() {
17 long startTime = System.currentTimeMillis();
18 for(int a=0;a<=255;a++) {
19 for(int b=0;b<=255;b++) {
20 for(int c=0;c<=255;c++) {
21 for(int d=0;d<=255;d++) {
22 String msg = "231456789 487654 500";
23 byte[] decodedKey = {(byte) a, (byte) b, (byte) c, (byte) d};
24 String resumen = performMACTest(msg, decodedKey);
25
26 if(resumen.equals("1abe3f04cf4c96f6e94b7b81ef021c0a973e8983")) {
27 System.out.println("Mensaje : " + msg);
28 System.out.println("Clave Byte : "+ decodedKey + "\t\tClave Hex : "+ byteArrayToHexString(decodedKey) + "\t\tString : " + new String(decodedKey));
29 System.out.println("HMAC generada : " + resumen);
30
31 long endTime = TimeUnit.MILLISECONDS.toMinutes(System.currentTimeMillis() - startTime);
32 System.out.println("Valores de los bytes: " + (byte)a + "-" + (byte)b + "-" + (byte)c + "-" + (byte)d);
33 System.out.println("Ha tardado "+endTime+" minutos en conseguir la clave.");
34 System.exit(0);
35 }
36 }
37 }
38 }
39 }
40 }
41
42 public static String performMACTest(String s, byte[] decodedKey) {
43 String st = "";
44 try {
45 Mac mac = Mac.getInstance(alg);
46 SecretKey key = new SecretKeySpec(decodedKey, 0, decodedKey.length,
47 alg);
48 mac.init(key);
49 mac.update(s.getBytes());
50 byte[] b = mac.doFinal();
51 st = byteArrayToHexString(b);
52 } catch (Exception e) {
53 System.out.println(e.getMessage());
54 }
55 return st;
56 }
57
58 private static String[] hexDigits = { "0", "1", "2", "3", "4", "5", "6",
59 "7", "8", "9", "a", "b", "c", "d", "e", "f" };
60
61 // Convierte un byte a una cadena hexadecimal
62 public static String byteToHexString(byte b) {
63 int n = b;
64 if (n < 0)
65 n = 256 + n;
66 int d1 = n / 16;
67 int d2 = n % 16;
68 return hexDigits[d1] + hexDigits[d2];
69 }
70
71 // Convierte un array de bytes a una cadena hexadecimal
72 public static String byteArrayToHexString(byte[] b) {
73 String result = "";
74 for (int i = 0; i < b.length; ++i)
75 result += byteToHexString(b[i]);
76 return result;
77 }
78}