In [1]:
from message import Message
from key import Key

with open('sample.txt','r') as source:
    sample = source.read()

with open('sample2.txt','r') as source:
    reference = source.read()
    
my_message = Message(sample)
my_reference = Message(reference)
my_reference = my_reference.filter()
filtered_message = my_message.filter()
encryption_key = Key()
encryption_key.random_key()
encrypted_message = filtered_message.map(encryption_key)

natural_frequencies0 = filtered_message.frequencies()
natural_frequencies1 = my_reference.frequencies()
observed_frequencies = encrypted_message.frequencies()

decryption_key0 = Key()
decryption_key0.frequency_key(natural_frequencies0, observed_frequencies)
decryption_key0 = decryption_key0.invert()

decryption_key1 = Key()
decryption_key1.frequency_key(natural_frequencies1, observed_frequencies)
decryption_key1 = decryption_key1.invert()

decrypted_message0 = encrypted_message.map(decryption_key0)
print decrypted_message0.text[0:50]
decrypted_message1 = encrypted_message.map(decryption_key1)
print decrypted_message1.text[0:50]


previous  table of contents  nextprevious  table o
krevioun  table oc fostestn  sextkrevioun  table o

In [2]:
resulting_key = encryption_key.substitute(decryption_key1)
resulting_key.map


Out[2]:
array([ 0,  1,  5,  3,  4,  2, 24,  7,  8,  9, 15, 11, 12, 18, 14, 10, 16,
       17, 13, 19, 20, 21, 22, 23,  6, 25, 26], dtype=int8)

In [1]:
from key import Key
key = Key(range(5))
key.swap(1)
key.map


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

In [4]:
import numpy as np
default_alpha = 'abcdefghijklmnopqrstuvwxyz '
map = np.array(range(27))
def dictionary(map, alpha = default_alpha):
    letter_map = np.zeros([2, len(alpha)], dtype = '|S1')
    for i, x in enumerate(alpha):
        letter_map[0, i] = x
        letter_map[1, i] = alpha[map[i]]
    return letter_map

d = dictionary(map)

In [2]:
import numpy as np
from key import Key
key = Key(np.array(range(27)))
array = key.array_swap()

In [10]:
key1 = Key(np.array(range(5)))
key2 = Key(np.array(range(5)))
np.all(key1.map == key2.map)


Out[10]:
True

In [7]:
key1.map


Out[7]:
[0, 1, 2, 3, 4]

In [6]:
a = 2
if a == 1:
    print 'hello'
else:
    print 'goober'


goober

In [28]:
len(a)


Out[28]:
4

In [1]:
import numpy as np
from key import Key
a = np.array(range(27))
origin = Key(a)
dt = np.dtype(object)
key_list = [x.array_swap() for x in origin.array_swap()]
flat_map_list = []
for list in key_list:
    for key in list:
        flat_map_list.append(key.map)

def remove_duplicates(values):
    output = []
    seen = []
    for value in values:
        #print value
        # If value has not been encountered yet,
        # ... add it to both list and set.
        if not np.any([np.all(value == x) for x in seen]):
            output.append(value)
            seen.append(value)
    return output

new_map_list = remove_duplicates(flat_map_list)

In [7]:
from functions import remove_duplicates

In [27]:
%%timeit
a = np.array(range(1))
origin = Key(a)
dt = np.dtype(object)
key_list = [x.array_swap() for x in origin.array_swap()]
flat_map_list = []
for list in key_list:
    for key in list:
        flat_map_list.append(key.map)
new_map_list = remove_duplicates(flat_map_list)
new_keys1 = [Key(x) for x in new_map_list]


The slowest run took 5.95 times longer than the fastest. This could mean that an intermediate result is being cached 
100000 loops, best of 3: 14.7 µs per loop

In [21]:
len(new_keys1)


Out[21]:
407

In [13]:
%%timeit
a = np.array(range(27))
origin = Key(a)
dt = np.dtype(object)
key_list = [x.array_swap3() for x in origin.array_swap3()]
flat_map_list = []
for list in key_list:
    for key in list:
        flat_map_list.append(key.map)
new_map_list = remove_duplicates(flat_map_list)
new_keys1 = [Key(x) for x in new_map_list]


1 loops, best of 3: 499 ms per loop

In [ ]:
def remove_duplicates(values):
    output = []
    seen = []
    for value in values:
        #print value
        # If value has not been encountered yet,
        # ... add it to both list and set.
        if not np.any([np.all(value == x) for x in seen]):
            output.append(value)
            seen.append(value)
    return output

def key_proliferation(input_keys):
    dt = np.dtype(object)
    key_list = [x.array_swap() for x in input_keys]
    map_list = []
    for list in key_list:
        for key in list:
            map_list.append(key.map)
    filtered_map_list = remove_duplicates(map_list)
    filtered_key_list = [Key(x) for x in filtered_map_list]
    return filtered_key_list
    
origin = Key(a)
dt = np.dtype(object)
key_list = [x.array_swap() for x in origin.array_swap()]
flat_map_list = []
for list in key_list:
    for key in list:
        flat_map_list.append(key.map)
new_map_list = remove_duplicates(flat_map_list)
new_keys1 = [Key(x) for x in new_map_list]

In [5]:
%%timeit
a = np.array(range(27))
origin = Key(a)
dt = np.dtype(object)
key_list = [Key(x) for x in origin.array_swap3()]
key_list = [x.array_swap3() for x in key_list]
flat_map_list = []
for list in key_list:
    for key in list:
        flat_map_list.append(key)
new_map_list = remove_duplicates(flat_map_list)
new_keys3 = [Key(x) for x in new_map_list]


---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-5-5476c99c4161> in <module>()
----> 1 get_ipython().run_cell_magic(u'timeit', u'', u'a = np.array(range(27))\norigin = Key(a)\ndt = np.dtype(object)\nkey_list = [Key(x) for x in origin.array_swap3()]\nkey_list = [x.array_swap3() for x in key_list]\nflat_map_list = []\nfor list in key_list:\n    for key in list:\n        flat_map_list.append(key)\nnew_map_list = remove_duplicates(flat_map_list)\nnew_keys3 = [Key(x) for x in new_map_list]')

C:\Users\Paul Brookes\Anaconda2\lib\site-packages\IPython\core\interactiveshell.pyc in run_cell_magic(self, magic_name, line, cell)
   2291             magic_arg_s = self.var_expand(line, stack_depth)
   2292             with self.builtin_trap:
-> 2293                 result = fn(magic_arg_s, cell)
   2294             return result
   2295 

C:\Users\Paul Brookes\Anaconda2\lib\site-packages\IPython\core\magics\execution.pyc in timeit(self, line, cell)

C:\Users\Paul Brookes\Anaconda2\lib\site-packages\IPython\core\magic.pyc in <lambda>(f, *a, **k)
    191     # but it's overkill for just that one bit of state.
    192     def magic_deco(arg):
--> 193         call = lambda f, *a, **k: f(*a, **k)
    194 
    195         if callable(arg):

C:\Users\Paul Brookes\Anaconda2\lib\site-packages\IPython\core\magics\execution.pyc in timeit(self, line, cell)
   1035             number = 1
   1036             for _ in range(1, 10):
-> 1037                 time_number = timer.timeit(number)
   1038                 worst_tuning = max(worst_tuning, time_number / number)
   1039                 if time_number >= 0.2:

C:\Users\Paul Brookes\Anaconda2\lib\site-packages\IPython\core\magics\execution.pyc in timeit(self, number)
    131         gc.disable()
    132         try:
--> 133             timing = self.inner(it, self.timer)
    134         finally:
    135             if gcold:

<magic-timeit> in inner(_it, _timer)

C:\Users\Paul Brookes\Documents\GitHub\subcipher\key.py in array_swap3(self)
     63 
     64     def array_swap3(self):
---> 65         return [self.swap3(i) for i in range(self.map.size)]
     66 
     67     def array_swap(self):

AttributeError: 'Key' object has no attribute 'size'

In [4]:
from key import Key

In [ ]:


In [177]:
import numpy as np
import random
default_alpha = 'abcdefghijklmnopqrstuvwxyz '

class Key(object):
    def __init__(self, map=[]):
        self.map = map

    def substitute(self, new_map):
        map_out = np.zeros(self.map.shape[0], dtype = np.int8)
        for x in range(self.map.shape[0]):
            map_out[x] = new_map.map[self.map[x]]
        return Key(map_out)

    def invert(self):
        inverted_map = np.zeros(self.map.shape[0], dtype = np.int8)
        for x in range(self.map.size):
            inverted_map[self.map[x]] = x
        return Key(inverted_map)

    def obtain_key(self, alpha, beta):
        key = np.zeros(len(beta))
        for x in range(len(beta)):
            key[x] = alpha.find(beta[x])
        return Key(key)

    def random_key(self, alpha = default_alpha):
        self.map = np.array(range(len(alpha)))
        random.shuffle(self.map)

    def frequency_key(self, natural_frequencies, observed_frequencies):
        natural_indices_sorted = np.argsort(natural_frequencies)
        observed_indices_sorted = np.argsort(observed_frequencies)
        frequency_key = np.zeros(natural_frequencies.size, dtype = np.int8)
        for i, x in enumerate(natural_indices_sorted):
            frequency_key[x] = observed_indices_sorted[i]
        self.map = frequency_key

    def dictionary(self, alpha = default_alpha):
        letter_map = np.zeros([2, len(alpha)], dtype = '|S1')
        for i, x in enumerate(alpha):
            letter_map[0, i] = x
            letter_map[1, i] = alpha[self.map[i]]
        return letter_map

    def swap(self, i):
        swap_map = np.copy(self.map)
        if i == swap_map.size - 1:
            swap_map[i], swap_map[0] = self.map[0], self.map[i]
        else:
            swap_map[i], swap_map[i + 1] = self.map[i + 1], self.map[i]
        return Key(swap_map)

    def array_swap(self):
        return [self.swap(i) for i in range(self.map.size)]
    
key = Key(np.array(range(27)))

In [3]:
from functions import key_proliferation
from key import Key
import numpy as np

a = np.arange(27)
origin = Key(a)
origin = origin.array_swap()
new_keys = key_proliferation(origin)

In [5]:
len(new_keys)


Out[5]:
379

In [5]:
from key import Key
import numpy as np
a = np.arange(27)
key = Key(a)
new = key.swap(30)
new.map


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

In [ ]:


In [ ]:


In [ ]:


In [ ]: