write-ups-challenges-2020-2021/scrambled_message/encode.py

40 lines
959 B
Python
Raw Normal View History

2022-11-24 17:03:20 +00:00
from random import randint, seed
from itertools import chain
seed(a = 10489, version=2)
flag = "IGCTF{RealEncryptionWouldHaveBeenABetterChoice}"
positions = []
while len(positions) < len(flag):
pos = randint(0, 65536)
if pos in positions:
continue
positions.append(pos)
size = max(positions)
buff = [ randint(0, 255) for _ in range(0, size+1) ]
for char, pos in zip(flag, positions):
buff[pos] = ord(char)
with open("memory.bin", "wb") as f:
print("Writing number of characters to the file")
# Write the number of characters first
v = len(flag).to_bytes(4, "little")
f.write(v)
print("Writing the positions to the file")
# Write the positions as bytes to the file
for n in positions:
v = n.to_bytes(4, "little")
f.write(v)
print("Writing the data to the file")
# Write the data to the file
for b in buff:
f.write(b.to_bytes(1, "little"))
print("Done")