Skip to main content

Feistel cipher implementation in Python for format-preserving encryption

Project description

feistel-py

Feistel cipher implementation in Python for format-preserving encryption

GitHub tag (latest by date) GitHub last commit GitHub issues GitHub license PyPI - Version

This is a Python library implementing the Feistel cipher for Format-Preserving Encryption (FPE).

Motivation

The main objective of this library is not to provide a secure encryption scheme but rather a safe obfuscation tool.

Formal description

This library operates on the concept of the Feistel cipher described in Wikipedia as:

A Feistel network is subdivided into several rounds or steps. In its balanced version, the network processes the data in two parts of identical size. On each round, the two blocks are exchanged, then one of the blocks is combined with a transformed version of the other block. Half of the data is encoded with the key, then the result of this operation is added using an XOR operation to the other half of the data. Then in the next round, we reverse: it is the turn of the last half to be encrypted and then to be xored to the first half, except that we use the data previously encrypted. The diagram below shows the data flow (the ${\oplus}$ represents the XOR operation). Each round uses an intermediate key, usually taken from the main key via a generation called key schedule. The operations performed during encryption with these intermediate keys are specific to each algorithm.

The algorithmic description (provided by Wikipedia) of the encryption is as follows:

  • Let $n+1$ be the number of steps, $K_{0},K_{1},...,K_{n}$ the keys associated with each step and $F:\Omega\times\mathcal{K}\mapsto\Omega$ a function of the $(words{\times}keys)$ space to the $words$ space.
  • For each step $i{\in}[0;n]$, note the encrypted word in step $i,m_{i}=L_{i}||R_{i}$:
    • $L_{i+1}=R_{i}$
    • $R_{i+1}=L_{i}{\oplus}F(L_{i},K_{i})$
  • $m_{0}=L_{0}||R_{0}$ is the unciphered text, $m_{n+1}=L_{n+1}||R_{n+1}$ is the ciphered word.

There is no restriction on the $F$ function other than the XOR operation must be possible. For simplicity, we will choose $L_1$ of the same size as $R_1$ and the function $F$ shall transform a word of length $k$ into a word of length $k$ (and this for all $k$).

Usage

pip install feistel-py

To get an obfuscated string from a source data using the SHA-256 hashing function at each round, first instantiate a Cipher object, passing it a key and a number of rounds. Then, use the encrypt() method with the source data as argument. The result will be a byte array. To ensure maximum security, I recommend you use a 256-bit key or longer and a minimum of 10 rounds.

The decryption process uses the obfuscated buffered data and pass it to the decrypt() method of the Cipher.

from feistel import Cipher


source = "my-source-data"

# Encrypt
cipher = Cipher("some-32-byte-long-key-to-be-safe", 10)
obfuscated = cipher.encrypt(source)

# Decrypt
deciphered = cipher.decrypt(obfuscated)

assert deciphered == source, "deciphered should be 'my-source-data'"

NB: This is the exact replica of my other implementations (see below).

You may also use your own set of keys through a CustomCipher instance, eg.

from feistel import CustomCipher


keys = [
    "1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef",
    "9876543210fedcba9876543210fedcba9876543210fedcba9876543210fedcba",
    "abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789",
]
cipher = CustomCipher(keys)

In that case, the number of rounds depends on the number of provided keys.

Finally, you might want to use the latest cipher, providing true format-preserving encryption for strings:

from feistel import FPECipher, SHA_256


cipher = FPECipher(SHA_256, "some-32-byte-long-key-to-be-safe", 128)
obfuscated = cipher.encrypt(source)

assert len(obfuscated) == len(source)

NB: For stability and security purposes, the number 0 always returns itself.

You might also want to use it with the command line:

usage: python3 -m feistel [-h] [-c CIPHER] [-e ENGINE] [-k KEY] [-r ROUNDS] [-o OPERATION] input

positional arguments:
  input                 The string to obfuscate (watch for quotes)

options:
  -h, --help            show this help message and exit
  -c CIPHER, --cipher CIPHER
                        The type of cipher: feistel [default] | custom | fpe
  -e ENGINE, --engine ENGINE
                        The hashing engine [default sha-256]
  -k KEY, --key KEY     The key(s) to use
  -r ROUNDS, --rounds ROUNDS
                        The (optional) number of rounds [default 10]
  -o OPERATION, --operation OPERATION
                        The operation to process : cipher | decipher

Dependencies

The following libraries are necessary:

  • pycryptodome;
  • py-utls.

Tests

$ git clone https://github.com/cyrildever/feistel-py.git
$ cd feistel-py/
$ pip install -e .
$ python3 -m unittest discover

Other implementations

For those interested, I also made two other implementations of these ciphers:

I also created a special library for redacting classified documents using the new FPE cipher. Feel free to contact me about it.

License

This module is distributed under a MIT license.
See the LICENSE file.


© 2024 Cyril Dever. All rights reserved.

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

feistel_py-0.3.0.tar.gz (15.4 kB view details)

Uploaded Source

Built Distribution

feistel_py-0.3.0-py3-none-any.whl (14.0 kB view details)

Uploaded Python 3

File details

Details for the file feistel_py-0.3.0.tar.gz.

File metadata

  • Download URL: feistel_py-0.3.0.tar.gz
  • Upload date:
  • Size: 15.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.31.0 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.3 CPython/3.10.2

File hashes

Hashes for feistel_py-0.3.0.tar.gz
Algorithm Hash digest
SHA256 0008f060f46467e1c0ed89164793c3c366e939af902137ff60fdec59498e382f
MD5 729ebbda209a4aad15cfec65fdae8cbe
BLAKE2b-256 7618dc1140b2f68038541925b5d8cbea2f1463ea04fcc269e23e3b2ee69aee66

See more details on using hashes here.

File details

Details for the file feistel_py-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: feistel_py-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 14.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.31.0 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.3 CPython/3.10.2

File hashes

Hashes for feistel_py-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3b7d5b95e6bb685141635c40e690c36e2f2a2e0a5830ded5c390b6fbf7cfac41
MD5 c61b3c416484fa13c08025cd4c8bdb38
BLAKE2b-256 f8b51f9469c16db9b7c43312faa78bd849fbfc46a244bf294946776f42f7d018

See more details on using hashes here.

Supported by

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