The Polybius cipher, named by a greek philosopher, utilizes a 5x5 square (or 6x6 with numbers) called Polybius square to encrypt and decrypt messages. It contains the cipher alphabet, modified by the key (distinct letters of the key first, then the rest of the alphabet).
- Case sensitive? ❌
- Deterministic? ✓
- Alphabet: A-Z, numbers optionally
- Characters not in alphabet will be: omitted, preserved, throw an error (default)
const options = {
key: '', // Custom key containing only cipher alphabet characters (duplicates allowed)
withNumbers: false, // Use numbers as well (increases square size from 5 to 6)
equalLetters: 'IJ', // Two letters used for substitution
failOnUnknownCharacter: true, // Throw an error on unknown characters
omitUnknownCharacter: true // If no error should be thrown, omit character?
}
ATTENTION: The equalLetters
option will only be used when withNumbers
is set to false.
The second character set will be substituted with the first one (default: J will become I).
This is necessary because a 5x5 square can only bear 25 characters.
import { polybius } from 'cipher-collection'
console.log(polybius.encode('AB')) // 11 12
console.log(polybius.encode('a')) // 11
import { polybius } from 'cipher-collection'
console.log(polybius.encode('Ac', { key: 'cakeIsLie' })) // 12 11
import { polybius } from 'cipher-collection'
console.log(polybius.encode('IJ')) // 24 24
console.log(polybius.encode('UV', { equalLetters: 'UV' })) // 51 51
import { polybius } from 'cipher-collection'
console.log(polybius.encode('Ac012', { withNumbers: true })) // 11 13 53 54 55
// No substitution anymore!
console.log(polybius.encode('IJ')) // 23 24
import { polybius } from 'cipher-collection'
console.log(polybius.encode('N*') // Error: Unknown character *
console.log(polybius.encode('N*', { failOnUnknownCharacter: false })) // 33
console.log(polybius.encode('N* ', { failOnUnknownCharacter: false, omitUnknownCharacter: false })) // 33 *
import { polybius } from 'cipher-collection'
console.log(polybius.decode('11 12')) // AB
import { polybius } from 'cipher-collection'
console.log(polybius.decode('12 11', { key: 'cakeIsLie' })) // AC
import { polybius } from 'cipher-collection'
console.log(polybius.decode('24 24')) // I I
console.log(polybius.decode('51', { equalLetters: 'UV' })) // U
console.log(polybius.decode('51', { equalLetters: 'VU' })) // V
import { polybius } from 'cipher-collection'
console.log(polybius.decode('11 13 53 54 55', { withNumbers: true })) // AC012
// No substitution anymore!
console.log(polybius.decode('23 24')) // IJ
import { polybius } from 'cipher-collection'
console.log(polybius.decode('33*') // Error: Unknown character *
console.log(polybius.decode('33*', { failOnUnknownCharacter: false })) // N
console.log(polybius.decode('33* ', { failOnUnknownCharacter: false, omitUnknownCharacter: false })) // N *