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