-
Notifications
You must be signed in to change notification settings - Fork 0
/
freq_an.py
executable file
·102 lines (75 loc) · 2.23 KB
/
freq_an.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#!/usr/bin/env python
# Title : Cryptanalysis of Caesar Cipher
# Author : Zweisamkeit
# Created : 07/28/2016
# Last modification : 08/01/2016
# Contact : contact@zweisamkeit.fr
# License : GNU/GPL
# Frequential Analysis
# Errors are managed by the bash wrapper
# To use this program alone, please uncomment the commented line which correspond to error messages
import sys
if (len(sys.argv)!=2):
# print("Arg error: ./freq_an.py [cipherfile]")
exit(1)
try:
cipherfile=open(sys.argv[1],"r")
except:
# print("Error: No such file directory")
exit(2)
ciphertext=cipherfile.read()
frequencies=[]
length=len(ciphertext)
alphabet="abcdefghijklmnopqrstuvwxyz"
# Experimental frequencies
for i in alphabet:
frequencies.append([i,0])
for i in range (0,len(frequencies)):
frequencies[i][1]=ciphertext.lower().count(frequencies[i][0])/float(length)*100
#frequencies.sort(key=lambda frequencies:frequencies[1],reverse=True)
# Theorical frequencies
try:
conf_file=open("dceasar.conf","r")
except:
# print("Error: No such file directory")
exit(2)
conf_lines=conf_file.readlines()
aux=0
for line in conf_lines: # manage error: no config
if (line[0] != "#"):
aux=1
break
if (aux==0): # Error: No line unsharped
# print("Conf error: No unsharped line")
exit(3)
elif (line.find("=")==-1): # Bad format
# print("Conf error: Bad format")
exit(4)
else:
theorical_frequencies=eval(line.split("=")[1])
# Error: Bad format
if (not (isinstance(theorical_frequencies, list)) or (len(theorical_frequencies)!=26)):
# print("Conf error: Bad format")
exit(4)
else:
for i in range (0,26):
if not ((isinstance(theorical_frequencies[i],float))):
# print("Conf error: Bad format")
exit(4)
# Differencies between theorical frequencies and experimental frequencies for each possible shift
averadges_diff=[]
for i in range (0,26):
tmp_differencies=[]
for j in range (0,26):
tmp_differencies.append(abs(theorical_frequencies[j]-(frequencies[(j+i)%26][1])))
averadges_diff.append(sum(tmp_differencies)/len(tmp_differencies))
# Lower difference = Most probable shift
min=0
for i in range (1,26):
if (averadges_diff[i]<averadges_diff[min]):
min=i
probable_shift=min
cipherfile.close()
conf_file.close()
print(probable_shift)
exit(0)