11from .crack import bruteforce , brute_freq
2- from .utils import write_file , analyse_text
2+ from .utils import write_file , analyse_text , get_text
33from .crypt import encrypt , decrypt
44from colorama import Fore , Style
55import re
66
77
88def 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' ))
1611 # Write to the file,
1712 # If an output path specified
1813 if args .output :
@@ -24,18 +19,9 @@ def bruteforce_handler(args):
2419
2520def encrypt_handler (args ):
2621 # 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 )
3925 # Write to the file,
4026 # If an output path specified
4127 if args .output :
@@ -46,18 +32,9 @@ def encrypt_handler(args):
4632
4733def decrypt_handler (args ):
4834 # 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 )
6138 # Write to the file,
6239 # If an output path specified
6340 if args .output :
@@ -70,10 +47,8 @@ def freq_analysis_handler(args):
7047 # Check for missing arguments
7148 if not args .wordlist :
7249 raise ValueError ("No Wordlist Found" )
73- text = ''
74- a_value = ' ' .join (args .freq_analysis )
7550 # 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' ) )
7752 # Analyse the frequency
7853 matches = analyse_text (text , '' .join (args .wordlist ))
7954 avg = float (matches ) / len (text .split (' ' )) * 100
@@ -84,11 +59,14 @@ def freq_analysis_handler(args):
8459
8560
8661def 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" )
8865 # Get cipher text
89- cipher = open ( c_value , 'r' ). read () if args . sourceFile else c_value
66+ cipher = get_text ( args , 'brute_frequency' )
9067 # Get analysed disctionary
91- analyzed = brute_freq (cipher , '' .join (args .wordlist ))
68+ bruteforced = brute_freq (cipher , '' .join (args .wordlist ))
69+ analyzed = bruteforced [0 ]
9270 # Write to the file,
9371 # If an output path specified
9472 if args .output :
@@ -105,7 +83,8 @@ def brute_freq_handler(args):
10583 found = False
10684 for i in analyzed :
10785 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 } ' )
10988 found = True
11089 if not found :
11190 print (
0 commit comments