CPH Unit 8

Cryptoanalysis

  • the field of code breaking

  • as old as the field as making codes

Bruteforce

  • trying all possibilities

  • appropriate because it takes advantage of the computer’s ability to perform millions of calculations very quickly

  • easily breaks the rail-fence cipher


Rail-fence Cipher

  • easily broken by rail-fence cipher

  • key to rail-fence cipher is the number of rails used

Minmax Pattern

  • when the current decrypted word list contains more known words than any of the previous decryptions, we remember it by setting maxGoodsoFar to the number of real words

Table 8.1 The String Method join

Method

Use

Explanation

.join(list)

seperator.join(list)

String method that returns a string composed of the elements in the list, separated by the string used to call the method

Row-Major Order

  • order of cells that stores all of the values on the first row, one after the other; it then stores all of the values on the second row, then the third row, and so on

  • most common

Column-Major Order

  • order of cells that stores all of the values in the first column, one after another, followed by the second column, the third column, and so on

Substitution Cipher

  • much more difficult to crack than the rail-fence cipher

  • bruteforce is not effective against this

  • can use patterns that help us find most frequently used letters.

Table 8.4 The sort Function for lists

Method

Use

Explanation

sort(key = none, reverse = false)

list.sort(key = sort.key, reverse = true)

Performs an in-place sort of the list— that is, the list is modified

Keyword Parameters:

key: specifies a function to used for comparing items; default is none, which means to sort on the first element

reverse: specifies whether the list should be sorted in reverse (high to low); default is low to high (False)

In-place sort

  • a type of sort method that modifies the list it is sorting

  • does not create a new list, nor return a new value

  • .sort() method uses this

Frequency Analysis

  • way of breaking an encryption by analyzing the popularity of each letter in a text and attaching a rational number representing the frequency

Table 8.6 The setDefault Method for Dictionaries

Method

Use

Explanation

setDefault(key, default)

value = nDict.setDefault(dict[i], defaultValue)

If the key is not in the dictionary, insert key with a value of default and return default. If the key is in the dictionary, return the value for the key

Pattern Matchings

  • finding a word from the dictionary that is the same length and matches the letters of our partially decoded ciphertext

Regular Expressions

  • allow users to see if two strings match, like when we use the == operator, except that in regular expressions we can use wild card characters as part of the match

Capture Group

  • group of characters that in conjunction with match objects, help us learn which characters in the target word matched letters in different parts of our pattern

  • to create a capture group you surround some part of the regular expression with left and right parenthesis

Table 8.11 The zip Built-in Function

Method

Use

Explanation

zip(list1, list2, …)

tList = list(zip(listA, listB)

Iterator that returns tuples composed of parallel elements of each list

Table 8.13 Simple Regular Expression Syntax

Expression

Meaning

.

Match every character

[abc]

Match a or b or c

[^abc]

Match any character other than a or b or c at the beginning of the string

[abc]+

Match one or more occurrences of the characters abc, — for example, b or aba or ccba

[abc]*

Match zero or more occurrences of the characters abc — for example, b or aba or ccba

(regex)

Create a capture group

$

Match at the end of the string

Table 8.14 Regular Expression Module Functions

Method

Use

Explanation

match(pattern, string)

re.match(‘[abc']XY’.’myString’)

Matches any string in myString that starts with a, b, or c followed by XY followed by any character. Returns a Match on success or None

sub(pattern, replacement, string)

re.sub(‘[tv]’, ‘X’, ‘vxyzbgtt’)

Returns ‘XxyzbgXX’. Like replace, except uses regular expression matching.

findall(pattern, string)

re.findall(‘[bc]+’, ’abcdefedcba’)

Retunrs [‘bc’, ‘cb’]. Returns a list of all substrings matching the regular expression

groups()

matchObj.groups()

Returns a tuple of all capture groups matched. matchObj is created by a call to match.

groups(i)

matchObj.groups(2)

Returns a single capture group at index i. matchObj is created by a call to match.