Advertisement
EaterSun

Extract CRC LSB

Nov 27th, 2024
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.26 KB | Cybersecurity | 0 0
  1. def extract_message_from_crc(file_path):
  2.     def to_binary(data):
  3.         return ''.join(format(byte, '08b') for byte in data)
  4.  
  5.     def from_binary(binary_data):
  6.         byte_array = [int(binary_data[i:i + 8], 2) for i in range(0, len(binary_data), 8)]
  7.         return bytes(byte_array).decode('utf-8', errors='ignore')
  8.  
  9.     with open(file_path, 'rb') as file:
  10.         signature = file.read(8)
  11.         if signature != b'\x89PNG\r\n\x1a\n':
  12.             raise ValueError("File is not a valid PNG file.")
  13.  
  14.         message_bits = []
  15.  
  16.         while True:
  17.             length_bytes = file.read(4)
  18.             if len(length_bytes) < 4:
  19.                 break  
  20.  
  21.             length = int.from_bytes(length_bytes, 'big')
  22.  
  23.             chunk_type = file.read(4).decode('ascii')
  24.  
  25.             file.read(length)
  26.  
  27.             crc_bytes = file.read(4)
  28.             crc_binary = to_binary(crc_bytes)
  29.  
  30.             for bit in crc_binary:
  31.                 message_bits.append(bit[-1])  
  32.  
  33.             if chunk_type == 'IEND':
  34.                 break
  35.  
  36.         message_binary = ''.join(message_bits)
  37.  
  38.         message = from_binary(message_binary)
  39.  
  40.         print("Extracted message:", message)
  41.         return message
  42.  
  43. extracted_message = extract_message_from_crc('output3.png')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement