@@ -39,44 +39,59 @@ def stop(self):
39
39
def get_key_hex_ascii (self ):
40
40
if not self .is_cracked ():
41
41
raise Exception ('Cracked file not found' )
42
+
42
43
with open (self .cracked_file , 'r' ) as fid :
43
44
hex_raw = fid .read ()
44
- hex_key = ''
45
+
46
+ return self ._hex_and_ascii_key (hex_raw )
47
+
48
+ @staticmethod
49
+ def _hex_and_ascii_key (hex_raw ):
50
+ hex_chars = []
45
51
ascii_key = ''
46
- while len (hex_raw ) > 0 :
47
- # HEX
48
- if hex_key != '' :
49
- hex_key += ':'
50
- hex_key += hex_raw [0 :2 ]
51
-
52
- # ASCII
53
- # Convert hex to decimal
54
- code = int (hex_raw [0 :2 ], 16 )
55
- if code < 32 or code > 127 :
56
- # Hex key is non-printable in ascii
57
- ascii_key = None
58
- continue
59
- elif ascii_key is None :
60
- # We can't generate an Ascii key
61
- continue
62
- # Convert decimal to char
63
- ascii_key += chr (code )
64
-
65
- # Trim first two characters
66
- hex_raw = hex_raw [2 :]
67
- continue
52
+ for index in xrange (0 , len (hex_raw ), 2 ):
53
+ byt = hex_raw [index :index + 2 ]
54
+ hex_chars .append (byt )
55
+ byt_int = int (byt , 16 )
56
+ if byt_int < 32 or byt_int > 127 or ascii_key is None :
57
+ ascii_key = None # Not printable
58
+ else :
59
+ ascii_key += chr (byt_int )
60
+
61
+ hex_key = ':' .join (hex_chars )
68
62
69
63
return (hex_key , ascii_key )
70
64
65
+ def __del__ (self ):
66
+ if os .path .exists (self .cracked_file ):
67
+ os .remove (self .cracked_file )
68
+
71
69
if __name__ == '__main__' :
70
+ (hexkey , asciikey ) = Aircrack ._hex_and_ascii_key ('A1B1C1D1E1' )
71
+ assert hexkey == 'A1:B1:C1:D1:E1' , 'hexkey was "%s", expected "A1:B1:C1:D1:E1"' % hexkey
72
+ assert asciikey is None , 'asciikey was "%s", expected None' % asciikey
73
+
74
+ (hexkey , asciikey ) = Aircrack ._hex_and_ascii_key ('6162636465' )
75
+ assert hexkey == '61:62:63:64:65' , 'hexkey was "%s", expected "61:62:63:64:65"' % hexkey
76
+ assert asciikey == 'abcde' , 'asciikey was "%s", expected "abcde"' % asciikey
77
+
72
78
from time import sleep
79
+
73
80
Configuration .initialize (False )
74
- a = Aircrack ('tests/files/wep-crackable.ivs' )
75
- while a .is_running ():
81
+
82
+ ivs_file = 'tests/files/wep-crackable.ivs'
83
+ print "Running aircrack on %s ..." % ivs_file
84
+
85
+ aircrack = Aircrack (ivs_file )
86
+ while aircrack .is_running ():
76
87
sleep (1 )
77
- if a .is_cracked ():
78
- print "cracked!"
79
- print '(hex, ascii) =' , a .get_key_hex_ascii ()
80
- else :
81
- print "Not cracked"
88
+
89
+ assert aircrack .is_cracked (), "Aircrack should have cracked %s" % ivs_file
90
+ print "aircrack process completed."
91
+
92
+ (hexkey , asciikey ) = aircrack .get_key_hex_ascii ()
93
+ print "aircrack found HEX key: (%s) and ASCII key: (%s)" % (hexkey , asciikey )
94
+ assert hexkey == '75:6E:63:6C:65' , 'hexkey was "%s", expected "75:6E:63:6C:65"' % hexkey
95
+ assert asciikey == 'uncle' , 'asciikey was "%s", expected "uncle"' % asciikey
96
+
82
97
Configuration .exit_gracefully (0 )
0 commit comments