title: “Creating a Caesar Cipher Animation with Manim” format: revealjs Editor: Visual
Introduction
The Caesar cipher is a substitution cipher that shifts letters by a fixed key (e.g., A→D with key=3). While simple for basic encryption, its lack of complexity makes it impractical for modern security. However, it remains valuable for educational purposes, introducing fundamental cryptography concepts. In industry, stronger algorithms (AES, RSA) dominate, but Caesar cipher principles help in understanding encryption basics, data obfuscation, and historical cryptographic evolution.
Presentation Overview & Learning Outcomes This presentation demonstrates a Manim-animated Caesar cipher, visually explaining encryption through letter shifting, frequency analysis, and dynamic effects. Users will learn:
How the cipher works
Visualizing cryptographic concepts
Creating engaging educational animations
Combining programming with mathematical principles for clear explanations.
1. Setting Up the Scene
class EnhancedCaesarAnimation(Scene):
def construct(self):
# Main animation sequence goes here
1. Setting Up the Scene
- Defines the animation class.
- Inherits from Manim’s
Scene
class. - All animation logic will go in the
construct
method.
2. Creating the Background Grid
def create_background_grid(self):
= VGroup()
grid for i in range(-8, 9):
= Line(
h_line =LEFT * 8 + UP * i,
start=RIGHT * 8 + UP * i,
end=0.5,
stroke_width=0.3,
stroke_opacity=BLUE_D
color
)= Line(
v_line =LEFT * i + UP * 4,
start=LEFT * i + DOWN * 4,
end=0.5,
stroke_width=0.3,
stroke_opacity=BLUE_D
color
)
grid.add(h_line, v_line)return grid
2. Creating the Background Grid
- Creates a grid of horizontal and vertical lines.
- Uses
VGroup
to group all lines together. - Lines are semi-transparent (opacity = 0.3) and blue (
BLUE_D
).
3. Creating the Alphabet Circle
= string.ascii_lowercase
alphabet = 3
radius = len(alphabet)
num_letters
= VGroup()
letter_circles = [BLUE_A, BLUE_B, BLUE_C, BLUE_D, BLUE_E]
colors
for i, letter in enumerate(alphabet):
= i * TAU / num_letters
angle = radius * np.array([np.cos(angle), np.sin(angle), 0])
position = colors[i % len(colors)]
color
= Circle(radius=0.3, color=color, stroke_width=4)
circle = Text(letter).scale(0.5)
text
text.move_to(circle.get_center())= VGroup(circle, text)
combined
combined.move_to(position) letter_circles.add(combined)
3. Creating the Alphabet Circle
- Arranges 26 letters in a circular layout using polar coordinates.
- Each letter gets a colored circle background.
- Uses
TAU
(2π) for even distribution around the circle. - Letters and circles are grouped for easy animation.
4. Text Displays
= "Omnianalytics"
input_text = 3
key
= Text(f"Input: {input_text}", font_size=18, color=WHITE)
input_display = Text(f"Key: {key}", font_size=18, color=RED)
key_display = Text(f"Encrypted: ", font_size=18, color=WHITE) encrypted_text_display
4. Text Displays
- Displays three text objects:
- The input text for encryption.
- The encryption key.
- The (incrementally built) encrypted output.
5. Frequency Histogram:
def create_frequency_histogram(self, text):
= {char: text.lower().count(char) for char in string.ascii_lowercase}
freq_dict = max(freq_dict.values())
max_freq
= VGroup()
histogram = [BLUE, RED, GREEN, YELLOW, PURPLE, ORANGE]
colors
for i, (char, freq) in enumerate(freq_dict.items()):
= (freq / max_freq) * 2.5
height = colors[i % len(colors)]
color = Rectangle(
bar =height,
height=0.3,
width=0.8,
fill_opacity=color
color* (i * 0.4 - 3) + UP * (height/2 - 3))
).move_to(RIGHT
= Text(char, font_size=16).next_to(bar, DOWN, buff=0.1)
label
histogram.add(VGroup(bar, label))
return histogram
5. Frequency Histogram
- Creates a bar chart showing frequency of each letter in the input text.
- Bars are colored and labeled with their corresponding letter.
- Bar height is normalized so the tallest bar always has the same height.
6. Particle Effects
def create_particle_effect(self, point):
= VGroup()
particles = [YELLOW_A, YELLOW_B, YELLOW_C, YELLOW_D]
colors
for _ in range(8):
= np.random.uniform(0, TAU)
angle = np.random.uniform(0.3, 0.6)
radius = np.random.choice(colors)
color = Circle(radius=0.02, fill_opacity=1, color=color)
particle
particle.move_to(point)
particles.add(particle)
= point + radius * np.array([np.cos(angle), np.sin(angle), 0])
target_point lambda m, dt, target=target_point: m.shift(
particle.add_updater(- m.get_center()) * dt * 2
(target =m.get_fill_opacity() - dt))
).set_fill(opacity
return particles
6. Particle Effects
- Creates an explosion effect when a letter is selected.
- Particles move outward from the source and fade away.
- Uses updaters to animate and fade particles smoothly.
7. Encryption Animation
for i, char in enumerate(input_text):
if char.isalpha():
= alphabet.index(char.lower())
current_pos = (current_pos + key) % 26
new_pos
# Highlight source letter
# Create arrow to target letter
# Animate target letter with glow effect
# Update encrypted text display
7. Encryption Animation
- For each character in the input:
- Finds its position in the alphabet.
- Shifts by the key (modulo 26).
- Animates:
- Highlight of the source letter.
- Arrow from source to encrypted letter.
- Particle and glow effects.
- Updates the encrypted text display.
8. Glow Effect Animation
= Circle(
glow_effect =0.5,
radius=GREEN,
color=0.4,
fill_opacity=0.2
stroke_opacity
).move_to(letter_circles[new_pos].get_center())
self.play(
2).set_opacity(0),
glow_effect.animate.scale(1.2).set_color(GREEN),
letter_circles[new_pos].animate.scale(=1.0
run_time )
8. Glow Effect Animation
- Creates a glow effect at the encrypted letter:
- Expands outward (scales up).
- Fades away (opacity to 0).
- Simultaneously scales and colors the target letter.
9. Final Cleanup
= VGroup(
all_elements
title, grid, letter_circles,
input_display, key_display,
encrypted_text_display, histogram_group
)
self.play(FadeOut(all_elements, run_time=5))
9. Final Cleanup
- Groups all scene elements.
- Fades everything out smoothly at the end.
run_time
controls the fade-out duration.
Summary
- Visualizes Caesar cipher encryption step-by-step.
- Shows frequency analysis of input text.
- Uses smooth animations, effects, and clear composition.
- Manim makes it easy to combine math, text, and custom effects for educational animations.