forked from Th4nat0s/Chall_Tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xlentro.py
executable file
·56 lines (48 loc) · 1.16 KB
/
xlentro.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/usr/bin/python
import sys
import math
from sets import Set
# v 0.1
# Copyleft Thanat0s
# http://Thanat0s.trollprod.org
#
# Licence GNU GPL
def shan(stin,pkey):
fstList = list(stin)
falphabet = list(Set(fstList))
ffreqList = []
for fsymbol in falphabet:
fctr = 0
for fsym in fstList:
if fsym == fsymbol:
fctr += 1
ffreqList.append(float(fctr) / len(fstList))
fent = 0.0
for ffreq in ffreqList:
fent = fent + ffreq * math.log(ffreq, 2)
fent = -fent
print 'KEYSIZE:', pkey,
print 'BIT:',
print int(math.ceil(fent)),
print 'SE:',
print fent
return fent
if len(sys.argv) != 2:
print 'Find lenght of the xor key using shannon entropy'
print 'To Use: '+ sys.argv[0]+ ' filename'
sys.exit()
file = open(sys.argv[1], 'rb')
filename = sys.argv[1]
byteArr = bytearray(file.read())
file.close()
fileSize = len(byteArr)
maxkeylen=33
if fileSize < 33:
maxkeylen=fileSize
for keylen in range (1,maxkeylen):
index = 0
st=''
for byte in range( 0, fileSize / keylen ):
st = st + chr(byteArr[index])
index = index + keylen
shan(st,keylen)