|
| 1 | +# Import the necessary libraries. |
| 2 | +import pyzipper, argparse, sys, re, getpass |
| 3 | +from colorama import Fore, init |
| 4 | + |
| 5 | +init() |
| 6 | + |
| 7 | +# Define a function to get CLI commands. |
| 8 | +def get_cli_arguments(): |
| 9 | + parser = argparse.ArgumentParser(description="A program to lock a ZIP File.") |
| 10 | + # Collect user arguments. |
| 11 | + parser.add_argument('--zipfile', '-z', dest='zip_file', help='Specify the ZIP file to create or update.') |
| 12 | + parser.add_argument('--addfile', '-a', dest='add_files', nargs='+', help='Specify one or more files to add to the ZIP file(s).') |
| 13 | + |
| 14 | + # Parse the collected arguments. |
| 15 | + args = parser.parse_args() |
| 16 | + |
| 17 | + # Check if arguments are missing, print appropriate messages and exit the program. |
| 18 | + if not args.zip_file: |
| 19 | + parser.print_help() |
| 20 | + sys.exit() |
| 21 | + if not args.add_files: |
| 22 | + parser.print_help() |
| 23 | + sys.exit() |
| 24 | + |
| 25 | + return args |
| 26 | + |
| 27 | +# Function to check password strength. |
| 28 | +def check_password_strength(password): |
| 29 | + # Check for minimum length. In our case, 8. |
| 30 | + if len(password) < 8: |
| 31 | + return False |
| 32 | + |
| 33 | + # Check for at least one uppercase letter, one lowercase letter, and one digit. |
| 34 | + if not (re.search(r'[A-Z]', password) and re.search(r'[a-z]', password) and re.search(r'\d', password)): |
| 35 | + return False |
| 36 | + |
| 37 | + return True |
| 38 | + |
| 39 | +# Call the arguments function. |
| 40 | +arguments = get_cli_arguments() |
| 41 | + |
| 42 | +# Get user password |
| 43 | +password = getpass.getpass("[?] Enter your password > ") |
| 44 | + |
| 45 | +# If password is weak, tell the user and exit the program. |
| 46 | +if not check_password_strength(password): |
| 47 | + print(f"{Fore.RED}[-] Password is not strong enough. It should have at least 8 characters and contain at least one uppercase letter, one lowercase letter, and one digit.") |
| 48 | + sys.exit() |
| 49 | + |
| 50 | +# Create a password-protected ZIP file. |
| 51 | +with pyzipper.AESZipFile(arguments.zip_file, 'w', compression=pyzipper.ZIP_LZMA, encryption=pyzipper.WZ_AES) as zf: |
| 52 | + zf.setpassword(password.encode()) |
| 53 | + |
| 54 | + # Add files to the ZIP file. |
| 55 | + for file_to_add in arguments.add_files: |
| 56 | + zf.write(file_to_add) |
| 57 | + |
| 58 | +# Print a Success message. |
| 59 | +print(f"{Fore.GREEN}[+] ZIP file is locked with a strong password.") |
0 commit comments