1
1
from .crack import bruteforce , brute_freq
2
- from .utils import write_file , analyse_text
2
+ from .utils import write_file , analyse_text , get_text
3
3
from .crypt import encrypt , decrypt
4
4
from colorama import Fore , Style
5
5
import re
6
6
7
7
8
8
def bruteforce_handler (args ):
9
- c_value = ' ' .join (args .bruteforce )
10
- cracked = []
11
- if args .sourceFile :
12
- f = open (c_value , 'r' ).read ()
13
- cracked = bruteforce (f )
14
- else :
15
- cracked = bruteforce (c_value )
9
+ # Get all possibilities of the cipher text
10
+ cracked = bruteforce (get_text (args , 'bruteforce' ))
16
11
# Write to the file,
17
12
# If an output path specified
18
13
if args .output :
@@ -24,18 +19,9 @@ def bruteforce_handler(args):
24
19
25
20
def encrypt_handler (args ):
26
21
# Extract key as an int from args
27
- key = int ('' .join (map (str , args .key )))
28
- key *= - 1 if args .backwards else 1
29
- # Get plain text
30
- e_value = ' ' .join (args .encrypt )
31
- # If source presents,
32
- # file content will be encrypted
33
- cipher = ''
34
- if args .sourceFile :
35
- f = open (e_value , 'r' ).read ()
36
- cipher = encrypt (f , key )
37
- else :
38
- cipher = encrypt (e_value , key )
22
+ key = int ('' .join (map (str , args .key ))) * - 1 if args .backwards else 1
23
+ # Get Cipher text
24
+ cipher = encrypt (get_text (args , 'encrypt' ), key )
39
25
# Write to the file,
40
26
# If an output path specified
41
27
if args .output :
@@ -46,18 +32,9 @@ def encrypt_handler(args):
46
32
47
33
def decrypt_handler (args ):
48
34
# Extract key as an int from args
49
- key = int ('' .join (map (str , args .key )))
50
- key *= - 1 if args .backwards else 1
51
- # Get Cipher text
52
- d_value = ' ' .join (args .decrypt )
53
- # If source presents,
54
- # file content will be decrypted
55
- plain = ''
56
- if args .sourceFile :
57
- f = open (d_value , 'r' ).read ()
58
- plain = decrypt (f , key )
59
- else :
60
- plain = decrypt (d_value , key )
35
+ key = int ('' .join (map (str , args .key ))) * - 1 if args .backwards else 1
36
+ # Get plain text
37
+ plain = decrypt (get_text (args , 'decrypt' ), key )
61
38
# Write to the file,
62
39
# If an output path specified
63
40
if args .output :
@@ -70,10 +47,8 @@ def freq_analysis_handler(args):
70
47
# Check for missing arguments
71
48
if not args .wordlist :
72
49
raise ValueError ("No Wordlist Found" )
73
- text = ''
74
- a_value = ' ' .join (args .freq_analysis )
75
50
# Filter letters, numbers and spaces
76
- text = re .sub (r'[^A-Za-z0-9 ]+' , '' , a_value )
51
+ text = re .sub (r'[^A-Za-z0-9 ]+' , '' , get_text ( args , 'freq_analysis' ) )
77
52
# Analyse the frequency
78
53
matches = analyse_text (text , '' .join (args .wordlist ))
79
54
avg = float (matches ) / len (text .split (' ' )) * 100
@@ -84,11 +59,14 @@ def freq_analysis_handler(args):
84
59
85
60
86
61
def brute_freq_handler (args ):
87
- c_value = ' ' .join (args .brute_frequency )
62
+ # Check for missing arguments
63
+ if not args .wordlist :
64
+ raise ValueError ("No Wordlist Found" )
88
65
# Get cipher text
89
- cipher = open ( c_value , 'r' ). read () if args . sourceFile else c_value
66
+ cipher = get_text ( args , 'brute_frequency' )
90
67
# Get analysed disctionary
91
- analyzed = brute_freq (cipher , '' .join (args .wordlist ))
68
+ bruteforced = brute_freq (cipher , '' .join (args .wordlist ))
69
+ analyzed = bruteforced [0 ]
92
70
# Write to the file,
93
71
# If an output path specified
94
72
if args .output :
@@ -105,7 +83,8 @@ def brute_freq_handler(args):
105
83
found = False
106
84
for i in analyzed :
107
85
if analyzed [i ] > 60 :
108
- print (f'{ Fore .GREEN } { round (analyzed [i ], 1 )} %{ Style .RESET_ALL } : { i } ' )
86
+ print (
87
+ f'{ Fore .GREEN } { round (analyzed [i ], 1 )} %{ Style .RESET_ALL } : { Fore .CYAN } { list (bruteforced [1 ].keys ()).index (i )} { Style .RESET_ALL } : { i } ' )
109
88
found = True
110
89
if not found :
111
90
print (
0 commit comments