Package Crypto :: Package Cipher :: Module blockalgo :: Class BlockAlgo
[frames] | no frames]

Class BlockAlgo

Known Subclasses:

Class modelling an abstract block cipher.
Instance Methods
 
__init__(self, factory, key, *args, **kwargs)
 
encrypt(self, plaintext)
Encrypt data with the key and the parameters set at initialization.
 
decrypt(self, ciphertext)
Decrypt data with the key and the parameters set at initialization.
Method Details

encrypt(self, plaintext)

 

Encrypt data with the key and the parameters set at initialization.

The cipher object is stateful; encryption of a long block of data can be broken up in two or more calls to encrypt(). That is, the statement:

>>> c.encrypt(a) + c.encrypt(b)

is always equivalent to:

>>> c.encrypt(a+b)

That also means that you cannot reuse an object for encrypting or decrypting other data with the same key.

This function does not perform any padding.

  • For MODE_ECB, MODE_CBC, and MODE_OFB, plaintext length (in bytes) must be a multiple of block_size.
  • For MODE_CFB, plaintext length (in bytes) must be a multiple of segment_size/8.
  • For MODE_CTR, plaintext can be of any length.
  • For MODE_OPENPGP, plaintext must be a multiple of block_size, unless it is the last chunk of the message.
Parameters:
  • plaintext (byte string) - The piece of data to encrypt.
Returns:
the encrypted data, as a byte string. It is as long as plaintext with one exception: when encrypting the first message chunk with MODE_OPENPGP, the encypted IV is prepended to the returned ciphertext.

decrypt(self, ciphertext)

 

Decrypt data with the key and the parameters set at initialization.

The cipher object is stateful; decryption of a long block of data can be broken up in two or more calls to decrypt(). That is, the statement:

>>> c.decrypt(a) + c.decrypt(b)

is always equivalent to:

>>> c.decrypt(a+b)

That also means that you cannot reuse an object for encrypting or decrypting other data with the same key.

This function does not perform any padding.

  • For MODE_ECB, MODE_CBC, and MODE_OFB, ciphertext length (in bytes) must be a multiple of block_size.
  • For MODE_CFB, ciphertext length (in bytes) must be a multiple of segment_size/8.
  • For MODE_CTR, ciphertext can be of any length.
  • For MODE_OPENPGP, plaintext must be a multiple of block_size, unless it is the last chunk of the message.
Parameters:
  • ciphertext (byte string) - The piece of data to decrypt.
Returns:
the decrypted data (byte string, as long as ciphertext).