Hacking Secret Ciphers with Python

(Ann) #1

186 http://inventwithpython.com/hacking


Email questions to the author: [email protected]


Summary


The dictionary data type is useful because like a list it can contain multiple values. However
unlike the list, we can index values in it with string values instead of only integers. Most of the
the things we can do with lists we can also do with dictionaries, such as pass it to len() or use
the in and not in operators on it. In fact, using the in operator on a very large dictionary
value executes much faster than using in on a very large list.


The NoneType data type is also a new data type introduced in this chapter. It only has one value:
None. This value is very useful for representing a lack of a value.


We can convert values to other data types by using the int(), float(), and str() functions.
This chapter brings up “divide-by-zero” errors, which we need to add code to check for and
avoid. The split() string method can convert a single string value into a list value of many
strings. The split() string method is sort of the reverse of the join() list method. The
append() list method adds a value to the end of the list.


When we define functions, we can give some of the parameters “default arguments”. If no
argument is passed for these parameters when the function is called, the default argument value is
used instead. This can be a useful shortcut in our programs.


The transposition cipher is an improvement over the Caesar cipher because it can have hundreds
or thousands of possible keys for messages instead of just 26 different keys. A computer has no
problem decrypting a message with thousands of different keys, but to hack this cipher, we need
to write code that can determine if a string value is valid English or not.


Since this code will probably be useful in our other hacking programs, we will put it in its own
module so it can be imported by any program that wants to call its isEnglish() function. All
of the work we’ve done in this chapter is so that any program can do the following:





import detectEnglish
detectEnglish.isEnglish('Is this sentence English text?')
True





Now armed with code that can detect English, let’s move on to the next chapter and hack the
transposition cipher!

Free download pdf