Skip to content

Commit df032fc

Browse files
committed
feat: Analyze frequency of a msg with a wordlist
1 parent d4e3e09 commit df032fc

File tree

4 files changed

+308513
-2
lines changed

4 files changed

+308513
-2
lines changed

project/__main__.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from .handlers import bruteforce_handler, decrypt_handler, encrypt_handler
1+
from .handlers import bruteforce_handler, decrypt_handler, encrypt_handler, freq_analysis_handler
22
import argparse
33

44
parser = argparse.ArgumentParser(
@@ -25,6 +25,12 @@
2525
parser.add_argument('-br', '--bruteforce', nargs='+',
2626
metavar='PLAIN', help='Cipher text to crack', type=str)
2727

28+
# -fr & --freq-analysis
29+
parser.add_argument('-fr', '--freq-analysis', nargs='+', metavar='CIPHER', help='Cipher text to crack by analysing', type=str)
30+
31+
# -w & --wordlist
32+
parser.add_argument('-w', '--wordlist', nargs=1, metavar='WORDLIST', help='Path to the wordlist containing language words', type=str)
33+
2834
args = parser.parse_args()
2935

3036

@@ -33,6 +39,8 @@ def handle_args():
3339
# If key isn't in the args,
3440
if not args.key and (args.encrypt or args.decrypt):
3541
raise NameError("Key is not defined")
42+
elif args.freq_analysis:
43+
freq_analysis_handler(args)
3644
# If the bruteforce flag used
3745
elif args.bruteforce:
3846
bruteforce_handler(args)

project/handlers.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from .crack import bruteforce
2-
from .utils import write_file
2+
from .utils import write_file, analyse_text
33
from .crypt import encrypt, decrypt
4+
import re
45

56

67
def bruteforce_handler(args):
@@ -62,3 +63,15 @@ def decrypt_handler(args):
6263
write_file(plain, args.output)
6364
else:
6465
print("Decrypted: " + plain)
66+
67+
68+
def freq_analysis_handler(args):
69+
text = ''
70+
a_value = ' '.join(args.freq_analysis)
71+
text = re.sub(r'[^A-Za-z0-9 ]+', '', a_value)
72+
matches = analyse_text(text, ''.join(args.wordlist))
73+
l = len(text.split(' '))
74+
avg = float(matches) / len(text.split(' ')) * 100
75+
if avg > 80:
76+
print('True')
77+
print('False')

project/utils.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,21 @@ def write_file(text, path):
1111
f.write("%s\n" % item)
1212
return
1313
f.write(text)
14+
15+
16+
def get_wordlist(path):
17+
WORDLIST = []
18+
words = open(path, 'r').read().split('\n')
19+
for word in words:
20+
WORDLIST.append(word.upper())
21+
return WORDLIST
22+
23+
24+
def analyse_text(text, wordlist_path):
25+
wordlist = get_wordlist(wordlist_path)
26+
text = text.upper()
27+
matches = 0
28+
for word in text.split(' '):
29+
if word in wordlist:
30+
matches += 1
31+
return matches

0 commit comments

Comments
 (0)