Skip to main content

A pure-Python Feistel Network Block Cipher Implementation

Project description

PyFeistel

A pure-Python Feistel Network Block Cipher Implementation.

** Documentation**: For a comprehensive analysis of the mathematical foundations, memory model, and architectural decisions, please consult the PyFeistel Design Document.

[!WARNING] Educational Use Only: This library is designed for educational purposes to demonstrate cryptographic principles. While it produces statistically random output, it has not undergone formal cryptanalysis and is likely vulnerable to advanced attacks (e.g., linear/differential cryptanalysis). Do not use this for protecting sensitive production data.

Features

  • Custom Feistel Network: 16-round architecture with non-linear Key Schedule.
  • Modes of Operation: CBC (Cipher Block Chaining) and CTR (Counter) modes.
  • Educational Design: Pure Python implementation using bytearray for clarity and secrets for security.
  • Visualization: Tools to visualize cryptographic properties like the Avalanche Effect.

Installation

pip install .

Or

pip install pyfeistel

Usage

Basic Encryption (CBC Mode)

import secrets
from pyfeistel import FeistelCipher, CBCMode, pad, unpad

# 1. Setup Cipher
key = secrets.token_bytes(32)  # 256-bit key
cipher = FeistelCipher(key)
cbc = CBCMode(cipher)

# 2. Prepare Data
plaintext = b"Hello, PyFeistel! This is a secret message."
padded_data = pad(plaintext)
iv = secrets.token_bytes(16)   # 128-bit IV

# 3. Encrypt
ciphertext = cbc.encrypt(padded_data, key, iv)
print(f"Ciphertext (Hex): {ciphertext.hex()}")

# 4. Decrypt
# (Note: CBC decryption extracts IV automatically if prepended, 
# but here we pass the IV explicitly if handled separately, 
# or rely on the implementation's handling).
# Our implementation prepends IV to ciphertext in 'encrypt'.
decrypted_padded = cbc.decrypt(ciphertext, key)
decrypted = unpad(decrypted_padded)

print(f"Decrypted: {decrypted.decode()}")
assert decrypted == plaintext

Testing & Verification

The library includes a robust test suite to verify both correctness and cryptographic properties.

1. Unit & Functional Tests

Verifies correct round-trip encryption/decryption for all modes (CBC, CTR) and padding logic.

  • Command: pytest tests/test_core.py tests/test_modes.py
  • Result: ✅ Passed (Correctness verified)

2. Avalanche Effect

Verifies that flipping 1 bit in the plaintext flips approximately 50% of bits in the ciphertext.

  • Command: pytest tests/test_avalanche.py
  • Result: ✅ Passed (Average Hamming Distance ~64 bits)

3. NIST Statistical Analysis

Integrated with the NIST SP 800-22 test suite (via nistrng) to check for statistical randomness.

  • Command: python tests/test_nist_stats.py
  • Scope: Checked 160,000 bits of CTR-mode output.
  • Results: Verified basic randomness properties.
    • Monobit (Frequency): ✅ PASS (p=0.50) - Output has equal 0s/1s.
    • Runs Test: ✅ PASS - Bit transition frequency is random.
    • Spectral/Complexity: ⚠️ Mixed (Sample size limitation vs educational scope).

Examples

Real-World Example: Secure File Encryption Tool

A complete command-line tool for encrypting and decrypting files using PyFeistel in CBC mode.

Location: examples/secure_file_tool.py

Usage:

# Encrypt
python examples/secure_file_tool.py encrypt secret.txt secret.enc

# Decrypt
python examples/secure_file_tool.py decrypt secret.enc secret_restored.txt

Avalanche Effect Visualization

PyFeistel includes a visualization tool to demonstrate the Avalanche Effect.

You can run the visualization script:

python examples/visualize_avalanche.py

This script generates a heatmap tracing the bit differences through all 16 rounds of the cipher.

Avalanche Effect Heatmap

Figure: Heatmap showing bit diffusion. Dark pixels represent bit differences between two encrypted blocks differing by only 1 bit in the plaintext. Note how the differences spread (diffuse) rapidly after the first few rounds.

License

This project is licensed under the MIT License.

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

pyfeistel-0.1.1.tar.gz (63.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyfeistel-0.1.1-py3-none-any.whl (12.1 kB view details)

Uploaded Python 3

File details

Details for the file pyfeistel-0.1.1.tar.gz.

File metadata

  • Download URL: pyfeistel-0.1.1.tar.gz
  • Upload date:
  • Size: 63.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.11

File hashes

Hashes for pyfeistel-0.1.1.tar.gz
Algorithm Hash digest
SHA256 d82b5ee8a84334806f13eeb23217702ee5c386974eb820a2c2f55920005f9add
MD5 560015b8a04f82e4c97d3a31702d95bb
BLAKE2b-256 e508e8f89d562993054454f640cbf9d6ebc04397f0ff8a7560238cba70317834

See more details on using hashes here.

File details

Details for the file pyfeistel-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: pyfeistel-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 12.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.11

File hashes

Hashes for pyfeistel-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 98d5a845152c18b758595b5d10deb149621535c347427d82355306372d25392b
MD5 cef087e9b764a865a23c0e81ca13f4f3
BLAKE2b-256 f46700b99c7279995f5ce147e9f71f3839bb699a9b26c0c4152b185d2467dde9

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