Advertisement
Guest User

program1.py

a guest
Aug 11th, 2017
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.10 KB | None | 0 0
  1.  
  2. import base64
  3. import datetime
  4. import sys
  5.  
  6. # Reverse Caesar shift the alphabetic characters in 'line' by 'amt' places.
  7. def outer_Caesar_shift(line, amt):
  8.     answer = ""
  9.     for c in line:
  10.         if (ord('A') <= ord(c)) and (ord(c) <= ord('Z')):
  11.             answer += chr(((ord(c) - ord('A') - amt) % 26) + ord('A'))
  12.         elif (ord('a') <= ord(c)) and (ord(c) <= ord('z')):
  13.             answer += chr(((ord(c) - ord('a') - amt) % 26) + ord('a'))
  14.         else:
  15.             answer += c
  16.     return answer
  17.  
  18. # Convert the external coded message text to a list of correctly flipped bytes.
  19. # This means that for New messages, the even-numbered bytes are reversed.
  20. # Arguments:
  21. #    'clue' is "O" for Old messages, "N" for New messages,
  22. #           "+" for New messages that need to be extended by a zero byte,
  23. #           and "?" for unknown message type, i.e., the sidebar.
  24. #    'stamplast' is last character of the Unix timestamp, to use for
  25. #                the reverse Caesar shift.
  26. #    'stuff' is the external coded message text.
  27. def extract_ordered_inside_bytes(clue, stamplast, stuff):
  28.     shifted_stuff = outer_Caesar_shift(stuff, ord(stamplast) - ord('0'))
  29.     if (len(shifted_stuff) % 4) != 0:
  30.         shifted_stuff += "".join(["=" for _1 in range((-len(shifted_stuff)) % 4)])
  31.     decimal_stuff = base64.b64decode(shifted_stuff)
  32.     message_value = int(decimal_stuff.decode("ascii"))
  33.     byte_string = "{:b}".format(message_value)
  34.     pad_to_whole_byte = "".join(["0" for _1 in range((-len(byte_string)) % 8)])
  35.     byte_string = pad_to_whole_byte + byte_string
  36.     if clue == "+":
  37.         byte_string = "00000000" + byte_string
  38.     byte_list = []
  39.     for i in range(0,len(byte_string),8):
  40.         current_byte = byte_string[i:i+8]
  41.         if (clue in ("N", "+")) and ((i % 16) == 8):
  42.             current_byte = current_byte[::-1]
  43.         byte_list.append(int(current_byte, 2))
  44.     return byte_list
  45.  
  46. def print_rows(byte_list):
  47.     pad_to_64_bits = [None for _1 in range((-len(byte_list)) % 8)]
  48.     byte_list = pad_to_64_bits + byte_list
  49.     column = 8
  50.     for current_byte in byte_list:
  51.         column = (column - 1) % 8
  52.         num_spaces = [1,2,1,3,1,2,1,4][column]
  53.         print("".join([" " for _1 in range(num_spaces)]), end="")
  54.         if current_byte == None:
  55.             print("        ", end="")
  56.         else:
  57.             print("{:08b}".format(current_byte).replace("0","-"), end="")
  58.         if column == 0:
  59.             print()
  60.  
  61.  
  62. def process_one(index, clue, stamp, posttime, where, byte_list):
  63.     print()
  64.     print("[{:s}] {:s} {:s}:".format(index, where, stamp))
  65.  
  66.     print()
  67.     if stamp[0] in "0123456789":
  68.         if stamp != "0000000000":
  69.             human = datetime.datetime.fromtimestamp(int(stamp)).strftime('%Y-%m-%d %H:%M:%S')
  70.         else:
  71.             human = "1970"
  72.         print("    Unix timestamp decode:  {:s}".format(human))
  73.     else:
  74.         print("    Unix timestamp is missing.")
  75.     print("    Reddit posting time is: {:s}".format(posttime.replace("_",":")))
  76.  
  77.     if clue in ("N", "+"):
  78.         print("    Message type is NEW.", end="")
  79.     elif clue in ("O",):
  80.         print("    Message type is OLD.", end="")
  81.     else:
  82.         print("    Message type is UNKNOWN.", end="")
  83.     if (len(byte_list) % 2) == 0:
  84.         print("  Message size is EVEN.", end="")
  85.     else:
  86.         print("  Message size is ODD.", end="")
  87.     print("  Message length is {:d} bytes.".format(len(byte_list)))
  88.  
  89.     print()
  90.     print_rows(byte_list)
  91.  
  92.     if stamp[0] in "0123456789":
  93.         print()
  94.         print("    TimeStamp bits = {:032b}".format(int(stamp)))
  95.  
  96.     print()
  97.  
  98.  
  99. def process_all():
  100.     datafile = open("dataset_for_solving_f04cb.txt", "r")
  101.     for oneline in datafile:
  102.         if oneline[-1] == '\n':
  103.             oneline = oneline[:-1]
  104.         index, clue, stamp, posttime, where, stuff = oneline.split(":")
  105.         if clue != "X":
  106.             byte_list = extract_ordered_inside_bytes(clue, stamp[-1], stuff)
  107.             if True:
  108.                 process_one(index, clue, stamp, posttime, where, byte_list)
  109.  
  110.  
  111. if __name__ == "__main__":
  112.     process_all()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement