Twofish Python Bindings

0.3.0 · abandoned · verified Wed Apr 15

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

Install

Imports

Quickstart

This example demonstrates basic encryption and decryption of a single 16-byte block using a 128-bit key. Note that this library explicitly handles only 16-byte blocks, and higher-level cipher modes (like CBC or CTR) must be implemented externally or by using a different library.

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}")

view raw JSON →