Basics of Encryption: the Caesar Cipher
Encryption turns information into a form that outsiders cannot read. Using the ancient Caesar cipher we learn the ideas of key, encryption and decryption.
All topics (7 of 11)
Chapter 1. Computer networks
Chapter 2. Information security
Chapter 3. Creating web pages
Cryptography is the science of encryption for protecting information. Encryption converts plain text by a special rule into an unreadable form (ciphertext), and decryption brings it back. So that the rule is not fixed forever, a secret key is used: only a person who knows the key can read the text.
One of the oldest ciphers is the Caesar cipher, which the Roman general Julius Caesar used in his military letters. Each letter of the text is replaced by the letter a set number of places further along the alphabet. The size of the shift is the key. For example, with key 3, A becomes D, B becomes E and Z becomes C, so the word CAT becomes FDW.
To decrypt, letters are shifted back by the same number: in FDW, moving every letter back 3 places gives CAT. In Python this can be written with the functions ord() (character code) and chr() (character from a code): the expression chr((ord(ch) - 65 + key) % 26 + 65) shifts a capital Latin letter by key places. The % operation makes the alphabet wrap from the end back to the start.
The Caesar cipher is very simple and easy to break: the Latin alphabet gives only 25 keys, and they can all be tried. Besides, some letters appear more often than others in a language (E is the most common in English), so frequency analysis cracks the cipher quickly. Modern methods (AES, RSA) are mathematically far more complex and are used in banks and messengers.
Key takeaways
- Encryption turns plain text into ciphertext, and decryption does the reverse.
- The key is the secret value needed to decrypt.
- In the Caesar cipher every letter is shifted along the alphabet by the key.
- The Caesar cipher is simple and is broken by trying keys or by frequency analysis.
Practice tasks
- Encrypt the word “INFORMATIKA” with the Caesar cipher (key 3), then decrypt the result to restore the original word.
- The ciphertext is “KHOOR” and the key is known to be 3. Find the original word.
- Write a Python function
caesar(text, key)that takes a text and a key and shifts only capital Latin letters (other characters stay unchanged).
Review questions
Test yourself: think first, then reveal the answer.
1What are encryption and decryption?Show answerHide answer
Encryption turns plain text into unreadable ciphertext; decryption returns the ciphertext to its original form using the key.2Why is the Caesar cipher not considered secure?Show answerHide answer
There are very few keys (25), so they can all be tried, and the cipher also falls quickly to frequency analysis.
0 of 11 topics completed
Sign in to mark