Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import base64
- import datetime
- import sys
- # Reverse Caesar shift the alphabetic characters in 'line' by 'amt' places.
- def outer_Caesar_shift(line, amt):
- answer = ""
- for c in line:
- if (ord('A') <= ord(c)) and (ord(c) <= ord('Z')):
- answer += chr(((ord(c) - ord('A') - amt) % 26) + ord('A'))
- elif (ord('a') <= ord(c)) and (ord(c) <= ord('z')):
- answer += chr(((ord(c) - ord('a') - amt) % 26) + ord('a'))
- else:
- answer += c
- return answer
- # Convert the external coded message text to a list of correctly flipped bytes.
- # This means that for New messages, the even-numbered bytes are reversed.
- # Arguments:
- # 'clue' is "O" for Old messages, "N" for New messages,
- # "+" for New messages that need to be extended by a zero byte,
- # and "?" for unknown message type, i.e., the sidebar.
- # 'stamplast' is last character of the Unix timestamp, to use for
- # the reverse Caesar shift.
- # 'stuff' is the external coded message text.
- def extract_ordered_inside_bytes(clue, stamplast, stuff):
- shifted_stuff = outer_Caesar_shift(stuff, ord(stamplast) - ord('0'))
- if (len(shifted_stuff) % 4) != 0:
- shifted_stuff += "".join(["=" for _1 in range((-len(shifted_stuff)) % 4)])
- decimal_stuff = base64.b64decode(shifted_stuff)
- message_value = int(decimal_stuff.decode("ascii"))
- byte_string = "{:b}".format(message_value)
- pad_to_whole_byte = "".join(["0" for _1 in range((-len(byte_string)) % 8)])
- byte_string = pad_to_whole_byte + byte_string
- if clue == "+":
- byte_string = "00000000" + byte_string
- byte_list = []
- for i in range(0,len(byte_string),8):
- current_byte = byte_string[i:i+8]
- if (clue in ("N", "+")) and ((i % 16) == 8):
- current_byte = current_byte[::-1]
- byte_list.append(int(current_byte, 2))
- return byte_list
- def print_rows(byte_list):
- pad_to_64_bits = [None for _1 in range((-len(byte_list)) % 8)]
- byte_list = pad_to_64_bits + byte_list
- column = 8
- for current_byte in byte_list:
- column = (column - 1) % 8
- num_spaces = [1,2,1,3,1,2,1,4][column]
- print("".join([" " for _1 in range(num_spaces)]), end="")
- if current_byte == None:
- print(" ", end="")
- else:
- print("{:08b}".format(current_byte).replace("0","-"), end="")
- if column == 0:
- print()
- def process_one(index, clue, stamp, posttime, where, byte_list):
- print()
- print("[{:s}] {:s} {:s}:".format(index, where, stamp))
- print()
- if stamp[0] in "0123456789":
- if stamp != "0000000000":
- human = datetime.datetime.fromtimestamp(int(stamp)).strftime('%Y-%m-%d %H:%M:%S')
- else:
- human = "1970"
- print(" Unix timestamp decode: {:s}".format(human))
- else:
- print(" Unix timestamp is missing.")
- print(" Reddit posting time is: {:s}".format(posttime.replace("_",":")))
- if clue in ("N", "+"):
- print(" Message type is NEW.", end="")
- elif clue in ("O",):
- print(" Message type is OLD.", end="")
- else:
- print(" Message type is UNKNOWN.", end="")
- if (len(byte_list) % 2) == 0:
- print(" Message size is EVEN.", end="")
- else:
- print(" Message size is ODD.", end="")
- print(" Message length is {:d} bytes.".format(len(byte_list)))
- print()
- print_rows(byte_list)
- if stamp[0] in "0123456789":
- print()
- print(" TimeStamp bits = {:032b}".format(int(stamp)))
- print()
- def process_all():
- datafile = open("dataset_for_solving_f04cb.txt", "r")
- for oneline in datafile:
- if oneline[-1] == '\n':
- oneline = oneline[:-1]
- index, clue, stamp, posttime, where, stuff = oneline.split(":")
- if clue != "X":
- byte_list = extract_ordered_inside_bytes(clue, stamp[-1], stuff)
- if True:
- process_one(index, clue, stamp, posttime, where, byte_list)
- if __name__ == "__main__":
- process_all()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement