Math 233: Introduction to Cryptology

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:

  • Code and output does not save, so make sure to copy and paste anything you want to keep.
  • This demo calculator works well for simple examples, but is not a good place to run extensive computational tasks. Don't try cracking the latest RSA challenge here. If you do need more power, Sage is open source and free! (But you still probably won't be cracking any RSA challenges on your laptop...)

Chapter 2 Examples

Example 1: The Caesar (Shift) Cipher

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

Example 2: Affine Ciphers

Example 3: Substitution Cipher: a ciphertext-only attack.

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.

Example 4: Vigenere Ciphers

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:

Chapter 3 Examples

Example 1: Euclidean Algorithm

Example 2: Modular Arithmetic

Example 3: Repeated squaring vs. repeated multiplying

Example 4: Primality Testing