Skip to main content

Fast, efficient Blowfish cipher implementation in pure Python (3.4+).

Project description

Fast, efficient Blowfish cipher implementation in pure Python (3.4+).

Dependencies

  • Python 3.4+

Features

  • Fast (well, as fast you can possibly go using only Python 3.4+)

  • Efficient; generators/iterators are used liberally to reduce memory usage

  • Electronic Codebook (ECB) mode

  • Cipher-Block Chaining (CBC) mode

  • Propagating Cipher-Block Chaining (PCBC) mode

  • Cipher Feedback (CFB) mode

  • Output Feedback (OFB) mode

  • Counter (CTR) mode

Installation

If you just need a Blowfish cipher in your Python project, feel free to manually copy blowfish.py to your package directory (license permitting).

distutils

To install the module to your Python distribution, use the included distutils script:

$ python setup.py install

pip

Stable versions can be installed from pypi using pip:

$ pip install blowfish

pip can also install the latest development version directly from git:

$ pip install 'git+https://github.com/jashandeep-sohi/python-blowfish.git'

Development

Want to add a mode of operation? Speed up encryption?

Make your changes to a clone of the repository at https://github.com/jashandeep-sohi/python-blowfish and send me a pull request.

Bugs

Are you having problems? Please let me know at https://github.com/jashandeep-sohi/python-blowfish/issues

Usage

First create a Cipher object with a key.

import blowfish

cipher = blowfish.Cipher(b"Key must be between 8 and 56 bytes long.")

By default this initializes a Blowfish cipher that will interpret bytes using big-endian byte order. This should not be a problem since most implementations use big-endian byte order as well. However, should the need arrive to use little-endian byte order, provide "little" as the second argument.

Block

To encrypt or decrypt a block of data (8 bytes), use the encrypt_block or decrypt_block methods of the Cipher object.

from os import urandom

block = urandom(8)

ciphertext = cipher.encrypt_block(block)
plaintext = cipher.decrypt_block(ciphertext)

assert block == plaintext

As these methods can only operate on 8 bytes of data, they’re of little practical use. Instead, use one of the implemented modes of operation.

Electronic Codebook Mode (ECB)

To encrypt or decrypt data in ECB mode, use encrypt_ecb or decrypt_ecb methods of the Cipher object.

block_multiple_data = urandom(10 * 8) # data to encrypt

ecb_ciphertext_iter = cipher.encrypt_ecb(block_multiple_data)
ecb_plaintext_iter = cipher.decrypt_ecb(b"".join(ecb_ciphertext_iter))

assert block_multiple_data == b"".join(ecb_plaintext_iter)

Cipher-Block Chaining Mode (CBC)

To encrypt or decrypt data in CBC mode, use encrypt_cbc or decrypt_cbc methods of the Cipher object.

iv = urandom(8) # initialization vector
cbc_ciphertext_iter = cipher.encrypt_cbc(block_multiple_data, iv)
cbc_plaintext_iter = cipher.decrypt_cbc(b"".join(cbc_ciphertext_iter), iv)

assert block_multiple_data == b"".join(cbc_plaintext_iter)

Propagating Cipher-Block Chaining Mode (PCBC)

To encrypt or decrypt data in PCBC mode, use encrypt_pcbc or decrypt_pcbc methods of the Cipher object.

pcbc_ciphertext_iter = cipher.encrypt_pcbc(block_multiple_data, iv)
pcbc_plaintext_iter = cipher.decrypt_pcbc(
  b"".join(pcbc_ciphertext_iter),
  iv
)

assert block_multiple_data == b"".join(pcbc_plaintext_iter)

Cipher Feedback Mode (CFB)

To encrypt or decrypt data in CFB mode, use encrypt_cfb or decrypt_cfb methods of the Cipher object. Although CFB mode can be implemented to allow input data of any length, the current implementation does not. So, the input data has to be a block multiple in length.

cfb_ciphertext_iter = cipher.encrypt_cfb(block_multiple_data, iv)
cfb_plaintext_iter = cipher.decrypt_cfb(b"".join(cfb_ciphertext_iter), iv)

assert block_multiple_data == b"".join(cfb_plaintext_iter)

Output Feedback Mode (OFB)

To encrypt or decrypt data in OFB mode, use encrypt_ofb or decrypt_ofb methods of the Cipher object. Like CFB mode, OFB mode can also be implemented to allow input data of any length. As the current implementation does not, input data has to be a block multiple in length.

ofb_ciphertext_iter = cipher.encrypt_ofb(block_multiple_data, iv)
ofb_plaintext_iter = cipher.decrypt_ofb(b"".join(ofb_ciphertext_iter), iv)

assert block_multiple_data == b"".join(ofb_plaintext_iter)

Counter Mode (CTR)

To encrypt or decrypt data in CTR mode, use encrypt_ctr or decrypt_ctr methods of the Cipher object. Although you can use any counter you want, a simple increment by one counter is secure and the most popular. So for convenience sake, a simple increment by one counter is implemented by the blowfish.ctr_counter function.

from operator import xor

non_block_multiple_data = urandom(10 * 8 + 5) # data to encrypt

encrypt_counter = blowfish.ctr_counter(nonce = 0xfaff1fffffffffff, f = xor)
decrypt_counter = blowfish.ctr_counter(nonce = 0xfaff1fffffffffff, f = xor)

ctr_ciphertext_iter = cipher.encrypt_ctr(
  non_block_multiple_data,
  encrypt_counter
)
ctr_plaintext_iter = cipher.decrypt_ctr(
  b"".join(ctr_ciphertext_iter),
  decrypt_counter
)

assert block_multiple_data == b"".join(ctr_plaintext_iter)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

blowfish-0.4.0.tar.gz (30.6 kB view details)

Uploaded Source

blowfish-0.4.0.tar.bz2 (26.2 kB view details)

Uploaded Source

File details

Details for the file blowfish-0.4.0.tar.gz.

File metadata

  • Download URL: blowfish-0.4.0.tar.gz
  • Upload date:
  • Size: 30.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for blowfish-0.4.0.tar.gz
Algorithm Hash digest
SHA256 1b788ea9614b3d0196140f22a3996d04c89addf29a0cec5a84a3db9aec013c2c
MD5 b116ecc81014e33a5408f387d5b2d729
BLAKE2b-256 45b2949c21a2639bc24acd938486c654172173ff66b31f56e69ee5e1cf3c78c8

See more details on using hashes here.

File details

Details for the file blowfish-0.4.0.tar.bz2.

File metadata

  • Download URL: blowfish-0.4.0.tar.bz2
  • Upload date:
  • Size: 26.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for blowfish-0.4.0.tar.bz2
Algorithm Hash digest
SHA256 47d3ee010b6d4735955c797cc91e84f04af517bce518a8675b086ccc2fb01a19
MD5 3129ff1c0b50df3d7b2f1f65a7e54e30
BLAKE2b-256 7a6f79a8c5fa61a58b827845db18058016518de010ab69456776606b3690be1d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page