What is Caesar's Cipher?

Caesar's Cipher (Caesar's Shift Cipher) is one of the simplest and most well-known encryption techniques. Named after Julius Caesar, it works by shifting each letter in the plaintext by a fixed number of places down or up the alphabet. For example, with a shift of 3, the letter A becomes D, B becomes E, and so on.
This cipher is a form of substitution cipher and is an example of symmetric encryption, meaning the same key is used for both encryption and decryption.
A Brief History
Julius Caesar reportedly used this cipher to communicate with his generals securely. By shifting letters three places in the alphabet, he ensured that intercepted messages would be meaningless to anyone unaware of the key. Despite its simplicity, Caesar's Cipher introduced the idea of encryption as a means to protect sensitive information, a concept foundational to modern cryptography.
The Caesar cipher's vulnerability was recognized as early as the 9th century by Al-Kindi, a Persian mathematician, cryptanalyst, and philosopher. Al-Kindi wrote a book called "A Manuscript on Deciphering Cryptographic Messages", where he described the technique of frequency analysis. This method, which looks for patterns in the frequency of letters, could easily expose the Caesar cipher’s key by analyzing the most common letters in a ciphertext.
The Equation for Caesar's Cipher
The transformation in Caesar's Cipher can be represented mathematically:
E(x) = (x + n) mod 26
Where:
E(x) is the encrypted letter.
x is the position of the plaintext letter in the alphabet (0 for A, 1 for B, ..., 25 for Z).
n is the shift value (key).
mod 26 ensures the result wraps around within the bounds of the alphabet.
Decryption reverses this process:
D(x) = (x - n + 26) mod 26
Implementing Caesar's Cipher in Go
Below is an implementation of Caesar's Cipher in Go, complete with functions for encryption and decryption.
package main
import (
"fmt"
"strings" )
// Encrypt function
func Encrypt(input string, shift int) string {
var result strings.Builder
shift = shift % 26
for _, char := range input {
if char >= 'A' && char <= 'Z' { // Uppercase letters
result.WriteRune('A' + (char-'A'+rune(shift))%26) }
else if char >= 'a' && char <= 'z' { // Lowercase letters
result.WriteRune('a' + (char-'a'+rune(shift))%26) }
else { result.WriteRune(char) // Non-alphabetic characters remain unchanged
}
}
return result.String()
}
// Decrypt function: reverses the encryption process
func Decrypt(input string, shift int) string {
return Encrypt(input, -shift)
}
func main() {
plaintext := "Hello, World!" shift := 3
// Encrypt the plaintext
encrypted := Encrypt(plaintext, shift)
fmt.Printf("Plaintext: %s\n", plaintext)
fmt.Printf("Encrypted: %s\n", encrypted)
// Decrypt the ciphertext
decrypted := Decrypt(encrypted, shift)
fmt.Printf("Decrypted: %s\n", decrypted)
}
The Encrypt Function iterates over each character in the input string, shifting letters (both uppercase and lowercase) by a specified amount using modular arithmetic to wrap around the alphabet. It leaves non-alphabetic characters like punctuation and spaces unchanged. The Decrypt Function reverses the encryption by applying the same logic with a negative shift. The Main Function demonstrates the encryption and decryption process using the string "Hello, World!" with a shift of 3.
Subscribe to my newsletter
Read articles from Peter Gari directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
