davegimo

Untitled

Jan 24th, 2021
53
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. def binary_to_ascii(binary_message):
  2. ascii_list = list()
  3. for byte in binary_message:
  4. byte = byte[::-1]
  5. acc = 0
  6. for i in range(len(byte)):
  7. acc += int(byte[i])*(2**i)
  8. ascii_list.append(acc)
  9. return ascii_list
  10.  
  11. def caesar(ascii_list, key):
  12. s = ""
  13. for e in ascii_list:
  14. key = int(key)
  15. val = (e - key)
  16.  
  17. if val < 0:
  18. val = 100 + ((e - key) % 26)
  19. if val > ord("z"):
  20. val -= 26
  21.  
  22. s = s + chr(val)
  23. return s
  24.  
  25. def vig(ascii_list, key):
  26. s = ""
  27. for i in range(len(ascii_list)):
  28. index = i % len(key)
  29. e = ascii_list[i]
  30. #print(i, key[index])
  31. val = e - ord(key[index])
  32. s = s + chr(val)
  33. return s
  34.  
  35.  
  36. def start(file_name):
  37. lines = open(file_name).read().splitlines()
  38. for line in lines:
  39. element_list = line.split(":")
  40. algorithm = element_list[0]
  41. key = element_list[1]
  42. message = element_list[2]
  43. letter_list = message.split(" ")
  44. ascii_list = binary_to_ascii(letter_list)
  45. if algorithm == "caesar":
  46. print(caesar(ascii_list, int(key)))
  47. else:
  48. print(vig(ascii_list, key))
  49.  
RAW Paste Data