Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def extract_message_from_crc(file_path):
- def to_binary(data):
- return ''.join(format(byte, '08b') for byte in data)
- def from_binary(binary_data):
- byte_array = [int(binary_data[i:i + 8], 2) for i in range(0, len(binary_data), 8)]
- return bytes(byte_array).decode('utf-8', errors='ignore')
- with open(file_path, 'rb') as file:
- signature = file.read(8)
- if signature != b'\x89PNG\r\n\x1a\n':
- raise ValueError("File is not a valid PNG file.")
- message_bits = []
- while True:
- length_bytes = file.read(4)
- if len(length_bytes) < 4:
- break
- length = int.from_bytes(length_bytes, 'big')
- chunk_type = file.read(4).decode('ascii')
- file.read(length)
- crc_bytes = file.read(4)
- crc_binary = to_binary(crc_bytes)
- for bit in crc_binary:
- message_bits.append(bit[-1])
- if chunk_type == 'IEND':
- break
- message_binary = ''.join(message_bits)
- message = from_binary(message_binary)
- print("Extracted message:", message)
- return message
- extracted_message = extract_message_from_crc('output3.png')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement