A Python script to decode Base64-encoded data
I stumbled across a base64-encoded string while investigating a potential attack, therefore I made the decision to create my own decoder. You can use the base64
module in Python to decode Base64-encoded data. Here's an example script that decodes a Base64 string:
import base64
def decode_base64(base64_string):
try:
# Add padding if necessary
padding = len(base64_string) % 4
if padding != 0:
base64_string += '=' * (4 - padding)
# Decode the Base64 string
decoded_bytes = base64.b64decode(base64_string)
# Convert the decoded bytes to a string
decoded_string = decoded_bytes.decode('utf-8')
return decoded_string
except Exception as e:
print(f"Error decoding Base64: {str(e)}")
return None
# Example usage
base64_string = "SGVsbG8gV29ybGQh"
decoded_string = decode_base64(base64_string)
print(decoded_string)
In this script, the decode_base64
function takes a Base64-encoded string as input and returns the corresponding decoded string. If there is an error during the decoding process, it will print an error message and return None
.
The example usage demonstrates how to decode a Base64 string using the decode_base64
function and then print the decoded string. In this case, the Base64 string "SGVsbG8gV29ybGQh"
is decoded to "Hello World!"
.
You can replace the base64_string
variable with your own Base64-encoded string to decode it.
Thank you for reading my post.