Caesar and Vigenère Ciphers

easy
Unit 2 · Strings II0/3 to milestone

Implement two classic ciphers on letters, preserving case. Non-letters pass through unchanged, and don't consume key letters.

  • caesar(text, shift): shift each letter by shift places (negative shifts work, and the alphabet wraps).
  • vigenere(text, key, decrypt=False): shift each letter by the value of the next key letter (a=0 … z=25, case-insensitive), cycling through the key. Decryption shifts the other way. Raise ValueError if the key contains non-letters or is empty.

Then implement crack_caesar(ciphertext): try all 26 shifts and return the shift whose decryption has the most occurrences of the letters e, t, a, o, i and n (smallest shift on ties). Return the encryption shift, so caesar(ciphertext, -shift) decrypts.

#strings#security-basics2 visible + 2 hidden tests
solution.py · Python 3.13
Loading editor…
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit
Caesar and Vigenère Ciphers · TechGradify