Click each chapter title to expand examples. All Sage examples are editable, so go ahead and try your own examples and code! Please note the following:
The following code defines the shift cipher, defines a plaintext, and encodes it using a shift of 3.
If you don't know the key, you can brute force it, i.e. try all possible keys
We first set up the system, and define a couple functions to do frequency counts. You can ignore this code, but run it before moving to the next block.
Next, define the ciphertext, and do a frequency count.
The next two blocks use the functions defined above to find the most frequent pairs and triples of letters in the cipher text.
Once we have some guesses, start trying to decrypt. Fill in more letters of the key as you solve.
The basic setup
Now let's do a ciphertext-only attack
Once we think we know the key length, we try to find the key: