In [89]:
encrypted_message.group_frequencies(2)
np.sum(encrypted_message.rates[0] != 0)


Out[89]:
88

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)


botixe nuek feoe borluneo nuai gtdrsuem suredms ldtfril frnu nuero tfi uean byoiril beurim a jerd th sctwe orsril hotc nue moaltis itsnords nue drlun th vyeinkis ntopu fasuem tjeo spades th maow loeei nue loeei th ctss ri nue meeg fttms an mysw qysn behtoe nue dasn drlun hames 
botixe nuek feoe borluneo nuai gtdrsuem suredms ldtfril frnu nuero tfi uean byoiril beurim a jerd th sctwe orsril hotc nue moaltis itsnords nue drlun th vyeinkis ntopu fasuem tjeo spades th maow loeei nue loeei th ctss ri nue meeg fttms an mysw qysn behtoe nue dasn drlun hames 

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)


ble lan hees hors a timmy at rugerris war to tle boitl os tle ren word ow tle trunest 
[ 0  7 23 13  4 22 10 11 20 21  3 12 25 18 14  2 16 17  1 19  8  6 15  9 24
  5 26]
11
0

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)


bronze thel were brayhter thin podasheg shaedgs ydowany wath thear own heit burnany behang i vead of smoke rasany from the griyons nostrads the dayht of quentlns torch wisheg over scides of girk yreen the yreen of moss an the geep woogs it gusk just before the dist dayht figes 
[ 0  1  2  5  4  3  6  7  8  9 17 10 12 13 14 15 16 18 11 19 20 21 22 23 24
 25 26]
43
1

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


batize nuek reae badgunea nusi ftldouem oudelmo gltrdig rdnu nueda tri uesn byaidig beudim s jedl th octwe adodig hatc nue masgtio itonadlo nue ldgun th qyeinkio ntapu rsouem tjea opsleo th msaw gaeei nue gaeei th ctoo di nue meef rttmo sn myow vyon behtae nue lson ldgun hsmeo 
[ 0  1  6  7  2 10  5  4 13  3 14 11 19 18 12  9  8 17 22 16 20 23 15 21 24
 25 26]

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


cloaxe sheb tele clynhsel shia podyrheg rhyedgr ndotyan tysh sheyl ota heis culayan cehyag i veyd om rfowe lyryan mlof she glinoar aorslydr she dynhs om jueasbar solkh tirheg ovel rkider om gilw nleea she nleea om forr ya she geep toogr is gurw qurs cemole she dirs dynhs miger 
[ 0  1  7  5  4 18  3  9  8 11 17 10 22 13 16  2 14  6 20 19 12 21 15 25 26
 23 24]

In [6]:
result = encryption_key.substitute(key_group2.keys[0])
print result.map


[14  6 15  7  4  5 13  8 17 25  2 22 20 19 11 10 16 18 26 12 24 21  3 23  1
  9  0]

In [7]:
result = encryption_key.substitute(key_group2.keys[1])
print result.map


[14  6 15  7  4  5 13  8 17 25 20 22  2 19 11 10 16 18 26 12 24 21  3 23  1
  9  0]

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


ctoize myep bete ctanymet myri lodasyeg syaedgs ndobain bamy myeat obi yerm cutiain ceyaig r vead of skowe tasain ftok mye gtrnois iosmtads mye danym of queimpis mothy brsyeg ovet shrdes of grtw nteei mye nteei of koss ai mye geel boogs rm gusw jusm cefote mye drsm danym frges 
[ 0  1 14  9  4  3  5  7 18  2 17 10  8 13 22 20 16  6 19 11 12 21 15 23 24
 25 26]

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


valize stem ueae vangtsea stri fldnotec otnedco gdlunig unst stena lui ters vhainig vetnic r bend ly oplke anonig yalp ste carglio ilosando ste dngts ly wheismio slaxt urotec lbea oxrdeo ly crak gaeei ste gaeei ly ploo ni ste ceef ullco rs chok jhos veylae ste dros dngts yrceo 
[ 0  1  7  9 11  6  5  4  2  3 12 10 24  8 19 13 18 17 14 16 21 20 22 23 25
 15 26]

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


ice sooyer stinciosem std ice itlledi icarp icad daye of ice bouritard madarp iserig feei thove ice happedi deriareld try dolyaem wared ar ice dummouryarp sooyd 
[19  7 13 24  4  5 15  2  0  9 16 11  1 17 14 22 23 12  3  8 20 21 18 10  6
 25 26]

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


she hyd beeo bnro y tullz yt riverruo fyr tn the snuth no the red fnrk nf the trideot 
[24  1  2  3  4  5  6  7  8  9 10 11 12 14 13 15 16 17 18 19 20 21 22 23 25
  0 26]

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]:
1.7887127752102296e-05

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)


10000 loops, best of 3: 71.7 µs per loop

In [20]:
%%timeit
a = np.argsort(metric_array)


10000 loops, best of 3: 38.1 µs per loop

In [14]:
x = time.time()
a = np.argsort([1,3,4])
y = time.time()
print "%.20f" % (y-x)


0.00000000000000000000

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


she had ween worn a tully at riverrun far to the south on the red fork of the trident 
[ 0 22  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21  1 23 24
 25 26]

In [19]:
range(4)


Out[19]:
[0, 1, 2, 3]

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


she had been born a tummx at rjverrun far to the south on the red fork of the trjdent 
[ 0 22  6  3  4  5  1  7  8  9 18 11 15 14 13 10 12 17  2 19 20 21 16 23 24
 25 26]

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)


7.89952192123
67135.6866491

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)


19646
1.0
Out[52]:
5.0900946757609692e-05

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]:
'she had beeo bnro a tully at riverruo far tn the snuth no the red fnrk nf the trideot '

In [7]:
a= np.arange(1000,dtype=np.int8)
b = np.arange(1000,dtype=np.int32)

In [9]:
%%timeit
for i in a:
    pass


10000 loops, best of 3: 39 µs per loop

In [10]:
%%timeit
for i in b:
    pass


10000 loops, best of 3: 39.5 µs per loop

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]:
'she had beeo bnro a tully at riverruo far tn the snuth no the red fnrk nf the trideot '

In [7]:
decrypted_message = encrypted_message.map(key_group.keys[0])
decrypted_message.text


Out[7]:
'she haf been born a tully at riwerrun dar to the south on the ref dork od the trifent '

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


10 loops, best of 3: 89.7 ms per loop

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]:
'she had been born a tully at riverrun far to the south on the red fork of the trident '

In [ ]:
new_message

In [17]:
%%timeit
encrypted_message.triplet_frequencies()


10000 loops, best of 3: 140 µs per loop

In [18]:
%%timeit
encrypted_message.quadruplet_frequencies()


100 loops, best of 3: 3.53 ms per loop

In [ ]:


In [ ]:


In [ ]:


In [41]:
sum([a,b])


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-41-fc391cc0073c> in <module>()
----> 1 sum([a,b])

TypeError: unsupported operand type(s) for +: 'int' and 'list'

In [ ]:


In [ ]:


In [7]:
%%timeit
original_message.group_frequencies(2)


1000 loops, best of 3: 232 µs per loop

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]:
1.0

In [32]:
group = 'fork'
alpha = 'abcdefghijklmnopqrstuvwxyz '
a = tuple([alpha.find(group[i]) for i in range(4)])
print a


(5, 13, 17, 10)

In [35]:
alpha.find(group[1])


Out[35]:
13

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)


10000 loops, best of 3: 186 µs per loop

In [16]:
%%timeit
quadruplet_dictionary_metric(original_message, natural_sample)


1000 loops, best of 3: 207 µs per loop

In [10]:
%%timeit
metric_function(original_message, natural_sample)


1000 loops, best of 3: 201 µs per loop

In [4]:
%%timeit
for i in range(5):
    key_group1.advance([proliferation_generator(i) for i in [2]], pair_metric, 10)


1 loops, best of 3: 703 ms per loop

In [106]:
a = [1,1,1]

In [108]:
a.append(None)

In [109]:
a


Out[109]:
[1, 1, 1, None]

In [ ]:


In [ ]:


In [ ]:


In [2]:
import numpy as np
a = np.zeros([4,4])

In [4]:
a.shape[0]


Out[4]:
4L

In [ ]: