Package: feistel-py
feistel-pyFeistel cipher implementation in Python for format-preserving encryptionThis is a Python library implementing the Feistel cipher for Format-Preserving Encryption (FPE).MotivationThe main objective of this library is not to provide a secure encryption scheme but rather a safe obfuscation tool.Formal descriptionThis library operates on the concept of the Feistel cipher described inWikipediaas: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 (therepresents 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:Letbe the number of steps,the keys associated with each step anda function of thespace to thespace.For each step, note the encrypted word in step:is the unciphered text,is the ciphered word.There is no restriction on thefunction other than the XOR operation must be possible. For simplicity, we will chooseof the same size asand the functionshall transform a word of lengthinto a word of length(and this for all).Usagepip install feistel-pyTo get an obfuscated string from a source data using the SHA-256 hashing function at each round, first instantiate aCipherobject, passing it a key and a number of rounds. Then, use theencrypt()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 thedecrypt()method of theCipher.fromfeistelimportCiphersource="my-source-data"# Encryptcipher=Cipher("some-32-byte-long-key-to-be-safe",10)obfuscated=cipher.encrypt(source)# Decryptdeciphered=cipher.decrypt(obfuscated)assertdeciphered==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 aCustomCipherinstance, eg.fromfeistelimportCustomCipherkeys=["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:fromfeistelimportFPECipher,SHA_256cipher=FPECipher(SHA_256,"some-32-byte-long-key-to-be-safe",128)obfuscated=cipher.encrypt(source)assertlen(obfuscated)==len(source)NB: For stability and security purposes, the number0always 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 | decipherDependenciesThe following libraries are necessary:pycryptodome;py-utls.Tests$gitclonehttps://github.com/cyrildever/feistel-py.git$cdfeistel-py/$pipinstall-e.$python3-munittestdiscoverOther implementationsFor those interested, I also made two other implementations of these ciphers:InGolangas an executable;InTypescriptfor the browser;InScalafor the JVM.I also created a special library for redacting classified documents using the new FPE cipher. Feel free tocontact meabout it.LicenseThis module is distributed under a MIT license.See theLICENSEfile.© 2024 Cyril Dever. All rights reserved.