Security News
PyPI Introduces Digital Attestations to Strengthen Python Package Security
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.
feistel-cipher
Advanced tools
This is a TypeScript library implementing the Feistel cipher for format-preserving encryption (FPE).
The main objective of this library is not to provide a secure encryption scheme but rather a safe obfuscation tool.
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:
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$).
NB: You may also read my original white paper here as well as the latest one on the full FPE version.
npm i feistel-cipher
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 Buffer
.
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
.
import * as feistel from 'feistel-cipher'
const source = 'my-source-data'
// Encrypt
const cipher = new feistel.Cipher('some-32-byte-long-key-to-be-safe', 10)
const obfuscated = cipher.encrypt(source)
// Decrypt
const deciphered = cipher.decrypt(obfuscated)
assert(deciphered == source)
NB: This is the same default behaviour as in my Golang implementation (see below).
You may also want to use your own set of keys with CustomCipher
and a number of rounds depending on the number of provided keys, eg.
const cipher = new feistel.CustomCipher([
'1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef',
'9876543210fedcba9876543210fedcba9876543210fedcba9876543210fedcba',
'abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789'
])
Finally, you might want to use the latest FPECipher
providing true format-preserving encryption for strings:
import { SHA_256 } from 'feistel-cipher'
const cipher = new feistel.FPECipher(SHA_256, 'some-32-byte-long-key-to-be-safe', 128)
const obfuscated = cipher.encrypt(source)
assert(obfuscated.length, source.length)
If you want to use FPE for numbers, you might want to use the encryptNumber()
method on the FPECipher
which will return a number that you may pad if need be to match your requirements:
const obfuscatedNumber = cipher.encryptNumber(sourceNumber)
const deobfuscatedNumber = cipher.decryptNumber(obfuscatedNumber)
assert(sourceNumber == deobfuscatedNumber)
NB: For stability and security purposes, the number 0
always returns itself.
This library relies on four dependencies:
Besides, to run the tests, you would need to install live-server
:
npm i -g live-server
To run specific tests for NodeJS, run the following: (eg. decryptNumber
in node.spec.ts)
$ npm run test-node -- --grep "decryptNumber"
For those interested, I also made two other implementations of these Feistel ciphers:
This module is distributed under a MIT license.
See the LICENSE file.
FAQs
Feistel cipher implementation for format-preserving encryption
We found that feistel-cipher demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.
Security News
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.