In [89]:
encrypted_message.group_frequencies(2)
np.sum(encrypted_message.rates[0] != 0)
Out[89]:
In [ ]:
In [ ]:
In [9]:
from functions import remove_duplicates, metric_function, key_proliferation_swap, best_keys, proliferation_generator, key_proliferation, metric_function2
from functions import quadruplet_dictionary_metric, triplet_dictionary_metric, dict_metric_generator, pair_metric
import numpy as np
from message import Message
from key import Key
from key_group import Key_Group
with open('sample.txt','r') as source:
my_sample = source.read()
with open('message3.txt','r') as source:
my_message = source.read()
natural_sample = Message(my_sample)
natural_sample = natural_sample.filter()
original_message = Message(my_message)
original_message = original_message.filter()
#encryption_key = Key(np.arange(27))
encryption_key = Key(np.array([0, 1, 6, 3, 2, 9 , 5 , 7, 13, 4, 14 ,10, 19 ,18 ,12 ,16, 8,11, 22, 17, 20, 23, 15, 25, 26, 21, 24]))
#for i in range(1500):
# encryption_key.pair_scramble()
encrypted_message = original_message.map(encryption_key)
natural_frequencies = natural_sample.frequencies()
observed_frequencies = encrypted_message.frequencies()
natural_sample.group_frequencies(2)
natural_sample.group_frequencies(3)
natural_sample.group_frequencies(4)
#natural_sample.group_frequencies(5)
print encrypted_message.text
freq_attempt = Key()
freq_attempt.frequency_key(natural_frequencies, observed_frequencies)
trial_map = np.arange(27)
#trial_map[26] = freq_attempt.map[26]
#trial_map[freq_attempt.map[26]] = 26
decryption_attempt = Key(trial_map)
#decryption_attempt = freq_attempt.invert()
#decryption_attempt = encryption_key.invert()
new_keys = [decryption_attempt]
key_group = Key_Group(new_keys,encrypted_message,natural_sample)
decrypted_message = encrypted_message.map(new_keys[0])
print decrypted_message.text
key_group1 = Key_Group(new_keys,encrypted_message,natural_sample)
key_group2 = Key_Group(new_keys,encrypted_message,natural_sample)
In [ ]:
In [28]:
for i in range(5):
key_group1.advance([proliferation_generator(i) for i in [2,3,4]], dict_metric_generator(5,5e-6), 5)
key_group1.advance([key_proliferation_swap], dict_metric_generator(2,5e-6), 3)
#key_group1.advance([key_proliferation_swap], quadruplet_dictionary_metric, 30)
decrypted_message = encrypted_message.map(key_group1.keys[0])
print decrypted_message.text
result = encryption_key.substitute(key_group1.keys[0])
print result.map
print quadruplet_dictionary_metric(decrypted_message, natural_sample)
print quadruplet_dictionary_metric(original_message, natural_sample)
In [13]:
for i in range(5):
#key_group1.advance([proliferation_generator(i) for i in [2,3,4]], dict_metric_generator(4,5e-6), 5)
key_group1.advance([key_proliferation_swap], dict_metric_generator(4,5e-6), 20)
#key_group1.advance([key_proliferation_swap], quadruplet_dictionary_metric, 30)
decrypted_message = encrypted_message.map(key_group1.keys[0])
print decrypted_message.text
result = encryption_key.substitute(key_group1.keys[0])
print result.map
print quadruplet_dictionary_metric(decrypted_message, natural_sample)
print quadruplet_dictionary_metric(original_message, natural_sample)
In [10]:
for i in range(10):
key_group1.advance([proliferation_generator(i) for i in [2,3,4]], pair_metric, 10)
key_group1.advance([key_proliferation_swap], pair_metric, 10)
decrypted_message = encrypted_message.map(key_group1.keys[0])
print decrypted_message.text
result = encryption_key.substitute(key_group1.keys[0])
print result.map
In [14]:
import time
for i in range(5):
v = time.time()
key_group2.proliferate([proliferation_generator(i) for i in [2,3,4]],10)
w = time.time()
key_group2.rank(pair_metric)
x = time.time()
key_group2.proliferate([key_proliferation_swap],10)
y = time.time()
key_group2.rank(pair_metric)
z = time.time()
#print "%.20f" % (w-v), (x-w), (y-x), (z-y)
decrypted_message = encrypted_message.map(key_group2.keys[0])
print decrypted_message.text
result = encryption_key.substitute(key_group2.keys[0])
print result.map
In [6]:
result = encryption_key.substitute(key_group2.keys[0])
print result.map
In [7]:
result = encryption_key.substitute(key_group2.keys[1])
print result.map
In [6]:
for i in range(10):
#key_group2.advance([proliferation_generator(i) for i in [2,3,4]], pair_metric, 5)
key_group2.advance([key_proliferation_swap], pair_metric, 5)
decrypted_message = encrypted_message.map(key_group2.keys[0])
print decrypted_message.text
result = encryption_key.substitute(key_group2.keys[0])
print result.map
In [11]:
for i in range(10):
key_group1.advance([proliferation_generator(i) for i in [2,3,4]], dict_metric_generator(3,5e-8), 10)
key_group1.advance([key_proliferation_swap], dict_metric_generator(3,5e-8), 3)
decrypted_message = encrypted_message.map(key_group1.keys[0])
print decrypted_message.text
result = encryption_key.substitute(key_group1.keys[0])
print result.map
In [77]:
for i in range(40):
#key_group1.advance([proliferation_generator(i) for i in [2,3,4]], metric_function, 10)
key_group1.advance([key_proliferation_swap], metric_function, 20)
decrypted_message = encrypted_message.map(key_group1.keys[0])
print decrypted_message.text
result = encryption_key.substitute(key_group1.keys[0])
print result.map
In [ ]:
In [17]:
natural_sample.quadruplet_frequencies()
for i in range(5):
key_group2.advance([proliferation_generator(i) for i in [2,3,4]], dict_metric_generator(4,5e-14), 1)
#key_group2.advance([key_proliferation_swap], dict_metric_generator(4,5e-8), 3)
decrypted_message = encrypted_message.map(key_group2.keys[0])
print decrypted_message.text
result = encryption_key.substitute(key_group2.keys[0])
print result.map
In [9]:
from message import Message
with open('sample.txt','r') as source:
test_file = source.read()
test_message = Message(test_file)
test_message.filter()
test_message.quadruplet_frequencies()
test_message.rates[(5, 14, 17, 10)]
Out[9]:
In [19]:
import random
metric_list = [random.random() for x in range(1000)]
metric_array = np.array(metric_list)
In [18]:
%%timeit
a = np.argsort(metric_list)
In [20]:
%%timeit
a = np.argsort(metric_array)
In [14]:
x = time.time()
a = np.argsort([1,3,4])
y = time.time()
print "%.20f" % (y-x)
In [3]:
natural_sample.triplet_frequencies()
for i in range(1):
key_group1.advance([proliferation_generator(i) for i in [2,3,4]], metric_function, 10)
key_group1.advance([key_proliferation_swap], metric_function, 3)
decrypted_message = encrypted_message.map(key_group1.keys[0])
print decrypted_message.text
result = encryption_key.substitute(key_group1.keys[0])
print result.map
In [19]:
range(4)
Out[19]:
In [ ]:
In [11]:
for i in range(10):
key_group2.advance([proliferation_generator(i) for i in [2,3,4]], dict_metric_generator(3,5e-8), 10)
key_group2.advance([key_proliferation_swap], dict_metric_generator(3,5e-8), 3)
decrypted_message = encrypted_message.map(key_group2.keys[0])
print decrypted_message.text
result = encryption_key.substitute(key_group2.keys[0])
print result.map
In [39]:
func1 = dict_metric_generator(4,1e1)
func2 = dict_metric_generator(4,1e-3)
print func1(decrypted_message,natural_sample)
print func2(decrypted_message,natural_sample)
In [52]:
print np.sum(natural_sample.rates != 0)
print np.sum(natural_sample.rates)
np.sum(natural_sample.rates)/np.sum(natural_sample.rates != 0)
Out[52]:
In [ ]:
for i in range(50):
key_group.advance([proliferation_generator(i) for i in [2,3,4]], metric_function, 20)
key_group.advance([key_proliferation_swap], metric_function, 3)
decrypted_message = encrypted_message.map(key_group.keys[0])
decrypted_message.text
In [50]:
for i in range(1):
key_group.advance([proliferation_generator(i) for i in [2,3,4]], quadruplet_dictionary_metric, 20)
key_group.advance([key_proliferation_swap], quadruplet_dictionary_metric, 20)
decrypted_message = encrypted_message.map(key_group.keys[0])
decrypted_message.text
Out[50]:
In [7]:
a= np.arange(1000,dtype=np.int8)
b = np.arange(1000,dtype=np.int32)
In [9]:
%%timeit
for i in a:
pass
In [10]:
%%timeit
for i in b:
pass
In [38]:
key_group = Key_Group(new_keys,encrypted_message,natural_sample)
In [4]:
for i in range(1):
key_group.advance([proliferation_generator(i) for i in [2,3,4]], dict_metric_generator(4,1e-12), 20)
key_group.advance([key_proliferation_swap], dict_metric_generator(4,1e-12), 20)
decrypted_message = encrypted_message.map(key_group.keys[0])
decrypted_message.text
Out[4]:
In [7]:
decrypted_message = encrypted_message.map(key_group.keys[0])
decrypted_message.text
Out[7]:
In [5]:
%%timeit
new_keys = [decryption_attempt]
for i in range(1):
new_keys5 = key_proliferation_swap(new_keys)
new_key_list = new_keys5
new_keys = best_keys(new_key_list, encrypted_message, natural_sample, 1)
decrypted_message = encrypted_message.map(new_keys[0])
decrypted_message.text
In [34]:
zero_map = np.arange(27)
zero_key = Key(zero_map)
zero_group = Key_Group([zero_key],original_message,natural_sample)
In [36]:
for i in range(1):
zero_group.advance([proliferation_generator(i) for i in [2,3,4]], metric_function, 20)
new_message = original_message.map(zero_group.keys[0])
new_message.text
Out[36]:
In [ ]:
new_message
In [17]:
%%timeit
encrypted_message.triplet_frequencies()
In [18]:
%%timeit
encrypted_message.quadruplet_frequencies()
In [ ]:
In [ ]:
In [ ]:
In [41]:
sum([a,b])
In [ ]:
In [ ]:
In [7]:
%%timeit
original_message.group_frequencies(2)
In [ ]:
In [ ]:
In [ ]:
In [30]:
from message import Message
with open('fragment.txt','r') as file:
text= file.read()
message = Message(text)
message.filter()
message.quadruplet_frequencies()
In [31]:
message.rates[5,14,17,10]
Out[31]:
In [32]:
group = 'fork'
alpha = 'abcdefghijklmnopqrstuvwxyz '
a = tuple([alpha.find(group[i]) for i in range(4)])
print a
In [35]:
alpha.find(group[1])
Out[35]:
In [ ]:
%%timeit
message.alt_frequencies()
In [ ]:
In [9]:
original_message.quadruplet_frequencies()
In [10]:
original_message.quadruplet_frequency_dictionary()
In [ ]:
a = np.zeros(1)
In [ ]:
a
In [ ]:
In [ ]:
In [15]:
natural_sample.quadruplet_frequencies()
In [7]:
from functions import triplet_dictionary_metric, quadruplet_dictionary_metric
In [9]:
%%timeit
triplet_dictionary_metric(original_message, natural_sample)
In [16]:
%%timeit
quadruplet_dictionary_metric(original_message, natural_sample)
In [10]:
%%timeit
metric_function(original_message, natural_sample)
In [4]:
%%timeit
for i in range(5):
key_group1.advance([proliferation_generator(i) for i in [2]], pair_metric, 10)
In [106]:
a = [1,1,1]
In [108]:
a.append(None)
In [109]:
a
Out[109]:
In [ ]:
In [ ]:
In [ ]:
In [2]:
import numpy as np
a = np.zeros([4,4])
In [4]:
a.shape[0]
Out[4]:
In [5]:
len(np.zeros(2))
Out[5]:
In [7]:
np.zeros([2,2])
Out[7]:
In [ ]: