Advertisement
EaterSun

Embedded CRC LSB

Nov 27th, 2024
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.03 KB | Cybersecurity | 0 0
  1. def embed_message_into_crc(file_path, message):
  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)
  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.         chunks = []
  15.         message_bits = to_binary(message.encode())
  16.         message_index = 0
  17.  
  18.         while True:
  19.             length_bytes = file.read(4)
  20.             if len(length_bytes) < 4:
  21.                 break  
  22.  
  23.             length = int.from_bytes(length_bytes, 'big')
  24.             chunk_type = file.read(4).decode('ascii')
  25.             chunk_data = file.read(length)
  26.             crc_bytes = file.read(4)
  27.             crc_binary = to_binary(crc_bytes)
  28.             modified_crc_binary = ''
  29.             for i in range(len(crc_binary)):
  30.                 if message_index < len(message_bits):
  31.                     modified_crc_binary += crc_binary[i][:-1] + message_bits[message_index]
  32.                     message_index += 1
  33.                 else:
  34.                     modified_crc_binary += crc_binary[i]
  35.  
  36.             modified_crc_bytes = from_binary(modified_crc_binary)
  37.  
  38.             chunks.append((chunk_type, length, chunk_data, modified_crc_bytes))
  39.  
  40.             if chunk_type == 'IEND':
  41.                 break
  42.  
  43.         with open('output2.png', 'wb') as modified_file:
  44.             modified_file.write(signature)
  45.             for chunk_type, length, chunk_data, modified_crc_bytes in chunks:
  46.                 modified_file.write(length.to_bytes(4, 'big'))
  47.                 modified_file.write(chunk_type.encode())
  48.                 modified_file.write(chunk_data)
  49.                 modified_file.write(modified_crc_bytes)
  50.  
  51.         print("Message successfully embedded into CRC values!")
  52.  
  53. message = "rahasia"  
  54. embed_message_into_crc('IPB.png', message)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement