Skip to content

Update __init__.py #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 45 additions & 34 deletions codesafe/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ def decrypt(encrypted_code:str, mapping: dict =_character_map) -> str:

return original_code

def run(encrypted_file: str, mapping: dict = _character_map) -> None:
def run(encrypted_file: str, mapping: dict = _character_map) -> bool:
"""
Decrypt and execute the Python code embedded in the specified file.

Expand All @@ -289,23 +289,28 @@ def run(encrypted_file: str, mapping: dict = _character_map) -> None:
Returns:
None
"""
# Read the encrypted code from the file
with open(encrypted_file, 'r') as file:
content = file.read()

# Extract the encrypted code from the comments
encrypted_code = re.search(r'# (.+)', content).group(1)
encrypted_code = encrypted_code.replace("# ", '')

# Decrypt the code
decrypted_code = decrypt_code(encrypted_code, mapping)

# Execute the decrypted Python code
exec(decrypted_code)

print("Code decrypted and executed successfully.")

def decrypt_to_file(encrypted_file: str, output_file: str, mapping: dict = _character_map) -> None:
try:
# Read the encrypted code from the file
with open(encrypted_file, 'r') as file:
content = file.read()

# Extract the encrypted code from the comments
encrypted_code = re.search(r'# (.+)', content).group(1)
encrypted_code = encrypted_code.replace("# ", '')

# Decrypt the code
decrypted_code = decrypt_code(encrypted_code, mapping)

# Execute the decrypted Python code
exec(decrypted_code)

# I modify the print statement global so this will break for me.
#print("Code decrypted and executed successfully.")
return true
except Exception:
return false

def decrypt_to_file(encrypted_file: str, output_file: str, mapping: dict = _character_map) -> bool:
"""
Decrypt the code embedded in the specified file and write it to an output file.

Expand All @@ -317,19 +322,25 @@ def decrypt_to_file(encrypted_file: str, output_file: str, mapping: dict = _char
Returns:
None
"""
# Read the encrypted code from the file
with open(encrypted_file, 'r') as file:
content = file.read()

# Extract the encrypted code from the comments
encrypted_code = re.search(r'# (.+)', content).group(1)
encrypted_code = encrypted_code.replace("# ", '')

# Decrypt the code
decrypted_code = decrypt_code(encrypted_code, mapping)

# Write the decrypted code to the specified output file
with open(output_file, 'w') as file:
file.write(decrypted_code)

print(f"Decrypted code written to {output_file}.")
try:

# Read the encrypted code from the file
with open(encrypted_file, 'r') as file:
content = file.read()

# Extract the encrypted code from the comments
encrypted_code = re.search(r'# (.+)', content).group(1)
encrypted_code = encrypted_code.replace("# ", '')

# Decrypt the code
decrypted_code = decrypt_code(encrypted_code, mapping)

# Write the decrypted code to the specified output file
with open(output_file, 'w') as file:
file.write(decrypted_code)

# I modify the print global so this will break for me.
#print(f"Decrypted code written to {output_file}.")
return true
except Exception:
return false