Last active
March 7, 2024 03:24
-
-
Save rightson/85609fba41a1e0b536da89751d775a46 to your computer and use it in GitHub Desktop.
Base 64 Converter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import argparse | |
import base64 | |
import zipfile | |
import os | |
def convert_exe_to_base64_and_zip(input_file, output_path): | |
"""Converts an EXE file to Base64, zips the encoded content, and renames the archive to .b64.zip.""" | |
with open(input_file, 'rb') as file: | |
binary_data = file.read() | |
base64_encoded_data = base64.b64encode(binary_data).decode('utf-8') | |
temp_file_name = "temp_output.txt" | |
with open(temp_file_name, 'w') as temp_file: | |
temp_file.write(base64_encoded_data) | |
zip_file_name = output_path + ".zip" | |
with zipfile.ZipFile(zip_file_name, 'w') as zipf: | |
zipf.write(temp_file_name, arcname=os.path.basename(temp_file_name)) | |
os.remove(temp_file_name) | |
b64z_file_name = output_path + ".b64.zip" | |
os.rename(zip_file_name, b64z_file_name) | |
print(f"Converted and compressed to '{b64z_file_name}'.") | |
def convert_b64z_to_exe(input_file, output_file): | |
"""Decodes a .b64.zip file back to its original EXE format.""" | |
zip_file_name = input_file.replace('.b64.zip', '.zip') | |
# Rename .b64.zip to .zip to unzip | |
os.rename(input_file, zip_file_name) | |
with zipfile.ZipFile(zip_file_name, 'r') as zipf: | |
zipf.extractall("temp_extract") | |
temp_file_name = os.path.join("temp_extract", os.listdir("temp_extract")[0]) | |
with open(temp_file_name, 'r') as temp_file: | |
base64_data = temp_file.read() | |
binary_data = base64.b64decode(base64_data) | |
with open(output_file, 'wb') as file: | |
file.write(binary_data) | |
# Clean up | |
os.remove(zip_file_name) | |
os.remove(temp_file_name) | |
os.rmdir("temp_extract") | |
print(f"Decompressed and converted to '{output_file}'.") | |
if __name__ == "__main__": | |
parser = argparse.ArgumentParser(description="Convert between EXE and B64Z formats.") | |
parser.add_argument("-i", "--input", help="Path to the input file", required=True) | |
parser.add_argument("-o", "--output", help="Path to the output file", required=True) | |
parser.add_argument("--to-b64z", help="Convert EXE to B64Z", action="store_true") | |
parser.add_argument("--from-b64z", help="Convert B64Z to EXE", action="store_true") | |
args = parser.parse_args() | |
if args.to_b64z and args.from_b64z: | |
print("Please select only one mode of operation (--to-b64z or --from-b64z).") | |
elif args.to_b64z: | |
convert_exe_to_base64_and_zip(args.input, args.output) | |
elif args.from_b64z: | |
convert_b64z_to_exe(args.input, args.output) | |
else: | |
print("Please specify the mode of operation (--to-b64z or --from-b64z).") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment