Skip to content
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