Key Entropy ExampleKey Entropy Defines the strength of a key. The results are then:
|
Formula
\(\begin{equation}Entropy = \frac{log(Phrases)}{log(2)} \end{equation}\)Coding
The following is some code:
import sys import math import array hex_string = "00 01 02 03 04 05 06 07" if (len(sys.argv)>1): hex_string=sys.argv[1] hex_string=hex_string.replace(' ','') hex_data = bytearray.fromhex(hex_string) byteArr = array.array('B', hex_data) fileSize = len(byteArr) print('File size in bytes: ' + str(fileSize)) print() # calculate the frequency of each byte value in the file freqList = [] for b in range(256): ctr = 0 for byte in byteArr: if byte == b: ctr += 1 freqList.append(float(ctr) / fileSize) # Shannon entropy ent = 0.0 for freq in freqList: if freq > 0: ent = ent + freq * math.log(freq, 2) ent = -ent print('Shannon entropy (min bits per byte-character): ' + str(ent)) print() print('Min possible file size: ' + str((ent * fileSize)), ' bits') print('Min possible file size: ' + str((ent * fileSize)/8), ' bytes') print("") val = ent*100/8 print("Efficency: "+"%.4f" % val+"% (where 100% is the maximum)") print() print('Frequencies of each byte-character:') print(freqList)
Theory
Go to 42:31 for key entropy