-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathfreq_analysis.py
257 lines (182 loc) · 6.21 KB
/
freq_analysis.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#
# #
# .d8888b. d8b 888 888 .d8888b. #
# d88P Y88b Y8P 888 888 d88P Y88b #
# 888 888 888 888 Y88b. #
# 888 888 88888b. 88888b. .d88b. 888d888 "Y888b. .d88b. 88888b. .d8888b .d88b. #
# 888 88888 888 888 "88b 888 "88b d8P Y8b 888P" "Y88b. d8P Y8b 888 "88b 88K d8P Y8b #
# 888 888 888 888 888 888 888 88888888 888 "888 88888888 888 888 "Y8888b. 88888888 #
# Y88b d88P 888 888 d88P 888 d88P Y8b. 888 Y88b d88P Y8b. 888 888 X88 Y8b. #
# "Y8888P88 888 88888P" 88888P" "Y8888 888 "Y8888P" "Y8888 888 888 88888P' "Y8888 #
# #
#
# (beta)
#
#
# Plugin Name : freq_analysis.py
# Plugin ID : fqa-01
# Plugin Purpose : Provides frequency analysis of a file contents.
# Used to analyse subsitution cipher.
#
# Plugin Author : @ajithatti
#
# Plugin Version : 0.0.1
# Plugin Status : beta
#
################################################################################
# Include Section
import os.path
import getopt
import os
import re
import sys
from sys import argv, stdout
from PIL import Image
import random
# --------------------------------------------------------------------------------
# Function : usage
#
# Purpose : Proivde a mini help on usage of this program
#
# Parameters : None
#
# Returns : None
def usage(script):
print "\n\n %s [-o] [-i] " %script
print "\n Purpose : Computes the frequency distribution each alphabet"
print " present in a file. Used for crypt analysis of "
print " common subsitution ciphers of English text"
print "\n"
print " -o [--out] : stores the frequency distribution table in a givne file"
print " -i [--in] : path of a file to be analyzed"
print " \n\n\n"
sys.exit(2)
pass # USAGE BLOCK
# --------------------------------------------------------------------------------
# Function : usage
#
# Purpose : Proivde a mini help on usage of this program
#
# Parameters : None
#
# Returns : None
def log( message, sink) :
sink.write(message)
pass
buf = ""
# --------------------------------------------------------------------------------
# Function : usage
#
# Purpose : Proivde a mini help on usage of this program
#
# Parameters : None
#
# Returns : None
def file_process(in_file):
f = open(in_file, "r")
buf = f.read()
return buf
pass
# --------------------------------------------------------------------------------
# Function : usage
#
# Purpose : Proivde a mini help on usage of this program
#
# Parameters : None
#
# Returns : None
def print_table(table, in_file, sink):
log( "\n\t\t\tFrequencey Distribution", sink)
log("\n\t\t\t=======================", sink)
log("\n\n", sink)
outstr = "\n\tFile Name : "+ in_file + "\n\n"
log(outstr, sink)
i = 65
while(i < 123):
outstr = "\n\t"+ chr(i)+ " => "+ str(table[i])
log( outstr, sink)
i += 1
if(i == 91) :
i = 97
pass
m = max(table)
c = table.index(m)
outstr = "\n\n\tHighest Freq of [" +chr(c)+ "] : "+ str(m)
log(outstr, sink)
dist = (101 - c)
outstr= "\n\n\tThe distance from 'e' is "+ str(dist)
log(outstr, sink)
return dist
pass
# --------------------------------------------------------------------------------
# Function : usage
#
# Purpose : Proivde a mini help on usage of this program
#
# Parameters : None
#
# Returns : None
def freq_analysis(in_file, sink):
buf = file_process(in_file)
l = len(buf)
table = [0] * 255
i = 0
while (i < l) :
c = ord(buf[i])
if( (c >= 65 and c <=90) or (c>= 97 and c <= 122)):
table[c] +=1
i +=1
pass
print_table(table, in_file, sink)
pass
# --------------------------------------------------------------------------------
# Function : main
#
# Purpose : Frequency analysis of contents of a file
#
# Parameters : in - path of a file to be analyzed
# out - file in which frequency distribution is to be stored
#
# Returns : dist - Distance of hights frequency alphabet from 'e'
def main(argv):
# Initialize the variables
in_file = ""
out_file = ""
this_script = argv[0]
sink = stdout
if len(argv) < 3:
usage(this_script)
sys.exit(2)
pass # if block
# --- try block
try:
opts, args = getopt.getopt(argv[1:], "o:i:",["out=", "in="])
except getopt.GetoptError:
usage(this_script)
sys.exit(2)
pass # TRY BLOCK
#---- Process the arguments passed to the script
for opt, arg in opts:
if opt in ("-i", "--in"):
in_file = arg
elif opt in ("-o", "--out"):
out_file = arg
pass # IF BLOCK
pass # FOR BLOCK
if( out_file != ""):
sink = open (out_file, "w")
pass
if( in_file == ""):
print "\n File name for frequency analysis is not specified, Exiting \n\n"
return (-2)
pass
dist = freq_analysis( in_file, sink)
return dist
print "\n\n"
pass # main
#-------------------------------------------------------------------------------
#
# Kick off the script
if __name__ == "__main__":
main (sys.argv[0:])
pass # IF BLOCK