Skip to main content

Format Preserving Encryption with support for dates, numbers, floats, and strings with any character

Project description

FPE Plus

Format Preserving Encryption for Python
Encrypt dates, numbers, floats, and strings while preserving format and length.

PyPI version License Python Version


Table of Contents

  1. Features
  2. Installation
  3. Usage
  4. Dependencies
  5. License
  6. Author & Contact

Features

  • 🔒 Format Preserving Encryption (FPE) for dates, numbers, floats, and strings
  • 🔤 Supports all characters (letters, digits, special characters)
  • 📄 CSV encryption/decryption with column format specification
  • 🛡️ No external dependencies (pure Python)
  • 🏷️ Preserves length and format for structured data

Installation

pip install fpe_plus

Usage

Quick Start

from fpe_plus import FPETypeHandler, FPEExtended, FPECSVHandler

# Generate secure key and tweak
key = FPEExtended.generate_key()
tweak = FPEExtended.generate_tweak(7)

# Create a type handler
handler = FPETypeHandler(key, tweak, mode="FF3-1")

# Encrypt and decrypt a date
date = "2023-12-25"
enc_date = handler.encrypt_date(date)
dec_date = handler.decrypt_date(enc_date)
print(f"Date: {date} -> {enc_date} -> {dec_date}")

# Encrypt and decrypt a float
float_str = "123.456"
enc_float = handler.encrypt_float(float_str)
dec_float = handler.decrypt_float(enc_float)
print(f"Float: {float_str} -> {enc_float} -> {dec_float}")

# Encrypt and decrypt a string
text = "hello@world!123"
enc_str = handler.encrypt_string(text)
dec_str = handler.decrypt_string(enc_str)
print(f"String: {text} -> {enc_str} -> {dec_str}")

# Encrypt and decrypt digits
number = "123456"
enc_num = handler.encrypt_digit(number)
dec_num = handler.decrypt_digit(enc_num)
print(f"Digits: {number} -> {enc_num} -> {dec_num}")

# Encrypt and decrypt a CSV file
csv_handler = FPECSVHandler(key, tweak, mode="FF3-1")
formats = ['STRING', 'DATE', 'FLOAT', 'DIGITS']
csv_handler.encrypt_csv('input.csv', 'encrypted.csv', formats)
csv_handler.decrypt_csv('encrypted.csv', 'decrypted.csv', formats)

API Reference

FPEExtended

Core format-preserving encryption engine.

  • FPEExtended(key: bytes, tweak: bytes, mode: str = "FF3-1")
    • key: 16, 24, or 32 bytes
    • tweak: 7 bytes for FF3-1
    • mode: "FF3-1" (default)
  • generate_key(): Generate a secure random key
  • generate_tweak(length=7): Generate a secure random tweak
  • encrypt(plaintext: str, radix: int = None) -> str: Encrypt any string
  • decrypt(ciphertext: str, radix: int = None) -> str: Decrypt any string

FPETypeHandler

Type-specific encryption/decryption.

  • FPETypeHandler(key: bytes, tweak: bytes, mode: str = "FF3-1")
  • encrypt_date(date_str: str, date_format: str = "%Y-%m-%d") -> str
  • decrypt_date(ciphertext: str, date_format: str = "%Y-%m-%d") -> str
  • encrypt_float(value) -> str
  • decrypt_float(ciphertext, return_type=str) -> str|float
  • encrypt_digit(value) -> str
  • decrypt_digit(ciphertext, return_type=str) -> str|int
  • encrypt_string(plaintext: str) -> str
  • decrypt_string(ciphertext: str) -> str

FPECSVHandler

CSV column-wise encryption/decryption.

  • FPECSVHandler(key: bytes, tweak: bytes, mode: str = "FF3-1")
  • encrypt_csv(input_path: str, output_path: str, formats: list)
  • decrypt_csv(input_path: str, output_path: str, formats: list)
    • formats: List of column types ('DATE', 'FLOAT', 'STRING', 'DIGITS')

Developer Tips & Best Practices

  • Always use secure random keys and tweaks for production.
  • Specify correct column formats for CSV encryption.
  • For digits, ensure even length for FF3-1 compatibility (handler pads automatically).
  • Use caching for repeated values to improve performance.
  • All encryption is reversible if you keep the key and tweak safe.
  • No external dependencies: works out-of-the-box with Python 3.7+.


Dependencies

None (uses Python standard library).


License

This project is licensed under the MIT License.


Author & Contact

Vasudev Jaiswal
Email: vasudevjaiswal786@gmail.com


Made with ❤️ by Vasudev Jaiswal

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

fpe_plus-0.1.0.tar.gz (11.2 kB view details)

Uploaded Source

Built Distribution

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

fpe_plus-0.1.0-py3-none-any.whl (10.0 kB view details)

Uploaded Python 3

File details

Details for the file fpe_plus-0.1.0.tar.gz.

File metadata

  • Download URL: fpe_plus-0.1.0.tar.gz
  • Upload date:
  • Size: 11.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.13

File hashes

Hashes for fpe_plus-0.1.0.tar.gz
Algorithm Hash digest
SHA256 b3dfe6c22c86eb320eed50976eb4ec2258603d5528a9f6719ab115150548f295
MD5 0b571f305de92d7fcb6d8685510e3efb
BLAKE2b-256 984bab498efcf83c78e9cba99546ed23f1ce391f5a5f049e9e3d8dcf0118063d

See more details on using hashes here.

File details

Details for the file fpe_plus-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: fpe_plus-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 10.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.13

File hashes

Hashes for fpe_plus-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 fbfc63ba40606f19bea5a9b64d82d4324056ebdfcb03ca0c71e230f1fbb19a93
MD5 64f0b428d629e7d66f5909c620d2fd95
BLAKE2b-256 67df54464413a514291de6c5307ba4302a827a0769f840d1ef7ca73d951cf9ac

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