The purpose of this post is to outline with a practical example the use in tandem of what to me were two obscure methods in Python. These methods belong to the string module. They are string.translate() and string.maketrans(). More on these methods later.

First some back background; a quick introduction to substitution ciphers. A cipher is the method by which you jumble up a plain text message into something that hopefully keeps it secret. Upon getting the ciphered message this jumbling up process in reversed to get back the original message. That is it, in essence.

Let us assume the English alphabet contains only 5 letters instead of the usual 26. These letters are ‘aeguv’ and with them you made the word ‘vague‘. You want to send this special word to someone as a secret. With a substitution cipher we would replace each letter with another character so that it loses it human meaning. With our example we could use the following substitutions or substitution table:

‘a’ becomes ‘1’,

‘g’ becomes ‘2’,

‘u’ becomes ‘3’,

‘v’ becomes ‘4’,

‘e’ becomes ‘5’

Using this our secret word becomes

41235

The receiver of this word would need to know how the substitutions works and then apply apply this knowledge in reverse to discover the message.

The above is known as a simple substitution cipher as it only operates on single letters. Using what we learnt here I wrote a script which takes any text string containing characters from the English alphabet and Roman numerals and to output a ciphered message.

So for example, taking out secret message to be “the quick brown fox jumps over the lazy dog” we get “4(% 15)#+ “2/7. &/8 *5-03 /6%2 4(% ,!:9 $/’


1    # coding=UTF-8 
2    # http://www.python.org/peps/pep-0263.html 
3    """ 
4    This module implements a simple substitute cipher 
5    using the maketrans() and translate functions of the 
6    string module 
7    """ 
8     
9     
10   # imports 
11   import sys 
12   import string 
13   # constants 
14   # exception classes 
15   # interface functions 
16   # classes 
17   # internal functions & classes 
18    
19   class Simplecipher(object): 
20       """Simplecipher is a implementation of a simple substution cipher""" 
21       def __init__(self): 
22           self._from = self.make_from() 
23           self._to = self.make_to() 
24           self.encode_table = self.encoding_table() 
25           self.decode_table = self.decoding_table() 
26        
27       def make_from(self): 
28           """ Create the full character set that our message uses """ 
29           # 'abcdefghijklmnopqrstuvwxyz0123456789' 
30           return string.ascii_lowercase + string.digits 
31    
32       def make_to(self): 
33           """ Return a string of 36 character used to substitute with """ 
34           # 'chr' converts ascii code to an ascii character 
35           result = "".join([chr(i+32) for i in range(1,37)])  
36           return result 
37    
38       def encoding_table(self): 
39           if len(self._from) != len(self._to): 
40               raise "The inputs to maketans() are not of the same length" 
41           else: 
42               # from > to 
43               return string.maketrans(self._from, self._to) 
44    
45       def decoding_table(self): 
46           if len(self._from) != len(self._to): 
47               raise "The input to maketans() are not of the same length" 
48           else: 
49               # to > from 
50               return string.maketrans(self._to, self._from) 
51    
52       def make_secret(self,message=None): 
53           """ cipher an input string """ 
54           result = string.translate(message, self.encode_table) 
55           return result 
56    
57       def find_message(self,secret_message=None): 
58           """ decode an inout string """ 
59           result = string.translate(secret_message, self.decode_table) 
60           return result 
61            
62    
63   def main(): 
64       # create a new Simplecipher object 
65       simp_ciph = Simplecipher() 
66       # the message we want to cipher 
67       message = 'the quick brown fox jumps over the lazy dog' 
68       # cipher the message 
69       secret = simp_ciph.make_secret(message) 
70    
71       print message 
72       print secret 
73        
74       # decode the secret message 
75       print simp_ciph.find_message(secret) 
76        
77   if __name__ == '__main__': 
78       status = main() 
79       sys.exit(status)
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s