Vigenère is a simple polyalphabetic substitution cipher which uses a tableau composed of each of the 26 options of the Caesar Cipher.
✅ 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.
Given a keyPart, create a string of the length as the string to encode or decode. For example, with a key part of hidden and a string to encode hello world, the full cipher key would be: hiddenhidde
key = makeKey('hidden', 'hello world'.length)
function makeKey (keyPart, targetKeyLength) {
if (typeof keyPart !== 'string' || keyPart.length <= 0) {
return ''
}
const keyPartLen = keyPart.length
let key = keyPart.repeat(Math.ceil(targetKeyLength / keyPartLen))
if (key.length > targetKeyLength) {
key = key.substring(0, targetKeyLength)
}
return key
}
(letterPosition + keyPosition) mod 26
(letterPosition - keyPosition) mod 26
Based on the current settings for Vigenère:
keyPart = "hide"
cipherKey = "hidehidehidehidehidehidehidehidehidehidehidehidehidehidehidehidehidehidehidehidehidehide"
Hello World!
ABCDEFGHIJKLMNOPQRSTUVWXYZ
abcdefghijklmnopqrstuvwxyz
0123456789
!@#$
✨🦄✨
Phpsw Avzoh!
DFJLHJNPLNRTPRVXTVZBXZDFBD
iegkmikoqmosuqswyuwacyaegc
0123456789
!@#$
✨🦄✨
Hello World!
ABCDEFGHIJKLMNOPQRSTUVWXYZ
abcdefghijklmnopqrstuvwxyz
0123456789
!@#$
✨🦄✨
letterPosition is the 0-index of the letter in the standard alphabetkeyPosition given the letter at same letterPosition in the key, get the 0-index of that letter in the alphabetRepeat of the keyPart that matches the length of the string to encode, this key is used to find the keyPosition.
letterPositionIndex of the letter in the English alphabet (0-indexed). For example,
A=0, B=1, C=2, and so on until Z=25.
keyPositionFind the letter at letterPosition in the key, then get the 0-index of that letter in the standard English alphabet.
You’ll see a lot of references to 26, it’s not a magic number, just the
number of letters in the English alphabet.
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
}