GithubHelp home page GithubHelp logo

cyrildever / feistel-cipher Goto Github PK

View Code? Open in Web Editor NEW
19.0 3.0 2.0 1021 KB

Feistel cipher implementation for format-preserving encryption

License: MIT License

TypeScript 91.67% HTML 0.91% JavaScript 7.42%
cryptography feistel-cipher xor-operation encryption format-preserving-encryption algorithm fpe

feistel-cipher's Introduction

feistel-cipher

GitHub tag (latest by date) npm GitHub last commit GitHub issues NPM

This is a TypeScript 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$).

NB: You may also read my original white paper here as well as the latest one on the full FPE version.

Usage

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.

Dependencies

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"

Other implementations

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

License

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


© 2019-2024 Cyril Dever. All rights reserved.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.