caesar

package module
v0.0.0-...-91e6095 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 30, 2019 License: MIT Imports: 5 Imported by: 0

README

caesar

a classic cipher cli

Installation

$ go install github.com/stripedpajamas/caesar/cmd/...
$ caesar help

generally the syntax is

caesar -c <cipher> -k <key> -t <text> <encrypt|decrypt>

leave off -t <text> and caesar will read from stdin.

optionally you can also provide a --groups or -g parameter to specify how the output should be grouped. the default value is 5. groups setting is ignored for decrypt operations.

# encrypting using caesar cipher
$ caesar -c caesar -k m -t "peter" encrypt
BQFQD
$ caesar -c caesar -k m -t "BQFQD" decrypt
PETER

ciphers implemented:

generally all the ciphers operate on english alphabetic letters (a-z). plaintext and keys are both specified as letters (although a number works for the caesar cipher as well, see below).

Using Different Ciphers

Caesar
  • encryption removes all non-alphabetic characters (encrypting abc123 == encrypting abc)
  • the key can be a letter or a number
    • the letter x is interpreted as a = x for the shift
    • the number n is interpreted as shift the alphabet n places. concretely, this means keys a, b, c, ... are equivalent to keys 0, 1, 2, ....
    • any number provided is wrapped around mod 26
    • if a multi-letter key is provided, all but the first letter is ignored
  • example:
    $ caesar -c caesar -k m -t "peter" encrypt
    BQFQD
    $ caesar -c caesar -k m -t "BQFQD" decrypt
    PETER
    
Playfair
  • encryption removes all non-alphabetic characters (encrypting "abc123" == encrypting "abc")
  • the key should be a keyword (anything else would be equivalent encrypting without a key)
  • j is merged with i, so decrypting an encryption of "Joel" will result in "Ioel"
  • odd-length plaintext and double-letters are padded with X, unless the double letter is an X, then it is padded with Q
  • example:
    $ caesar -c playfair -k secret -t "peter" encrypt
    OCSCC Y
    $ caesar -c playfair -k secret -t "OCSCCY" decrypt
    PETERX
    
Vigenère
  • encryption removes all non-alphabetic characters (encrypting "abc123" == encrypting "abc")
  • non-alphabetic characters are ignored if present in the key
  • an empty key will return an error
  • example:
    $ caesar -c vigenere -k hotsauce -t "let's eat some cheerios" encrypt
    SSMKE UVWVA XUHYG VPCL
    $ caesar -c vigenere -k hotsauce -t "SSM KEU VWV AXU HYG VPC L" decrypt
    LETSEATSOMECHEERIOS
    
ADFGX
  • encryption removes all non-alphabetic characters (encrypting "abc123" == encrypting "abc")
  • this is not ADFGVX, so numbers are no supported
  • two keys are required for this cipher, one for the polybius square and one for the transposition step
    • the keys should be comma delimited when running (e.g. -k keyOne,keyTwo)
  • non-alphabetic characters are ignored if present in either key
  • if either key is empty, it will return an error
  • example:
    $ caesar -c adfgx -k help,me -t "this is a test" encrypt
    GAAFA FXGDF GGAFG FGAGA GG
    $ caesar -c adfgx -k help,me -t "GAAFA FXGDF GGAFG FGAGA GG" decrypt
    THISISATEST
    
Bifid
  • encryption removes all non-alphabetic characters (encrypting "abc123" == encrypting "abc")
  • non-alphabetic characters are ignored if present in either key
  • example:
    $ caesar -c bifid -k golden -t spandex encrypt
    SAGXW DX
    $ caesar -c bifid -k golden -t sagxwdx decrypt
    SPANDEX
    

Other options

  • group output in chunks
    $ caesar -g 10 -c caesar -k m -t "mary had a little lamb whose fleece was white as snow" encrypt
    YMDKTMPMXU FFXQXMYNIT AEQRXQQOQI MEITUFQMEE ZAI
    
  • read from stdin
    $ cat my_love_letter.txt | caesar -c caesar -k 5 encrypt
    NKDTZ WJWJF INSLY MNXYM FYXUW JYYDH TTQ
    
  • chaining (careful when chaining ciphers that use a polybius square with ciphers that don't, as the I/J situation can get wonky)
    $ echo "potatoes or potatos" | caesar -c caesar -k 7 e | caesar -c vigenere -k "poptarts" e
    LJPAA MERKM LOAYT NO
    $ echo "LJPAA MERKM LOAYT NO" | caesar -c vigenere -k "poptarts" d | caesar -c caesar -k 7 d
    POTATOESORPOTATOS
    

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ADFGX

type ADFGX struct{}

ADFGX represents the ADFGX cipher and conforms to the Cipher interface https://en.wikipedia.org/wiki/ADFGVX_cipher

func (ADFGX) Decrypt

func (a ADFGX) Decrypt(ciphertext, key string) (string, error)

Decrypt operates on a ciphertext string and a key string that consists of two keys delimited by a semicolon (,). The function first transposes the letters according to key2, and then undoes the substitution using an alphabet square and key1.

func (ADFGX) Encrypt

func (a ADFGX) Encrypt(plaintext, key string) (string, error)

Encrypt operates on a plaintext string and a key string that consists of two keys delimited by a semicolon (,). The function constructs an alphabet square from key 1, and obtains substitution values from it. Then key 2 is used to transpose the values into the finished ciphertext.

type Bifid

type Bifid struct{}

Bifid represents the Bifid cipher and conforms to the Cipher interface https://en.wikipedia.org/wiki/Bifid_cipher

func (Bifid) Decrypt

func (b Bifid) Decrypt(ciphertext, key string) (string, error)

Decrypt operates on a ciphertext string and a key string The function constructs and alphabet square from the key, and obtains substitution values from it. The substitution values are de-transposed into values that are looked up in the square to obtain the original plaintext string.

func (Bifid) Encrypt

func (b Bifid) Encrypt(plaintext, key string) (string, error)

Encrypt operates on a plaintext string and a key string The function constructs an alphabet square from the key, and obtains substitution values from it. The substitution values are transposed and the transposed values are converted back into letters

type Caesar

type Caesar struct{}

Caesar represents the classic Caesar cipher and conforms to the Cipher interface https://en.wikipedia.org/wiki/Caesar_cipher

func (Caesar) Decrypt

func (c Caesar) Decrypt(ciphertext string, key string) (string, error)

Decrypt converts alphabetic characters to their up-shifted values based on the key parameter (e.g. a shifted 1 = z)

func (Caesar) Encrypt

func (c Caesar) Encrypt(plaintext string, key string) (string, error)

Encrypt converts alphabetic characters to their down-shifted values based on the key parameter (e.g. a shifted 1 = b)

type Cipher

type Cipher interface {
	Encrypt(string, string) (string, error)
	Decrypt(string, string) (string, error)
}

Cipher represents an old school cipher with encryption and decryption methods that operate on ASCII inputs and keys

type Playfair

type Playfair struct{}

Playfair represents the Playfair cipher and conforms to the Cipher interface https://en.wikipedia.org/wiki/Playfair_cipher

func (Playfair) Decrypt

func (pf Playfair) Decrypt(ciphertext string, key string) (string, error)

Decrypt constructs a playfair alphabet from key, and then decrypts ciphertext using it

func (Playfair) Encrypt

func (pf Playfair) Encrypt(plaintext string, key string) (string, error)

Encrypt constructs a playfair alphabet from key, and then encrypts plaintext using it

type Vigenere

type Vigenere struct{}

Vigenere represents the polyalphabetic substitution Vigenere cipher and conforms to the Cipher interface https://en.wikipedia.org/wiki/Vigen%C3%A8re_cipher

func (Vigenere) Decrypt

func (v Vigenere) Decrypt(ciphertext, key string) (string, error)

Decrypt converts alphabetic characters to their corresponding values based on the key parameter

func (Vigenere) Encrypt

func (v Vigenere) Encrypt(plaintext, key string) (string, error)

Encrypt converts alphabetic characters to their corresponding values based on the key parameter

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL