Skip to main content

A pure-Python Rijndael (AES) and PBKDF2 library. Python2.7- and Python3-compatible.

Project description

Overview

This package was a remedy to there being no PyPI-published, pure-Python Rijndael (AES) implementations, and that nothing available, in general, was compatible with both Python2 and Python3. The same is true of the PBKDF2 key-expansion algorithm.

The encryptor takes a source generator (which yields individual blocks). There are source-generators provided for both data from a variable and data from a file. It is trivial if you’d like to write your own. The encryptor and decryptor functions are written as generators. Decrypted data has PKCS7 padding. A utility function is provided to trim this (trim_pkcs7_padding).

The implementation includes Python2 and Python3 implementations of both Rijndael and PBKDF2, and chooses the version when loaded.

The default block-size is 128-bits in order to be compatible with AES.

This project is also referred to as pprp, which stands for “Pure Python Rijndael and PBKDF2”.

Installation

Install via pip:

$ sudo pip install pprp

Example

Encrypt and decrypt the data, and compare the results. This example works in both Python 2 and 3.

Top imports and defines:

import io
import os.path
import hashlib

import pprp
import pprp.config

# Make the strings the right type for the current Python version.
def trans(text):
    return text.encode('ASCII') if sys.version_info[0] >= 3 else text

passphrase = trans('password')
salt = trans('salt')
key_size = 32
data = "this is a test" * 100

Do the key-expansion:

key = pprp.pbkdf2(passphrase, salt, key_size)

Create a source from available data:

sg = pprp.data_source_gen(data)

Feed the source into the encryptor:

eg = pprp.rjindael_encrypt_gen(key, sg)

Feed the encryptor into the decryptor:

dg = pprp.rjindael_decrypt_gen(key, eg)

Sink the output into an IO stream, and trim the padding off the last block:

s = io.BytesIO()
ends_at = 0
for block in dg:
    ends_at += pprp.config.DEFAULT_BLOCK_SIZE
    if ends_at >= len(data):
        block = pprp.trim_pkcs7_padding(block)

    s.write(block)

decrypted = s.getvalue()

Check the result:

assert data == decrypted.decode('ASCII')

The following is a portion of the output of the example script (test/example.py). Notice that, due to this being an efficient, generator-based design, the encryption of each block is followed by a decryption:

2014-07-01 12:24:13,182 - pprp.source - DEBUG - Yielding [data] source block: (0)-(0)
2014-07-01 12:24:13,182 - pprp.adapters - DEBUG - Encrypting and yielding encrypted block: (0)
2014-07-01 12:24:13,183 - pprp.adapters - DEBUG - Decrypting and yielding decrpted block: (0)
2014-07-01 12:24:13,183 - pprp.source - DEBUG - Yielding [data] source block: (1)-(16)
2014-07-01 12:24:13,183 - pprp.adapters - DEBUG - Encrypting and yielding encrypted block: (1)
2014-07-01 12:24:13,183 - pprp.adapters - DEBUG - Decrypting and yielding decrpted block: (1)
2014-07-01 12:24:13,183 - pprp.source - DEBUG - Yielding [data] source block: (2)-(32)
2014-07-01 12:24:13,183 - pprp.adapters - DEBUG - Encrypting and yielding encrypted block: (2)
2014-07-01 12:24:13,183 - pprp.adapters - DEBUG - Decrypting and yielding decrpted block: (2)
2014-07-01 12:24:13,184 - pprp.source - DEBUG - Yielding [data] source block: (3)-(48)
2014-07-01 12:24:13,184 - pprp.adapters - DEBUG - Encrypting and yielding encrypted block: (3)
2014-07-01 12:24:13,184 - pprp.adapters - DEBUG - Decrypting and yielding decrpted block: (3)
...

Notes

A different block-size may be passed in to each of the generators. The default block-size can also be passed via environment variable as PPRP_BLOCK_SIZE.

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

pprp-0.2.1.tar.gz (13.9 kB view details)

Uploaded Source

Built Distribution

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

pprp-0.2.1-py2-none-any.whl (20.5 kB view details)

Uploaded Python 2

File details

Details for the file pprp-0.2.1.tar.gz.

File metadata

  • Download URL: pprp-0.2.1.tar.gz
  • Upload date:
  • Size: 13.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for pprp-0.2.1.tar.gz
Algorithm Hash digest
SHA256 a72f86f9846b219b07e7d0d3ca9e5fdb896dc5d8ef25986784724ce79df1d66b
MD5 656787fe42b07350c7876312728bae26
BLAKE2b-256 d93e9615954f8e74580efe2942a7bf43935f3902a27ba2ef92f7b49d1a1a077d

See more details on using hashes here.

File details

Details for the file pprp-0.2.1-py2-none-any.whl.

File metadata

  • Download URL: pprp-0.2.1-py2-none-any.whl
  • Upload date:
  • Size: 20.5 kB
  • Tags: Python 2
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for pprp-0.2.1-py2-none-any.whl
Algorithm Hash digest
SHA256 53fc0554e70b706258c9d4db48e7d484fc375d118386fc46d93afdb4b1e58f6e
MD5 6406738e896288d809f3f4f245035cdd
BLAKE2b-256 a9e1a5d237c2b412914a7302e5b079d871d32c230794d9d3ff5043281cc9043b

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