#!/usr/bin/python3

import base64, random, string, os
from Crypto.Cipher import AES
from Crypto.Random import get_random_bytes
from Crypto.Util.Padding import pad, unpad

try:
    encrypt_flag=True if os.sys.argv[1].lower()=='1' else False 
except Exception as err:
    encrypt_flag=False

def generate_password(length=8, num_uppercase=1, num_lowercase=1, num_digits=1, num_sp_char=1):
    sp_char = '!@#$'
    all_chars = string.ascii_letters + string.digits + sp_char

    password = [
        *random.choices(string.ascii_uppercase, k=num_uppercase),
        *random.choices(string.ascii_lowercase, k=num_lowercase),
        *random.choices(string.digits, k=num_digits),
        *random.choices(sp_char, k=num_sp_char)
    ]

    remaining_length = length - (num_uppercase + num_lowercase + num_digits + num_sp_char)
    password += random.choices(all_chars, k=remaining_length)
    
    random.shuffle(password)
    return ''.join(password)

def encrypt(plain_text, key):
    manual_iv = b'PhilinnovatorDEV'
    cipher = AES.new(key, AES.MODE_CBC, iv=manual_iv)
    ct_bytes = cipher.encrypt(pad(plain_text.encode(), 16))
    ct = base64.b64encode(ct_bytes).decode('utf-8')
    return ct

key = b'PhilinnovatorDEVPhilinnovatorDEV'
plain_text = generate_password()

if encrypt_flag: 
    encrypted_text = encrypt(plain_text, key)
    print(encrypted_text)
else:
    print(plain_text)
