Caesar a popular substitution cipher, where the alphabet is shifted up or down a specified number of positions.
✅ Perfect Decoding
✅ Uppercase letters (A-Z)
✅ Lowercase letters (a-z)
❌ Numbers (0-9)
❌ Symbols (!@#$)
❌ Emojis (😍🤬👩🏾💻)
Numbers, Symbols, and Emojis
Numbers, symbols, and emoji are outputted as-is by this cipher.
What is “Perfect Decoding”?
Perfect Decoding is when the decoded text exactly matches the text that was encoded.
(letterPosition + shift) mod 26
(letterPosition - shift) mod 26
Based on the current settings for Caesar:
shift = 5(letterPosition + 5) mod 26
(letterPosition - 5) mod 26
Hello World!
ABCDEFGHIJKLMNOPQRSTUVWXYZ
abcdefghijklmnopqrstuvwxyz
0123456789
!@#$
✨🦄✨
Mjqqt Btwqi!
FGHIJKLMNOPQRSTUVWXYZABCDE
fghijklmnopqrstuvwxyzabcde
0123456789
!@#$
✨🦄✨
Hello World!
ABCDEFGHIJKLMNOPQRSTUVWXYZ
abcdefghijklmnopqrstuvwxyz
0123456789
!@#$
✨🦄✨
letterPosition is the index of the lettershift any integerletterPositionIndex of the letter in the English alphabet (0-indexed). For example, A=0, B=1, C=2, and so on until Z=25.
shiftAny integer, positive or negative. This number is used to “shift” the alphabet. For example, if shift=1, then B=0, C=1, D=2, until A=25.
mod 26Both formulas for encoding and decoding have mod 26 which performs the modulo (%) operation on the output.
This is a safe-keeping action to guarantee the character is one of the 26 letters in the alphabet. Javascript doesn’t have proper support for mod so this formula is used:
export function mod (a, b) {
return ((a % b) + b) % b
}