Vigenere cipher encryption for Python
Project description
vigenere-py
This is a Python implementation of the Vigenère cipher, where each letter of the plaintext is shifted according to each letter of the key.
Despite having been invented in the 16th century, the Vigenère cipher is still useful because it's simple enough for anyone to encipher and decipher using only pen and paper. See cipher-wheel/ for images of printable cipher wheels. (Pin the centers together with a brass brad.)
If the key is random and at least as long as the plaintext, the Vigenère cipher is effectively a one-time pad, which provide perfect secrecy when properly used.
However, if the key is reused, not fully random, or shorter than the plaintext, then the cipher can be easily broken through frequency analysis.
As a result, vigenere-py
comes with a keygen
command for generating
suitable random keys, and it will refuse to encrypt text with a key that is too
short unless the --insecure
mode is specified.
Installation
Install this package with pipx
for use as a standalone CLI:
pipx install vigenere-py
vigenere --help
Alternatively, you can install this package for use as a library via pip
(ideally run this inside a virtualenv):
pip install vigenere-py
Usage
For help, run:
vigenere --help
You can also use:
python -m vigenere --help
Alphabets
Several different alphabets are available. The decimal
alphabet expects keys
and ciphertext to be 2-digit decimal numbers. This makes it convenient to
compute by hand because encryption is just adding the numbers modulo 100.
The other alphabets are more traditional ciphers that can be computed on paper
with the help of a table or a cipher wheel. The printable
alphabet contains
all printable ASCII characters with spaces but no other whitespace.
The other alphabets will pass through punctuation like spaces unchanged.
decimal: 100-char full ASCII, ciphertext written as digits aliases: (100|ascii) passthrough: none chars: ␀␉␊␌␍ !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_`abcdefghijklmnopqrstuvwxyz{|}~
printable: All printable characters and spaces aliases: (print|wheel) passthrough: other whitespace chars: !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_`abcdefghijklmnopqrstuvwxyz{|}~
letters: Uppercase letters only aliases: (upper|uppercase) passthrough: punctuation/whitespace chars: ABCDEFGHIJKLMNOPQRSTUVWXYZ
alpha-mixed: Mixed case letters and numbers aliases: (alpha|alphanumeric|alphanumeric-mixed) passthrough: punctuation/whitespace chars: ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789
alpha-upper: Uppercase letters and numbers aliases: (alphanumeric-upper) passthrough: punctuation/whitespace chars: ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789
Examples
Generating a key and encrypting:
$ vigenere genkey -a letters 20 > key.txt
$ cat key.txt
RVRTCLIWHNPZAOJLXEWY
$ cat plain.txt
ATTACK AT DAWN
$ vigenere enc -a letters -k key.txt plain.txt
ROKTEV IP KNLM
Decrypting:
$ cat key.txt
RVRTCLIWHNPZAOJLXEWY
$ cat cipher.txt
ROKTEV IP KNLM
$ vigenere dec -a letters -k key.txt cipher.txt
ATTACK AT DAWN
Interactive mode, end the message with ctrl+d
:
$ vigenere enc -a letters
Key: •••••••••••••••••
Text to encrypt:
SECRET MESSAGE
Ciphertext:
QSWIIT PXZWDUG
Decimal
The decimal
alphabet (aka ascii
or 100
) expects keys and ciphertext to be
encoded as two-digit numbers in base 10.
$ vigenere genkey -a ascii 14 > key.txt
$ cat key.txt
20 95 47 06 32 32 16 88 59 87
$ echo 'Hello, world!' > plain.txt
$ vigenere enc -a ascii -k key.txt plain.txt
18 32 84 77 37 76 86 89 86 97 95 30 76 36
To directly encode or decode from decimal, use vigenere decimal
.
Note that decoded output for the ascii
alphabet may contain control
characters like \0
!
$ export VIGENERE_ALPHABET=ascii
$ echo 'Hello!' | vigenere decimal -e
45 74 81 81 84 06 02
$ echo '45 74 81 81 84 06 02' | vigenere decimal -d
Hello!
$ echo '00 01 02 03 63' | vigenere decimal -d | xxd
00000000: 0009 0a0c 5a ....Z
Decimal encoding also works with other alphabets, if you want that for some reason.
$ echo 'ABCD' | vigenere decimal -a letters -e
00 01 02 03
$ echo '23 24 25 26 27' | vigenere decimal -a alpha -d
XYZab
Bash shell completions
_VIGENERE_COMPLETE=bash_source vigenere > ~/.local/share/bash-completion/completions/vigenere
Development
To contribute to this tool, first checkout the code.
Poetry
Poetry is used to manage dependencies and virtualenvs. So install poetry before proceeding.
I recommend installing poetry with pipx.
pipx install poetry
But if you don't want to use pipx, there are other installation instructions here: https://python-poetry.org/docs/#installation
Installing dependencies
cd vigenere-py
poetry install
Running the app
poetry run vigenere --help
Running tests
poetry run mypy .
poetry run pytest -v
Or, you can run these as a poe
task:
Install poe:
pipx install poethepoet
Run tests:
poe test
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file vigenere_py-0.2.0.tar.gz
.
File metadata
- Download URL: vigenere_py-0.2.0.tar.gz
- Upload date:
- Size: 15.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.0 CPython/3.12.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d006ad9065d2564104f4740f3c510a725bbd42adaef4b6c6b331ec6c4598a462 |
|
MD5 | ef68c2ccd2317ecd482c07ccb20c1de2 |
|
BLAKE2b-256 | bc58e85d6eeb1402b7f3ace893451459c9944d40239b4ac2bfd5958b7cbd1041 |
File details
Details for the file vigenere_py-0.2.0-py3-none-any.whl
.
File metadata
- Download URL: vigenere_py-0.2.0-py3-none-any.whl
- Upload date:
- Size: 15.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.0 CPython/3.12.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2f8b52489b20c570c7c17420d12651208682d6654b6f78cb52ac4afc9401b7a2 |
|
MD5 | 86b1f5d88102f96af317174dd913e650 |
|
BLAKE2b-256 | c3d71224887f22387ad1b3b6e62cf310731d3fdabd9d5f7e93dd06a1bf68a931 |