Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def embed_message_into_crc(file_path, message):
- 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)
- 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.")
- chunks = []
- message_bits = to_binary(message.encode())
- message_index = 0
- 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')
- chunk_data = file.read(length)
- crc_bytes = file.read(4)
- crc_binary = to_binary(crc_bytes)
- modified_crc_binary = ''
- for i in range(len(crc_binary)):
- if message_index < len(message_bits):
- modified_crc_binary += crc_binary[i][:-1] + message_bits[message_index]
- message_index += 1
- else:
- modified_crc_binary += crc_binary[i]
- modified_crc_bytes = from_binary(modified_crc_binary)
- chunks.append((chunk_type, length, chunk_data, modified_crc_bytes))
- if chunk_type == 'IEND':
- break
- with open('output2.png', 'wb') as modified_file:
- modified_file.write(signature)
- for chunk_type, length, chunk_data, modified_crc_bytes in chunks:
- modified_file.write(length.to_bytes(4, 'big'))
- modified_file.write(chunk_type.encode())
- modified_file.write(chunk_data)
- modified_file.write(modified_crc_bytes)
- print("Message successfully embedded into CRC values!")
- message = "rahasia"
- embed_message_into_crc('IPB.png', message)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement