Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def binary_to_ascii(binary_message):
- ascii_list = list()
- for byte in binary_message:
- byte = byte[::-1]
- acc = 0
- for i in range(len(byte)):
- acc += int(byte[i])*(2**i)
- ascii_list.append(acc)
- return ascii_list
- def caesar(ascii_list, key):
- s = ""
- for e in ascii_list:
- key = int(key)
- val = (e - key)
- if val < 0:
- val = 100 + ((e - key) % 26)
- if val > ord("z"):
- val -= 26
- s = s + chr(val)
- return s
- def decrypt(encrypt_text, s):
- decrypted_text = ''
- for i in range(len(encrypt_text)):
- if encrypt_text[i] == ' ':
- decrypted_text = decrypted_text + encrypt_text[i]
- elif encrypt_text[i].isupper():
- decrypted_text = decrypted_text + chr((ord(encrypt_text[i])-s-65)%26+65)
- else:
- decrypted_text = decrypted_text + chr((ord(encrypt_text[i])-s-97)%26+97)
- return decrypted_text
- def start(file_name):
- lines = open(file_name).read().splitlines()
- for line in lines:
- element_list = line.split(":")
- algorithm = element_list[0]
- key = element_list[1]
- message = element_list[2]
- letter_list = message.split(" ")
- ascii_list = binary_to_ascii(letter_list)
- print(ascii_list)
- if algorithm == "caesar":
- print(caesar(ascii_list, int(key)))
- else:
- print("implementa funzione")
- start("caesartwo.txt")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement