generated from mwc/lab_encryption
Trying to make progress with the challenge...
This commit is contained in:
parent
6ce564f821
commit
de981ca670
Binary file not shown.
49
ccipher.py
49
ccipher.py
|
@ -1,3 +1,5 @@
|
||||||
|
from tqdm import tqdm
|
||||||
|
|
||||||
test_str = "abc"
|
test_str = "abc"
|
||||||
|
|
||||||
bytes_lst = []
|
bytes_lst = []
|
||||||
|
@ -17,16 +19,20 @@ for item in encoded_lst:
|
||||||
hex_lst.append(list(item)[0])
|
hex_lst.append(list(item)[0])
|
||||||
print("The hex values associated with those bytes is:")
|
print("The hex values associated with those bytes is:")
|
||||||
print(hex_lst)
|
print(hex_lst)
|
||||||
|
print("\n\n")
|
||||||
|
|
||||||
# Checkpoint 1
|
# Checkpoint 1
|
||||||
|
print("Checkpoint 1:")
|
||||||
def numerify(message):
|
def numerify(message):
|
||||||
hex_values=[]
|
hex_values=[]
|
||||||
for item in message:
|
for item in message:
|
||||||
hex_values.append(item.encode()[0])
|
hex_values.append(item.encode()[0])
|
||||||
return hex_values
|
return hex_values
|
||||||
print(numerify('abc'))
|
print(numerify('abc'))
|
||||||
|
print("\n")
|
||||||
|
|
||||||
# Checkpoint 2
|
# Checkpoint 2
|
||||||
|
print("Checkpoint 2:")
|
||||||
secret_number = 1
|
secret_number = 1
|
||||||
def encrypt(numeric_message):
|
def encrypt(numeric_message):
|
||||||
to_encrypt = []
|
to_encrypt = []
|
||||||
|
@ -34,31 +40,62 @@ def encrypt(numeric_message):
|
||||||
to_encrypt.append((item + secret_number) % 256)
|
to_encrypt.append((item + secret_number) % 256)
|
||||||
return to_encrypt
|
return to_encrypt
|
||||||
print(encrypt(numerify('abc')))
|
print(encrypt(numerify('abc')))
|
||||||
|
print("\n")
|
||||||
|
|
||||||
# Checkpoint 3
|
# Checkpoint 3
|
||||||
|
print("Checkpoint 3:")
|
||||||
def decrypt(numeric_message):
|
def decrypt(numeric_message):
|
||||||
to_decrypt = []
|
to_decrypt = []
|
||||||
for item in numeric_message:
|
for item in numeric_message:
|
||||||
to_decrypt.append((item + (256 - secret_number)) % 256)
|
to_decrypt.append((item + (256 - secret_number)) % 256)
|
||||||
return to_decrypt
|
return to_decrypt
|
||||||
print(decrypt(encrypt(numerify('abc'))))
|
print(decrypt(encrypt(numerify('abc'))))
|
||||||
|
print("\n")
|
||||||
|
|
||||||
# Checkpoint 4
|
# Checkpoint 4
|
||||||
|
print("Checkpoint 4:")
|
||||||
def wordify(decrypted_message):
|
def wordify(decrypted_message):
|
||||||
word = []
|
word = []
|
||||||
for items in decrypted_message:
|
for items in decrypted_message:
|
||||||
word.append(chr(items))
|
word.append(chr(items))
|
||||||
return ''.join(word)
|
return ''.join(word)
|
||||||
|
# print(wordify(decrypt(encrypt(numerify('abc')))))
|
||||||
print(wordify(decrypt(encrypt(numerify('abc')))))
|
|
||||||
|
|
||||||
secret_number = 78
|
secret_number = 78
|
||||||
|
|
||||||
message = open('xfile.txt','r').read().replace('[','').replace(']','').split(',')
|
message = open('xfile.txt','r').read().replace('[','').replace(']','').split(',')
|
||||||
# Is this how I was supposed to get the list from the file?
|
# Is this how I was supposed to get the list from the file?
|
||||||
|
print(type(message))
|
||||||
message_copy = []
|
message_copy = []
|
||||||
for item in message:
|
for item in message:
|
||||||
message_copy.append(int(item))
|
message_copy.append(int(item))
|
||||||
message_copy=wordify(decrypt(message_copy))
|
message_copy=wordify(decrypt(message_copy))
|
||||||
print(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)
|
||||||
|
'''
|
|
@ -0,0 +1,71 @@
|
||||||
|
from die import Die
|
||||||
|
from tqdm import tqdm
|
||||||
|
|
||||||
|
class FiveDice:
|
||||||
|
def __init__(self):
|
||||||
|
self.dice = [Die() for number in range(5)]
|
||||||
|
|
||||||
|
def roll(self):
|
||||||
|
for die in self.dice:
|
||||||
|
die.roll()
|
||||||
|
return self.faces()
|
||||||
|
|
||||||
|
def faces(self):
|
||||||
|
return [die.face for die in self.dice]
|
||||||
|
|
||||||
|
def all_ones(self):
|
||||||
|
for face in self.faces():
|
||||||
|
if face != 1:
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
|
||||||
|
def findn(self,nofakind):
|
||||||
|
""" dice.findn(n) determines whether there are at least n of a kind in the roll and returns true if so and returns false otherwise. """
|
||||||
|
nmuch = {} # create an empty dictionary to associate each face rolled with its frequency
|
||||||
|
for die in dice.faces(): # go through each dice rolled
|
||||||
|
if die in nmuch: # check if the dictionary already has the face included, and if so increase its frequency by 1
|
||||||
|
nmuch[die] += 1
|
||||||
|
if nmuch[die]==nofakind: # each time the dictionary is updated, check if the desired frequency has been achieved, and if so return true
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
nmuch[die] = 1 # add the face to the dictionary with frequency 1 if it has not already been included
|
||||||
|
return False # if the desired frequency was never achieved, return false
|
||||||
|
|
||||||
|
def is_three_of_a_kind(self):
|
||||||
|
""" I didn't read the assignment carefully... this should return the result of dice.findn(3) """
|
||||||
|
return dice.findn(3)
|
||||||
|
|
||||||
|
def is_four_of_a_kind(self):
|
||||||
|
""" same issue... returns the result of dice.findn(4) """
|
||||||
|
return dice.findn(4)
|
||||||
|
|
||||||
|
dice = FiveDice()
|
||||||
|
successes = 0
|
||||||
|
trials = 1000000
|
||||||
|
|
||||||
|
print("THIS IS THE ORIGINAL SIMULATION:")
|
||||||
|
for trial in tqdm(range(trials)):
|
||||||
|
dice.roll()
|
||||||
|
if dice.all_ones():
|
||||||
|
successes += 1
|
||||||
|
print(successes/trials)
|
||||||
|
|
||||||
|
print("\n")
|
||||||
|
print("THIS IS TO CHECK IF THERE ARE AT LEAST THREE OF A KIND:")
|
||||||
|
successes = 0
|
||||||
|
for trial in tqdm(range(trials)):
|
||||||
|
dice.roll()
|
||||||
|
# if dice.findn(3):
|
||||||
|
if dice.is_three_of_a_kind():
|
||||||
|
successes += 1
|
||||||
|
print(successes/trials)
|
||||||
|
|
||||||
|
print("\n")
|
||||||
|
print("THIS IS TO CHECK IF THERE ARE AT LEAST FOUR OF A KIND:")
|
||||||
|
successes = 0
|
||||||
|
for trial in tqdm(range(trials)):
|
||||||
|
dice.roll()
|
||||||
|
# if dice.findn(4):
|
||||||
|
if dice.is_four_of_a_kind():
|
||||||
|
successes += 1
|
||||||
|
print(successes/trials)
|
Loading…
Reference in New Issue