Part 1 - Cracking Codes in Python

July 03, 2020


This is the first post in a multi-part series covering the book Cracking Codes with Python. This book is awesome because it intertwines Python with the fundamentals of cryptography, starting with basic logic and data structures. So you don’t have to come in with an understanding of either subject to get value from this book!

Chapter 1 - Making Paper Cryptography Tools

  • In short, cryptography is the science of using secret codes
  • Cryptographers study secret codes; cryptoanalysts break them

Some Terms

  • Codes - messages that are made to be understandable and publicly available
  • Ciphers - a means of encrypting a message, including an algorithm
  • Ciphertext - text after it’s passed through an encryption algorithm
  • Plaintext - text before it’s encrypted; human-readable

Caesar Cipher

Caesar Cipher Go to this site and it actually spins:

  • Alphanumeric => 0 - 25 corresponding with the Roman alphabet
  • Encrypt => add numbers
  • Decrypt => add numbers
  • This system works until the sum of the original number + the key numbers > 25, then we have to do something else
  • If sum of the original number and the key number is ≥ 26, you need to subtract 26 from it
  • E.g. O is 14 and the key number is 13, so the sum is 27
  • Take 27 - 26 and O (14) is now B (1).

Double Encryption

  • Doesn’t work because it doesn’t add additional strength
  • It’s mostly a waste of effort because you could just use a higher key number that produces a ciphertext further off from the original message (plaintext)
  • If you encrypted some plaintext with two keys that added up to 26, the resulting ciphertext would be the same as the original plaintext

Chapter 2 - Programming in the Interactive Shell

Integers and Floating - Point Values

  • Integers - whole numbers, e.g. 2.0 and 86
  • Floating point - numbers with decimal points, e.g. 3.5
  • Expressions - math problems

    • Made up of values (the numbers)
    • Connected by operators
    • Can have as many numbers in an expression as you want, as long as they’re connected by operators
    • Start expressions at the beginning of the line, with no spaces at the front
    • Python interprets spaces and it’ll change how your instructions are read

Order of Operations

  • Order of operations in Python are similar to how they were in your elementary school math class

    • Parentheses
    • Exponents
    • Multiplication
    • Division
    • Addition
    • Subtraction

Evaluating Expressions

  • Evaluation - when the computer solves the expression
  • Python evaluates an expression by evaluating sub-expression until it’s solved with an answer
  • Python evaluates an expression starting with the with the innermost, left-most parentheses

Storing Values with Variables

  • Save values to use later in the program

Overwriting Variables

  • Change a stored value in variable by entering another assignment statement

Variable Names

  • Case sensitive in Python
  • Good variable name describes the data it contains

Chapter 3 - Strings and Writing Programs

String Concatenation with the + Operator

  • String concatenation => adding 2 string values to create 1 new string by using + operator
  • Python is very literal about concatenation in terms of characters

String Replication with the * Operator

  • Can use the * operator on a string and an integer to do string replication

Getting Characters from Strings Using Indexes

  • Encryption programs often need to get a single character from a string, which you can accomplish through indexing
  • Python indexes start at 0, so index of the 1st character in a string is 0

Negative Indexes

  • Negative indexes - start at the end of a string and go backward
  • Negative index -1 is the last character in a string; -2 is second to last and so forth
  • Fun fact: In 'Hello', -5 and 0 are the indexes for the same character
  • Usually you’ll use the positive indexes, but sometimes it’s easier to use the negative ones

Getting Multiple Characters from Strings Using Slices

  • If you need > 1 character, you can use slicing instead of indexing
  • Take a string like 'howdy' => 'howdy'[0:3] with 0 as the starting index and 3 as the stopping index
  • If 1st index value is left blank, then Python assumes index position 0
  • If 2nd index value is omitted, then Python uses 1st value to end of the string

Printing Values with the print() function

  • Throw these things into a REPL and see what happens!

    • print('thing')
    • Define a variable => spam = 'Al'
    • print('Hello', + spam)

Printing Escape Characters

  • \\ - escape a backlash
  • \' - escape a single quote
  • \"" - escape a double quote
  • \n - create a new line within a string
  • \t - create a tab within a string

Quotes and Double Quotes

  • Use whichever ones you want, but don’t mix them

Write a Hello World

Write it and run it If you run this in your text editor—assuming your Python environment is set up—the program will ask you for your name and tell you it’s nice to meet you. So polite!

I'm Mallory, an Atlanta-based support engineer and programmer. Follow me on Twitter or email me at hello at