
This Python script allows a user to decode a Base64 string that is saved in a text file. This script utilizes TKinter, the premier choice for Graphical User Interface (GUI), to select a file, then decode the contents of the file into the console. This script is compatible with Windows, MacOS, and Linux hosts.
Code segment:
## PyBase64DecodeGUI: Written by Andrew Akers (andrew@techmasteracademy.com)##
import os.path
from os import system
import tkinter as tk
from tkinter import filedialog
import base64
def file_prompt(): ## File Prompt
print("Please select the file that you would like to Base64 decode.\n\n")
#Clear Screen
if os.name == 'posix':
os.system('clear')
if os.name =='nt':
os.system('cls')
file_prompt()
root = tk.Tk()
root.withdraw()
file_path_name = filedialog.askopenfilename()
try:
encodedStr = open(file_path_name, encoding='utf8').read()
except:
print ("Error: Could not find file or read data.\n\n")
quit()
else:
print ("File opened successfully.\n\n")
print("File Path: " + file_path_name + "\n\n")
print ("Base64 Encoded text: " + encodedStr + "\n\n")
# Url Safe Base64 Decoding
decodedBytes = base64.urlsafe_b64decode(encodedStr)
decodedStr = str(decodedBytes, "utf-8")
print("Base64 Decoded text: " + decodedStr + "\n\n")