In [4]:
from message import Message
from key import Key
import numpy as np
alpha = 'abcdefghijklmnopqrstuvwxyz'
message = Message(alpha)
key = np.array(range(26))
key[0] = 25
key[25] = 0
In [10]:
%%timeit
message.map1(key)
In [11]:
%%timeit
message.map2(key)
In [1]:
from message import Message
alpha = 'abcdefghijklmnopqrstuvwxyz'
my_message = Message(alpha)
In [6]:
%%timeit
my_message.frequencies(alpha)
In [21]:
from message import Message
from key import Key
import numpy as np
with open('sample.txt','r') as source:
text = source.read()
my_message = Message(text)
my_message = my_message.filter()
encipher_key = Key()
encipher_key.random_key()
enciphered_message = my_message.map(encipher_key)
enciphered_message.text
decipher_key = encipher_key.invert()
deciphered_message = enciphered_message.map(decipher_key)
#deciphered_message.text
In [22]:
decipher_key.map
Out[22]:
In [1]:
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])
for x in range(self.map.shape[0]):
map_out[x] = new_map[self.map[x]]
return Key(map_out)
def invert(self):
inverted_map = np.zeros(self.map.shape[0])
for x in range(self.map.shape[0]):
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)
key = Key(np.array([1,2,0]))
inverted_key = key.invert()
inverted_key.map
Out[1]:
In [26]:
import numpy as np
class Message(object):
def __init__(self, text, alpha = 'abcdefghijklmnopqrstuvwxyz '):
self.text = text
self.alpha = alpha
def map(self, key):
new_message = ''
for x in self.text:
x_index = self.alpha.find(x)
if x_index == -1:
new_message += x
else:
mapped_to = self.alpha[key.map[x_index]]
new_message += mapped_to
return Message(new_message)
def frequencies(self, alpha = 0):
if alpha == 0:
alpha = self.alpha
counts = np.zeros([len(alpha)])
for i, x in enumerate(alpha):
counts[i] = self.text.count(x)
rates = counts/len(self.text)
return rates
def filter(self, alpha = 0):
if alpha == 0:
alpha = self.alpha
filtered_message = ''
for x in self.text.lower():
if x in alpha:
filtered_message += x
return Message(filtered_message, self.alpha)
from key import Key
import numpy as np
with open('sample.txt','r') as source:
text = source.read()
my_message = Message(text)
my_message = my_message.filter()
#encipher_key = Key()
#encipher_key.random_key()
map = np.array(range(27))
encipher_key = Key(map)
enciphered_message = my_message.map(encipher_key)
enciphered_message.text
decipher_key = encipher_key.invert()
deciphered_message = enciphered_message.map(decipher_key)
deciphered_message.text
In [44]:
import numpy as np
class Message(object):
def __init__(self, text, alpha = 'abcdefghijklmnopqrstuvwxyz '):
self.text = text
self.alpha = alpha
def map(self, key):
new_message = ''
for x in self.text:
x_index = self.alpha.find(x)
if x_index == -1:
new_message += x
else:
mapped_to = self.alpha[key.map[x_index]]
new_message += mapped_to
return Message(new_message)
def frequencies(self, alpha = 0):
if alpha == 0:
alpha = self.alpha
counts = np.zeros([len(alpha)])
for i, x in enumerate(alpha):
counts[i] = self.text.count(x)
rates = counts/len(self.text)
return rates
def filter(self, alpha = 0):
if alpha == 0:
alpha = self.alpha
filtered_message = ''
for x in self.text.lower():
if x in alpha:
filtered_message += x
return Message(filtered_message, self.alpha)
from key import Key
import numpy as np
with open('sample.txt','r') as source:
text = source.read()
my_message = Message(text)
my_message = my_message.filter()
encipher_key = Key()
encipher_key.random_key()
enciphered_message = my_message.map(encipher_key)
enciphered_message.text
decipher_key = encipher_key.invert()
deciphered_message = enciphered_message.map(decipher_key)
deciphered_message.text
Out[44]:
In [66]:
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])
for x in range(self.map.shape[0]):
map_out[x] = new_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]
return Key(frequency_key)
def frequency_key2(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 x in range(frequency_key.size):
frequency_key[natural_indices_sorted[i]] = observed_indices_sorted[i]
return Key(frequency_key)
key = Key()
new_key=key.frequency_key(frequencies,frequencies)
In [68]:
key = Key()
In [73]:
%%timeit
new_key=key.frequency_key(frequencies,frequencies)
In [72]:
%%timeit
new_key=key.frequency_key2(frequencies,frequencies)
In [59]:
np.zeros(3)
Out[59]:
In [60]:
a
Out[60]:
In [62]:
a = np.array([1,3,2])
for x, i in enumerate(a):
print x, i
In [ ]: