Advertisement
davegimo

Untitled

Jan 24th, 2021
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  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. if val < 0:
  17. val = 100 + ((e - key) % 26)
  18. if val > ord("z"):
  19. val -= 26
  20. s = s + chr(val)
  21. return s
  22.  
  23.  
  24. def decrypt(encrypt_text, s):
  25. decrypted_text = ''
  26. for i in range(len(encrypt_text)):
  27. if encrypt_text[i] == ' ':
  28. decrypted_text = decrypted_text + encrypt_text[i]
  29. elif encrypt_text[i].isupper():
  30. decrypted_text = decrypted_text + chr((ord(encrypt_text[i])-s-65)%26+65)
  31. else:
  32. decrypted_text = decrypted_text + chr((ord(encrypt_text[i])-s-97)%26+97)
  33. return decrypted_text
  34.  
  35. def start(file_name):
  36. lines = open(file_name).read().splitlines()
  37. for line in lines:
  38. element_list = line.split(":")
  39. algorithm = element_list[0]
  40. key = element_list[1]
  41. message = element_list[2]
  42. letter_list = message.split(" ")
  43. ascii_list = binary_to_ascii(letter_list)
  44. print(ascii_list)
  45. if algorithm == "caesar":
  46. print(caesar(ascii_list, int(key)))
  47. else:
  48. print("implementa funzione")
  49.  
  50.  
  51. start("caesartwo.txt")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement