diff --git a/worksheet3/Entropy.py b/worksheet3/Entropy.py index ff9a3b4..b129728 100644 --- a/worksheet3/Entropy.py +++ b/worksheet3/Entropy.py @@ -1,33 +1,44 @@ +# +# Project 2 +# +# 01435 Practiacal Cryptanalysis +# Technical University of Denmark + +# Markus Faerevaag (s123692@student.dtu.dk) +# Christian Mathias Rohde Kiaer (s123812@student.dtu.dk) +# Jonathan Becktor (s123094@student.dtu.dk) +# + import math -letters=['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'] -sum =0 -def find_perc(s): +LETTERS = ['A','B','C','D','E', + 'F','G','H','I','J', + 'K','L','M','N','O', + 'P','Q','R','S','T', + 'U','V','W','X','Y','Z'] +PERC = [0.082, 0.015, 0.028, 0.043, 0.127, + 0.022, 0.02, 0.061, 0.07, 0.002, + 0.008, 0.04, 0.024, 0.067, 0.075, + 0.019, 0.001, 0.060, 0.063, 0.091, + 0.028, 0.01, 0.023, 0.001, 0.02, 0.001] - perc=[0.082,0.015,0.028,0.043,0.127,0.022,0.02,0.061,0.07,0.002,0.008,0.04,0.024,0.067,0.075,0.019,0.001,0.060,0.063,0.091,0.028,0.01,0.023,0.001,0.02,0.001] - letter=['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'] - pos = 0 - temp = s.upper() - if temp in letter: - for x in xrange(1,len(letter)): - if temp==letter[x]: - pos = x - return perc[pos] +def calc_entropy(letter): + """Calculates entropy for given letter""" + P = PERC[LETTERS.index(letter.upper())] + return P * math.log(1/P)/math.log(2) -def calc_ent(s): - P=find_perc(s) - sum=0 - temp = math.log(1/P)/math.log(2) - #temp = (-P*(math.log(P)/math.log(2)))-((1-P)*(math.log(1-P)/math.log(2))) - sum=P*temp - print sum - return sum +def main(): + # Sum entropy for each letter + sum = 0 + for letter in LETTERS: + entropy = calc_entropy(letter) + sum += entropy + print "%s: %.3f" % (letter, entropy) + print "The average number of bits: %f" % sum -for x in xrange(0,25): - sum=sum+calc_ent(letters[x]) - print x -print "The min bit is : %f"%sum \ No newline at end of file +if __name__ == '__main__': + main() diff --git a/worksheet3/README.md b/worksheet3/README.md new file mode 100644 index 0000000..8345027 --- /dev/null +++ b/worksheet3/README.md @@ -0,0 +1,177 @@ +Worksheet 3 +=========== + +Script for calculating the average number of bits required to store one letter of British English if perfect compression. + + +## Usage + +Simply: + + $ python Entropy.py + + +## Logic + +Sum the entropy of each character in the alphabet using the given +probability in the slides. + + +## Results + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
LetterProbabilityEntropy
A0.0820.296
B0.0150.091
C0.0280.144
D0.0430.195
E0.1270.378
F0.0220.121
G0.020.113
H0.0610.246
I0.07,0.269
J0.0020.018
K0.0080.056
L0.040.186
M0.0240.129
N0.0670.261
O0.0750.280
P0.0190.109
Q0.0010.010
R0.0600.244
S0.0630.251
T0.0910.315
U0.0280.144
V0.010.066
W0.0230.125
X0.0010.010
Y0.020.113
Z0.0010.010
Total4.180245
+
+ +Results can be found in `results.csv`. + + +## Further Help + +For further help or explanation please contact one of us by mail and +we'll be happy to help: + + * Markus Faerevaag [s123692@student.dtu.dk](mailto:s123692@student.dtu.dk) + * Christian Mathias Rohde Kiaer [s123812@student.dtu.dk](mailto:s123812@student.dtu.dk) + * Jonathan Becktor [s123094@student.dtu.dk](mailto:s123094@student.dtu.dk) diff --git a/worksheet3/results.csv b/worksheet3/results.csv new file mode 100644 index 0000000..1079767 --- /dev/null +++ b/worksheet3/results.csv @@ -0,0 +1,26 @@ +A,0.296 +B,0.091 +C,0.144 +D,0.195 +E,0.378 +F,0.121 +G,0.113 +H,0.246 +I,0.269 +J,0.018 +K,0.056 +L,0.186 +M,0.129 +N,0.261 +O,0.280 +P,0.109 +Q,0.010 +R,0.244 +S,0.251 +T,0.315 +U,0.144 +V,0.066 +W,0.125 +X,0.010 +Y,0.113 +Z,0.010 \ No newline at end of file