Untitled

 avatar
unknown
plain_text
10 months ago
2.7 kB
6
Indexable
import base64
import re

def extract_base64_content(file_path):
    with open(file_path, 'r') as file:
        content = file.read()
    # Wyodrębnij część zakodowaną w base64
    base64_content = re.search(r"""Here's the email encoded with an unknown number of rounds of base64 encoding:\s*(.*)""", content, re.DOTALL).group(1).strip()
    return base64_content

def correct_base64_padding(encoded_data):
    padding_needed = 4 - (len(encoded_data) % 4)
    if padding_needed and padding_needed != 4:
        encoded_data += '=' * padding_needed
    return encoded_data

def decode_base64_string(encoded_data, max_iterations=100):
    decoded_data = encoded_data.encode('utf-8')
    for iteration in range(max_iterations):
        try:
            decoded_data = base64.b64decode(correct_base64_padding(decoded_data))
            print(f"Iteracja {iteration + 1}: {decoded_data[:100]}")  # Pokazanie pierwszych 100 bajtów
        except Exception as e:
            print(f"Dekodowanie zakończone po {iteration} iteracjach.")
            print(f"Błąd: {e}")
            break
    return decoded_data

# Odczytaj i wyodrębnij zawartość base64 z pliku
base64_content = extract_base64_content('email_encoded.txt')

# Dekoduj wiadomość wielokrotnie
decoded_bytes = decode_base64_string(base64_content)

# Zapisz zdekodowaną wiadomość jako bajty do pliku
with open('decoded_email_bytes.txt', 'wb') as file:
    file.write(decoded_bytes)

print("Zdekodowana wiadomość została zapisana do pliku 'decoded_email_bytes.txt' jako bajty.")

# Spróbuj różne metody dekodowania na końcu
try:
    decoded_message_str_utf8 = decoded_bytes.decode('utf-8')
    with open('decoded_email_utf8.txt', 'w') as file:
        file.write(decoded_message_str_utf8)
    print("Zdekodowana wiadomość została zapisana do pliku 'decoded_email_utf8.txt' jako UTF-8.")
except UnicodeDecodeError:
    print("Nie udało się zdekodować wiadomości na UTF-8.")

try:
    decoded_message_str_latin1 = decoded_bytes.decode('latin1')
    with open('decoded_email_latin1.txt', 'w') as file:
        file.write(decoded_message_str_latin1)
    print("Zdekodowana wiadomość została zapisana do pliku 'decoded_email_latin1.txt' jako Latin1.")
except UnicodeDecodeError:
    print("Nie udało się zdekodować wiadomości na Latin1.")

try:
    decoded_message_str_ascii = decoded_bytes.decode('ascii')
    with open('decoded_email_ascii.txt', 'w') as file:
        file.write(decoded_message_str_ascii)
    print("Zdekodowana wiadomość została zapisana do pliku 'decoded_email_ascii.txt' jako ASCII.")
except UnicodeDecodeError:
    print("Nie udało się zdekodować wiadomości na ASCII.")
Editor is loading...
Leave a Comment