Skip to content

Latest commit

 

History

History
145 lines (88 loc) · 3.26 KB

polybius.md

File metadata and controls

145 lines (88 loc) · 3.26 KB

Polybius

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).

Cipher behavior information

  • Case sensitive? ❌
  • Deterministic? ✓
  • Alphabet: A-Z, numbers optionally
  • Characters not in alphabet will be: omitted, preserved, throw an error (default)

Default options object

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.

Usage

Encoding

Default

import { polybius } from 'cipher-collection'


console.log(polybius.encode('AB')) // 11 12
console.log(polybius.encode('a')) // 11

Custom key

import { polybius } from 'cipher-collection'


console.log(polybius.encode('Ac', { key: 'cakeIsLie' })) // 12 11

Custom substitution

import { polybius } from 'cipher-collection'


console.log(polybius.encode('IJ')) // 24 24
console.log(polybius.encode('UV', { equalLetters: 'UV' })) // 51 51

With numbers

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

Error handling

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 *

Decoding

Default

import { polybius } from 'cipher-collection'


console.log(polybius.decode('11 12')) // AB

Custom key

import { polybius } from 'cipher-collection'


console.log(polybius.decode('12 11', { key: 'cakeIsLie' })) // AC

Custom substitution

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

With numbers

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

Error handling

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 *