Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- # Filename: encoding_identifier_utility.py
- # Version: 1.0.1
- # Author: Jeoi Reqi
- """
- Description:
- - This script identifies and decodes various types of encoded or obfuscated data.
- - It supports Base64, hexadecimal, binary, and ASCII encoding.
- - Additionally, it offers to try Caesar Shift decryption (ROT 1-26) if the input data does not match any of the other encoding types.
- Functions:
- - decode_base64(data):
- Decodes Base64 encoded data.
- - decode_hexadecimal(data):
- Decodes hexadecimal encoded data.
- - decode_binary(data):
- Decodes binary encoded data.
- - decode_ascii(data):
- Decodes ASCII encoded data.
- - caesar_shift_decrypt(data, shift):
- Decrypts data encoded with a Caesar Shift cipher.
- - is_printable(text):
- Checks if the text contains only printable ASCII characters.
- - line():
- Prints a line separator.
- - main():
- Main function to run the encoding identification utility.
- Requirements:
- - Python 3.x
- - base64 module (Part of Python's standard library)
- Usage:
- - Run the script and input the encoded/obfuscated data when prompted. The script will attempt to identify and decode the data, and if no match is found, it will offer to try Caesar Shift decryption.
- Additional Notes:
- - If the script does not find a matching encoding type, it suggests trying other decoding methods like URL encoding, Morse code, Base32 encoding, XOR encryption, etc.
- - The script includes error handling to manage invalid inputs and unexpected errors gracefully.
- """
- import base64
- def decode_base64(data):
- """
- Decodes Base64 encoded data.
- Args:
- data (str): The Base64 encoded data string.
- Returns:
- bytes or None: The decoded data if successful, otherwise None.
- """
- try:
- decoded_data = base64.b64decode(data)
- decoded_data.decode("utf-8") # Verify if it's valid UTF-8 text
- return decoded_data
- except Exception:
- return None
- def decode_hexadecimal(data):
- """
- Decodes hexadecimal encoded data.
- Args:
- data (str): The hexadecimal encoded data string.
- Returns:
- bytes or None: The decoded data if successful, otherwise None.
- """
- try:
- hex_values = data.split()
- decoded_data = bytes(int(value, 16) for value in hex_values)
- decoded_data.decode("utf-8") # Verify if it's valid UTF-8 text
- return decoded_data
- except Exception:
- return None
- def decode_binary(data):
- """
- Decodes binary encoded data.
- Args:
- data (str): The binary encoded data string.
- Returns:
- str or None: The decoded data if successful, otherwise None.
- """
- try:
- if all(bit in "01" for bit in data.replace(" ", "")):
- decoded_data = "".join(chr(int(chunk, 2)) for chunk in data.split())
- return decoded_data
- else:
- return None
- except Exception:
- return None
- def decode_ascii(data):
- """
- Decodes ASCII encoded data.
- Args:
- data (str): The ASCII encoded data string.
- Returns:
- str or None: The decoded data if successful, otherwise None.
- """
- try:
- ascii_values = [int(sub) for sub in data.split()]
- if all(0 <= value <= 127 for value in ascii_values):
- decoded_data = "".join(chr(value) for value in ascii_values)
- return decoded_data
- else:
- return None
- except Exception:
- return None
- def caesar_shift_decrypt(data, shift):
- """
- Decrypts data encoded with a Caesar Shift cipher.
- Args:
- data (str): The Caesar Shift encoded data string.
- shift (int): The ROT value (1-26) for decryption.
- Returns:
- str: The decrypted data.
- """
- decrypted_data = ""
- for char in data:
- if char.isalpha():
- shifted_char = chr(
- ((ord(char) - ord("A" if char.isupper() else "a") - shift) % 26)
- + ord("A" if char.isupper() else "a")
- )
- decrypted_data += shifted_char
- else:
- decrypted_data += char
- return decrypted_data
- def is_printable(text):
- """
- Checks if the text contains only printable ASCII characters.
- Args:
- text (str): The text to check.
- Returns:
- bool: True if all characters are printable, False otherwise.
- """
- return all(32 <= ord(char) <= 126 for char in text)
- def line():
- """
- Prints a line separator.
- """
- print("_" * 70, "\n")
- def main():
- """
- Main function to run the encoding identification utility.
- """
- while True:
- try:
- while True:
- # User input
- encoded_data = input("\nEnter the encoded string or obfuscated data: ")
- line()
- # Validate that the input is not empty
- if not encoded_data.strip():
- raise ValueError("\nInput cannot be empty!\n")
- # ASCII decoding
- ascii_decoded = decode_ascii(encoded_data)
- ascii_match = ascii_decoded is not None
- print("\t ASCII Match: ", ascii_match)
- # Base64 decoding
- base64_decoded = decode_base64(encoded_data)
- base64_match = base64_decoded is not None
- print("\t Base64 Match: ", base64_match)
- # Binary decoding
- binary_decoded = decode_binary(encoded_data)
- binary_match = binary_decoded is not None
- print("\t Binary Match: ", binary_match)
- # Hexadecimal decoding
- hex_decoded = decode_hexadecimal(encoded_data)
- hex_match = hex_decoded is not None
- print("\t Hexadecimal Match:", hex_match)
- line()
- # Determine the type of input
- input_type = None
- decoded_data = None
- if ascii_match:
- input_type = "ASCII"
- decoded_data = ascii_decoded
- elif base64_match:
- input_type = "Base64"
- decoded_data = base64_decoded.decode("utf-8")
- elif binary_match:
- input_type = "Binary"
- decoded_data = binary_decoded
- elif hex_match:
- input_type = "Hexadecimal"
- decoded_data = hex_decoded.decode("utf-8")
- # Print the input type and decoded data
- if input_type:
- print(f"\nThe input string is being identified as - {input_type} -")
- print(f"Decoded {input_type} data: {decoded_data}")
- else:
- print("\t Failed to identify the input type used!")
- line()
- while True: # Loop for Caesar Shift option
- # Ask user if they want to try Caesar Shift
- try_caesar = input(
- "Do you want to try Caesar Shift (ROT 1-26)?\n1: Yes\n2: No\nMake your selection (1 or 2):"
- )
- if try_caesar == "1":
- line()
- print("Caesar Shift (ROT 1-26) results:\n")
- caesar_results = []
- for shift in range(1, 27):
- caesar_decoded = caesar_shift_decrypt(encoded_data, shift)
- if is_printable(caesar_decoded):
- caesar_results.append((shift, caesar_decoded))
- print(f"ROT {shift:02}: {caesar_decoded}")
- else:
- line()
- print(
- "\n\t Invalid input! Please enter 1 or 2.\n"
- )
- line()
- break # Exit the Caesar Shift loop
- elif try_caesar == "2":
- print("Exiting program... \tGoodbye!\n")
- return
- else:
- line()
- print("\n\t Invalid input! Please enter 1 or 2.\n")
- line()
- break
- while True: # Loop for input validation
- line()
- found = input(
- "Did any of the Caesar Shift results return the expected data?\n1: Yes\n2: No\nMake your selection (1 or 2):"
- )
- if found == "1":
- while True: # Loop for ROT value validation
- try:
- rot_value = int(
- input(
- "\nPlease enter the ROT value that matched your expected result: "
- )
- )
- if 1 <= rot_value <= 26:
- matching_result = next(
- (
- result
- for shift, result in caesar_results
- if shift == rot_value
- ),
- None,
- )
- if matching_result is not None:
- line()
- print(
- "\t Congratulations on finding a match!"
- )
- line()
- print(
- f"The input string is being identified as Caesar Shift (ROT-{rot_value})"
- )
- print(
- "\nDecoded Caesar Shift data:\n\n\t",
- matching_result,
- )
- line()
- continue_or_exit = input(
- "Do you want to input another string?\n1: Yes\n2: No\nMake your selection (1 or 2):"
- )
- if continue_or_exit == "2":
- print("\nExiting program... \tGoodbye!\n")
- return
- else:
- break # Exit the ROT value loop and go back to asking for another string
- else:
- line()
- print(
- "\n\tInvalid ROT value! Please enter a value between 1 and 26."
- )
- line()
- else:
- line()
- print(
- "\n\tInvalid ROT value! Please enter a value between 1 and 26."
- )
- line()
- except ValueError:
- line()
- print(
- "\n\tInvalid input! Please enter a valid integer value."
- )
- line()
- elif found == "2":
- print(
- "\t Sorry you could not find a match :(\n\nYou may want to try other decoding methods like URL encoding,\nMorse code, Base32 encoding, XOR encryption, or others."
- )
- line()
- print("Exiting program... \tGoodbye!\n")
- return
- else:
- line()
- print("\n\t Invalid input! Please enter 1 or 2.\n")
- line()
- break # Break from the input validation loop and go back to asking for another string
- break # Break from the Caesar Shift loop and go back to asking for another string
- except ValueError as e:
- line()
- print(f"\n\tError: {e}\n")
- line()
- except Exception as e:
- line()
- print(f"\n\tAn unexpected error occurred: {e}\n")
- line()
- if __name__ == "__main__":
- main()
Add Comment
Please, Sign In to add comment