How it works
The Caesar cipher uses modular arithmetic to shift letters:- Convert each letter to a number (A=0, B=1, …, Z=26)
- Add the shift value to each number
- Apply modulo 27 to wrap around the alphabet
- Convert numbers back to letters
This implementation uses 27 characters (A-Z plus Ñ) instead of the traditional 26-letter English alphabet.
Algorithm explanation
Encryption formula
xis the numeric position of the plaintext letter (0-26)shiftis the key (default: 8)E(x)is the encrypted letter position
Decryption formula
Implementation details
The implementation is insustitucion.py and consists of four main functions:
Alphabet mapping
Core functions
Usage example
Basic encryption and decryption
Step-by-step example
Parameters
The plaintext message to encrypt. Only letters A-Z and Ñ are processed; other characters are ignored.
The shift value (key). Can be any integer from 0 to 26. Values outside this range work but are equivalent to
pos % 27.Key characteristics
Total possible keys
Only 27 different keys (0-26)
Character handling
Converts to uppercase and ignores non-alphabetic characters
Alphabet size
27 characters including Ñ
Encryption speed
O(n) linear time complexity
Security notes
Vulnerabilities
- Brute force attack: With only 27 possible keys, an attacker can try all combinations in seconds
- Frequency analysis: Letter frequency patterns are preserved, making cryptanalysis easy
- Known-plaintext attack: If any part of the plaintext is known, the key is immediately revealed
- No key management: The shift value must be securely shared between parties
Educational value
Despite its weaknesses, the Caesar cipher is excellent for learning:
- Basic encryption principles
- Modular arithmetic
- Why simple substitution is insufficient
- How frequency analysis works
Example with Spanish characters
Common use cases
- Educational
- Historical
- Recreational
- Teaching basic cryptography concepts
- Demonstrating substitution ciphers
- Learning modular arithmetic
- Understanding cipher weaknesses
See also
- Transposition cipher - Different approach using rearrangement
- RSA encryption - Modern asymmetric encryption
- Algorithms overview - Compare all three algorithms