Welcome to jaraco.crypto documentation!

Blowfish Encryption

This module is a pure python implementation of Bruce Schneier’s encryption scheme ‘Blowfish’. Blowish is a 16-round Feistel Network cipher and offers substantial speed gains over DES.

The key is a string of length anywhere between 64 and 448 bits, or equivalently 8 and 56 bytes. The encryption and decryption functions operate on 64-bit blocks, or 8 byte strings.

Send questions, comments, bugs my way:

Michael Gilfix <mgilfix@eecs.tufts.edu>

The module has been expanded to include CTR stream encryption/decryption mode, built from the primitives from the orignal module. This change did not alter any of the base Blowfish code from the original author.

The author of CTR changes is:

Ivan Voras <ivoras@gmail.com>

Test usage:

>>> key = 'This is a test key'
>>> cipher = Blowfish (key)

test encryption

>>> xl = 123456
>>> xr = 654321

Plain text is (xl, xr)

>>> cl, cr = cipher.cipher(xl, xr, cipher.ENCRYPT)

Cipher text is (cl, cr)

>>> dl, dr = cipher.cipher(cl, cr, cipher.DECRYPT)
>>> (dl, dr) == (xl, xr)
True

Test block encrypt

>>> text = "testtext"
>>> crypted = cipher.encrypt(text)
>>> crypted == text
False
>>> decrypted = cipher.decrypt(crypted)
>>> decrypted == text
True

Test CTR encrypt

>>> text = "If the offer's shunned, you might as well be walking on the sun"
>>> cipher.initCTR()
>>> crypted = cipher.encryptCTR(text)
>>> crypted == text
False
>>> cipher.initCTR()
>>> decrypted = cipher.decryptCTR(crypted)
>>> decrypted == text
True
class jaraco.crypto.blowfish.Blowfish(key)

Bases: object

Blowfish encryption Scheme

This class implements the encryption and decryption functionality of the Blowfish cipher.

Public functions:

def __init__ (self, key)

Creates an instance of blowfish using ‘key’ as the encryption key. Key is a string of length ranging from 8 to 56 bytes (64 to 448 bits). Once the instance of the object is created, the key is no longer necessary.

def encrypt (self, data):

Encrypt an 8 byte (64-bit) block of text where ‘data’ is an 8 byte string. Returns an 8-byte encrypted string.

def decrypt (self, data):

Decrypt an 8 byte (64-bit) encrypted block of text, where ‘data’ is the 8 byte encrypted string. Returns an 8-byte string of plaintext.

def cipher (self, xl, xr, direction):

Encrypts a 64-bit block of data where xl is the upper 32-bits and xr is the lower 32-bits. ‘direction’ is the direction to apply the cipher, either ENCRYPT or DECRYPT constants. returns a tuple of either encrypted or decrypted data of the left half and right half of the 64-bit block.

def initCTR(self):

Initializes CTR engine for encryption or decryption.

def encryptCTR(self, data):

Encrypts an arbitrary string and returns the encrypted string. The method can be called successively for multiple string blocks.

def decryptCTR(self, data):

Decrypts a string encrypted with encryptCTR() and returns the decrypted string.

Private members:

def __round_func(self, xl)

Performs an obscuring function on the 32-bit block of data ‘xl’, which is the left half of the 64-bit block of data. Returns the 32-bit result as a long integer.

DECRYPT = 1
ENCRYPT = 0
blocksize()
cipher(xl, xr, direction)

Encryption primitive

decrypt(data)
decryptCTR(data)
encrypt(data)
encryptCTR(data)

Encrypts a buffer of data with CTR mode. Multiple successive buffers (belonging to the same logical stream of buffers) can be encrypted with this method one after the other without any intermediate work.

initCTR(iv=0)

Initializes CTR mode of the cypher

key_bits()
key_length()
modulus = 4294967296
class jaraco.crypto.cipher.Cipher(type, key, iv, encrypt=True)

Bases: _ctypes.Structure

app_data

Structure/Union member

block_mask

Structure/Union member

buf

Structure/Union member

buf_len

Structure/Union member

cipher

Structure/Union member

cipher_data

Structure/Union member

encrypt

Structure/Union member

engine

Structure/Union member

final

Structure/Union member

final_used

Structure/Union member

finalize(data=None)
finalized = False
flags

Structure/Union member

static interpret_type(type)
iv

Structure/Union member

key_len

Structure/Union member

num

Structure/Union member

oiv

Structure/Union member

set_padding(padding=True)
update(data)

From docs: EVP_EncryptUpdate() encrypts inl bytes from the buffer in and writes the encrypted version to out. This function can be called multiple times to encrypt successive blocks of data. The amount of data written depends on the block alignment of the encrypted data: as a result the amount of data written may be anything from zero bytes to (inl + cipher_block_size - 1) so outl should contain sufficient room. The actual number of bytes written is placed in outl.

exception jaraco.crypto.cipher.CipherError

Bases: Exception

class jaraco.crypto.cipher.CipherType

Bases: _ctypes.Structure

app_data

Structure/Union member

block_size

Structure/Union member

cleanup

Structure/Union member

ctrl

Structure/Union member

ctx_size

Structure/Union member

do_cipher

Structure/Union member

flags

Structure/Union member

classmethod from_name(*cipher_name)

Create a CipherType from a cipher name.

Takes one or two parameters. If one is supplied, it should be a dash-separated string of algorithm-mode. If two are supplied, they should be the algorithm and mode.

get_asn1_parameters

Structure/Union member

init

Structure/Union member

iv_len

Structure/Union member

key_len

Structure/Union member

nid

Structure/Union member

set_asn1_parameters

Structure/Union member

class jaraco.crypto.digest.Digest(digest_type)

Bases: _ctypes.Structure

digest(data=None)
engine

Structure/Union member

finalized = False
flags

Structure/Union member

md_data

Structure/Union member

p_type

Structure/Union member

pctx

Structure/Union member

update(data)
update_func

Structure/Union member

exception jaraco.crypto.digest.DigestError

Bases: Exception

class jaraco.crypto.digest.DigestType

Bases: _ctypes.Structure

cleanup

Structure/Union member

copy

Structure/Union member

ctx_size

Structure/Union member

final

Structure/Union member

flags

Structure/Union member

classmethod from_name(digest_name)
init

Structure/Union member

md_ctrl

Structure/Union member

md_size

Structure/Union member

pkey_type

Structure/Union member

required_pkey_type

Structure/Union member

sign

Structure/Union member

type

Structure/Union member

update

Structure/Union member

verify

Structure/Union member

exception jaraco.crypto.rand.RandError

Bases: Exception

jaraco.crypto.rand.bytes(num, check_result=False)
jaraco.crypto.rand.pseudo_bytes(num)
jaraco.crypto.rand.seed(data, entropy=None)
jaraco.crypto.support.find_lib_Linux(lib_name)
jaraco.crypto.support.find_lib_default(lib_name)

Given a name like libeay32, find the best match.

jaraco.crypto.support.find_library(lib_name)

Indices and tables