Caesar Cipher
The Caesar Cipher is one of the oldest and simplest encryption techniques, named after Julius Caesar who used it to protect military messages around 100 BC. It works by shifting each letter in the plaintext by a fixed number of positions down the alphabet. For example, with a shift of 3, A becomes D, B becomes E, and so on. Despite being used for over 2000 years, it's extremely weak by modern standards with only 25 possible keys, making it trivially breakable by brute force or frequency analysis.
Visualization
Interactive visualization for Caesar Cipher
Interactive visualization with step-by-step execution
Implementation
1function caesarCipher(text: string, shift: number, decrypt: boolean = false): string {
2 if (decrypt) shift = -shift;
3 shift = ((shift % 26) + 26) % 26; // Normalize shift
4
5 return text.split('').map(char => {
6 if (char >= 'a' && char <= 'z') {
7 return String.fromCharCode(((char.charCodeAt(0) - 97 + shift) % 26) + 97);
8 } else if (char >= 'A' && char <= 'Z') {
9 return String.fromCharCode(((char.charCodeAt(0) - 65 + shift) % 26) + 65);
10 }
11 return char;
12 }).join('');
13}
14
15// ROT13 (shift 13, self-inverse)
16function rot13(text: string): string {
17 return caesarCipher(text, 13);
18}
19
20// Brute force attack
21function caesarBruteForce(ciphertext: string): string[] {
22 const results: string[] = [];
23 for (let shift = 0; shift < 26; shift++) {
24 results.push(`Shift ${shift}: ${caesarCipher(ciphertext, shift, true)}`);
25 }
26 return results;
27}Deep Dive
Theoretical Foundation
Caesar Cipher is a substitution cipher where each letter is replaced by another letter a fixed number of positions down the alphabet. For key k, encryption: E(x) = (x + k) mod 26, decryption: D(x) = (x - k) mod 26, where x is letter position (A=0, B=1, ..., Z=25). The cipher wraps around: with k=3, X→A, Y→B, Z→C. Security is virtually non-existent: only 25 possible keys means brute force takes seconds. Frequency analysis also breaks it instantly since letter patterns are preserved (E is still most common). ROT13 (k=13) is a special case that's self-inverse, used for text obfuscation not security.
Complexity
Time
O(n)
O(n)
O(n)
Space
O(n)
Applications
Industry Use
ROT13 (shift 13, self-inverse)
Educational purposes
Puzzle games
Obfuscation (not security)
Use Cases
Related Algorithms
A* Search Algorithm
Informed search algorithm combining best-first search with Dijkstra's algorithm using heuristics. Widely used in pathfinding and graph traversal, A* is optimal and complete when using admissible heuristic. Used in games, GPS navigation, and robotics. Invented by Peter Hart, Nils Nilsson, and Bertram Raphael in 1968.
Convex Hull (Graham Scan)
Find smallest convex polygon containing all points. Graham Scan invented by Ronald Graham in 1972, runs in O(n log n). Essential in computational geometry, computer graphics, and pattern recognition.
Line Segment Intersection
Determine if two line segments intersect. Fundamental geometric primitive used in graphics, CAD, GIS. Uses orientation and collinearity tests.
Vigenère Cipher
Polyalphabetic substitution cipher using keyword. Invented by Giovan Battista Bellaso in 1553, misattributed to Blaise de Vigenère. More secure than Caesar, resists simple frequency analysis.