find RC632 key patters in files
[perl-Mifare-MAD.git] / mifare_rc632_find_key.c
1 /*
2
3 Find rc632 keys encoded as 12 bytes for flash writing
4 Dobrica Pavlinusic <dpavlin@rot13.org> 2014-01-22
5
6 compile with:
7
8 gcc -o mifare_rc632_find_key mifare_rc632_find_key.c
9
10 try it out:
11
12 mifare_rc632_find_key some_binary_file_with_keys
13
14 */
15
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <stdbool.h>
19
20 int main ( int argc, char *argv[] ) {
21         FILE *fd;
22         char *filename;
23
24         filename = argv[1];
25
26         fd = fopen(filename,"rb");
27         if ( fd == NULL ) {
28                 printf("error opening %s", filename);
29                 exit(1);
30         }
31
32         fseek(fd, 0, SEEK_END);
33         long size = ftell(fd);
34         fseek(fd, 0, SEEK_SET);
35
36         char *str = malloc( size + 1 );
37         fread(str, size, 1, fd);
38         fclose(fd);
39
40         int keys_found = 0;
41
42         int i;
43         for( i = 0; i <= size; i++ ) {
44
45                 bool found = true;
46                 int j;
47                 char key[12];
48
49                 for ( j = 0; j <= 11; j++ ) {
50                         char c = str[i + j];
51
52                         if ( ( ( ( c & 0xf0 ) ^ 0xf0 ) >> 4 ) != ( c & 0x0f ) ) {
53                                 found = false;
54                                 break;
55                         }
56                         key[j] = c;
57                 }
58                 if ( found ) {
59                         printf("%08x: ", i);
60                         for( j = 0; j <= 11; j++ ) {
61                                 printf("%01x", (unsigned char)key[j] & 0x0f);
62                         }
63                         printf(" %s\n", filename);
64                         keys_found++;
65                 }
66
67         }
68 }
69