Skip to content
program to encode decode decrypt vigenere cipher
Find file
Pull request Compare This branch is even with jlapeyre:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.


NAME - Vigenere cipher. Encoding, decoding, cracking.

SYNOPSIS [-help] [-man] [-encode] [-decode] [-crack] [-try_length] [-key KEY] [-threshold TH] [-ratio RATIO] [-max_length NUM] [-verbose] [-pod] [-ofile OFILE] [IFILE ...]

See below for more description of these switches.

DESCRIPTION encodes plaintext and either decodes or cracks coded text using the Vigenere cipher. For encoding, the plaintext is supplied in a file IFILE, while the key, which is a string of letters is given on the command line with -key. For decoding the encoded text is supplied in the file IFILE.

The option -freq prints character frequencies and exits without performing any transcoding or cracking.

Before encoding or frequency countng, the iput text is stripped of any non-alphabetic charcters and is converted to upper case. The cracking algorithm assumes that the coded message consists only of characters A-Z.

This program was written for educational purposes.


The option names may be given as unique abbreviations. For example, -dec, or -d rather than -decode.


short help.


long help.

-key KEY (string)

cipher key for encoding or decoding.


encode the message in each input IFILE using KEY. encode is the default and does not need to be specified.


decode the encrypted message in each IFILE.

-ofile OFILE

send output to OFILE rather than standard out (the terminal).


Try to find the key of the encrypted message in IFILE by analyzing character frequencies.

-try_length LEN (positive integer)

Like -crack, except only keys of length LEN are analyzed.

-threshold TH (floating point number)

The threshold score at which the code is considered cracked. Default value is 0.1. The key cracking algorithm rejects scores that are higher than TH. A lower value of TH requires that the trial frequency table matches more closely the standard frequency table.

-ratio RATIO (positive integer)

Only the first RATIO * key_length characters of the encrypted message are analyzed when cracking. The default value of RATIO is 1000. Try increasing this value if the algorithm fails to find the key. If RATIO is zero or negative, then the entire encrypted message is analyzed.

-max_length NUM (positive integer)

The maximum keylength to try when cracking. If this option is not given, the maximum defaults to the length of the encoded message in IFILE.


Compute and print the frequency table of the input file and exit.


Print some informative messages while running.


Write this documentation to the file README.pod.


  • Encode the text in ulysses.txt with key joyce and write the result to codedtext.txt. -key joyce  -o codedtext.txt ulysses.txt
  • Decode the text in codedtext.txt and write the result to decodedtext.txt. -key joyce  -dec  -o decodedtext.txt codedtext.txt
  • Try to find the encryption key used to encode input file codedtext.txt. -crack  codedtext.txt
  • Encode as above, but write output to standard out. -key joyce  ulysses.txt


The cracking algorithm first searches for a key of length one, then of length two, etc. For each key length it computes the frequencies of characters in the input text that would have been encoded by the same position in the keyword. It then repeatedly rotates this freqency sequence and records the rotation that gives the best match to the average frequencies in english language text. It then averages scores from the best match for each position in the key and compares this to a threshold value. If the average score is below the threshold, then these positions are reported as a likely key and the program exits. Otherwise the keylength is incremented and the algorithm continues.

The score is the average over the alphabet of the relative mean squared deviation of the observed frequency from the frequency standard.

Possible Improvements

  • Account for the effect of poisson statistics on the score.
  • Add support for other languages.


Perl 5.10


John Lapeyre <>

Something went wrong with that request. Please try again.