Unit 7 · Cybersecurity · Lesson 7.3 · Code

Symmetric Encryption in Python

You did twelve letters in four minutes. Python does a whole book in a second. Today you write encrypt(message, shift), its matching decrypt, and a cracker that tries all 25 keys.

Big Idea 5 — Impact of ComputingCED 5.6 · 1.4 ord/chr · Create PT rehearsal

Before this lesson

No reading — code. Keep 1.4 (text as numbers), 3.8 (traversal), and 3.12 (procedures with parameters) open. You finish and test this program in the 7.9 lab.

1

Where We Are

People→ Bits→ Programs→ Internet→ Security→ Big Data→ Impact

Security, in code. Keeping the Create PT skills warm in the middle of a vocabulary unit, on purpose.

2

The Hook

One idea from Unit 1, finally earning its keep. ord("A") is 65. chr(66) is "B". Letters are numbers — so shifting a letter is just adding.

The only hard part is what happens after Z.

3

The Idea

A Caesar cipher is symmetric encryption: the same key (the shift) is used to encrypt and to decrypt. Encrypt with +3; decrypt with −3. Whoever has the key can do both.

The arithmetic for one capital letter c and a shift k:

Python

pos = ord(c) - 65          # A→0 … Z→25
new = (pos + k) % 26        # wrap past Z
result = chr(new + 65)    # back to a letter

AP Pseudocode

pos ← position of c   (A = 0)
new ← (pos + k) MOD 26
result ← letter at new
MOD is the wrap. Y is position 24. 24 + 3 = 27, and 27 MOD 26 = 1, which is B. Without the MOD you’d get position 27 — which isn’t a letter at all.

The model, in a different costume. Same shape as your cipher — traverse, shift, wrap, build a result — on digits instead of letters:

Python

def rotate_digits(code, shift):
    result = ""
    for ch in code:
        d = int(ch)
        result = result + str((d + shift) % 10)
    return result

print(rotate_digits("0429", 3))   # 3752

AP Pseudocode

PROCEDURE rotateDigits(digits, shift)
{
   result ← []
   FOR EACH d IN digits
   {
      APPEND(result, (d + shift) MOD 10)
   }
   RETURN(result)
}
DISPLAY(rotateDigits([0, 4, 2, 9], 3))
4

Try It — Trace One Letter

Before you code the loop, make sure the math is right for a single letter. Pick a letter and a shift.

letter: shift:

Your program (finish and test it in Lab 7.9)

1. encrypt(message, shift) — traverse the string; shift capital letters; leave spaces and punctuation alone; return the ciphertext.

2. decrypt(message, shift) — one line, using encrypt.

3. crack(ciphertext) — print all 25 candidates, each with its shift. Read them; find the English.

AP connection. A procedure with a parameter, iteration over a string, and selection (letter or not?) — three of the four Create PT requirements in one small program.
5

Vocabulary

symmetric encryption Security
Encryption where the same key is used to encrypt and decrypt. Both people must already share that key.
ord() / chr() Security
Python: ord turns a character into its number; chr turns a number back into a character. (Unit 1.4.)
MOD Security
The remainder after division. 27 MOD 26 is 1. It’s what makes the alphabet wrap.
6

Check

Five questions. Trace the arithmetic on paper.

0 of 5 answered
7

Connections

Came from 1.4 — text as numbers. ord and chr were a curiosity in August; now they’re a cipher.
Came from 3.12 — a procedure with a parameter — the shift — that works for any key.
Next in 7.4 — symmetric needs a shared key. How do you share a key with someone you’ve never met, over a network anyone can watch?
Returns in 7.9 — lab day: finish, test, and crack a partner’s message.
← 7.2 Caesar’s Cipher, on Paper