from tqdm import tqdm test_str = "abc" bytes_lst = [] for item in test_str: bytes_lst.append(bytes(item,'utf-8')) print("As bytes, the string is:") print(bytes_lst) encoded_lst = [] for item in test_str: encoded_lst.append(item.encode()) print("Encoding the characters in the string also gives:") print(encoded_lst) hex_lst=[] for item in encoded_lst: hex_lst.append(list(item)[0]) print("The hex values associated with those bytes is:") print(hex_lst) print("\n\n") # Checkpoint 1 print("Checkpoint 1:") def numerify(message): hex_values=[] for item in message: hex_values.append(item.encode()[0]) return hex_values print(numerify('abc')) print("\n") # Checkpoint 2 print("Checkpoint 2:") secret_number = 1 def encrypt(numeric_message): to_encrypt = [] for item in numeric_message: to_encrypt.append((item + secret_number) % 256) return to_encrypt print(encrypt(numerify('abc'))) print("\n") # Checkpoint 3 print("Checkpoint 3:") def decrypt(numeric_message): to_decrypt = [] for item in numeric_message: to_decrypt.append((item + (256 - secret_number)) % 256) return to_decrypt print(decrypt(encrypt(numerify('abc')))) print("\n") # Checkpoint 4 print("Checkpoint 4:") def wordify(decrypted_message): word = [] for items in decrypted_message: word.append(chr(items)) return ''.join(word) # print(wordify(decrypt(encrypt(numerify('abc'))))) secret_number = 78 message = open('xfile.txt','r').read().replace('[','').replace(']','').split(',') # Is this how I was supposed to get the list from the file? print(type(message)) message_copy = [] for item in message: message_copy.append(int(item)) message_copy=wordify(decrypt(message_copy)) print(message_copy) print("\n") # Challenge print("Challenge:") dictionary = open('words_370k.txt','r').read().split('\n') # This was the list of words we used in mwc1 unit 2. best_shift=0 # Used to keep track of which potential secret number worked the best in_dictionary_placeholder = 0 # Used to keep track of how many words in a potentially decrypted message exist in our dictionary. message_copy = [] shifted_message_copy=[] for item in message: message_copy.append(int(item)) for shift in tqdm(range(78,79)): # Cycle through all possible shifts and include a progress bar (so I have a rough idea of how long I can step away and cry in a corner) secret_number = shift shifted_message_copy=wordify(decrypt(message_copy)) shifted_message_copy.split(' ') # Words are separated by white space, so split the message according to where the spaces are. print(shifted_message_copy) ''' in_dictionary=0 for item in shifted_message_copy: # There must be an extraordinarily more efficient way to do this... for word in dictionary: if item == word: in_dictionary = in_dictionary + 1 if in_dictionary > in_dictionary_placeholder: in_dictionary_placeholder = in_dictionary # If the number of "real" words for this secret number is better than a previous secret number's, keep track of how many "real words there were," and best_shift=shift # keep track of what secret number was used. secret_number = best_shift message_copy=wordify(decrypt(message_copy)) print("Assuming a caesarian cipher was used, the secret_number is likely "+str(best_shift)+".") print(message_copy) '''