Twofish Python Bindings
The `twofish` library provides Python bindings for a C implementation of the Twofish symmetric key block cipher, designed by Niels Ferguson. Twofish is a 128-bit block cipher supporting key sizes up to 256 bits, and was a finalist in the AES competition. The current version is 0.3.0, with the last PyPI release in 2013, and the official GitHub repository was archived in January 2025, indicating it is no longer actively maintained.
Warnings
- breaking The `keybase/python-twofish` GitHub repository, which hosts the code for this PyPI package, was archived by its owner on January 22, 2025. This means the library is no longer actively maintained and will not receive updates or bug fixes.
- gotcha This library only implements the core Twofish block cipher, operating exclusively on 16-byte (128-bit) data blocks. It *does not* include higher-level cipher modes (such as CBC, CTR, GCM) or padding schemes.
- gotcha Keys must be binary strings (bytes in Python 3) and can only be 16, 24, or 32 bytes long (corresponding to 128, 192, or 256 bits). Providing an invalid key length will result in a `KeyError`.
- deprecated The library explicitly states compatibility with Python 2.6, 2.7, and 3.3. While it might still function on some newer Python 3 versions, it is not officially supported and may encounter compatibility issues or unexpected behavior.
Install
-
pip install twofish
Imports
- Twofish
from twofish import Twofish
Quickstart
from twofish import Twofish
import os
# Key must be 16, 24, or 32 bytes (128, 192, or 256 bits)
# For a real application, use a securely generated random key
key = os.urandom(16) # 128-bit key
cipher = Twofish(key)
# Data blocks must be exactly 16 bytes
plaintext_block = b'This is a 16-byt'
ciphertext_block = cipher.encrypt(plaintext_block)
decrypted_block = cipher.decrypt(ciphertext_block)
print(f"Original: {plaintext_block}")
print(f"Encrypted: {ciphertext_block.hex()}")
print(f"Decrypted: {decrypted_block}")