Symmetric Key Algorithms

Symmetric key algorithms are encryption algorithms that use the same cryptographic key for both encryption and decryption of data. This operation is also known as Symmetric Key Encryption.

Symmetric Key Encryption Classes

Interface

All Symmetric Key Ciphers available in this module implements the following interface:

class wolfcrypt.ciphers._Cipher(key, mode, IV=None)[source]

A PEP 272: Block Encryption Algorithms compliant Symmetric Key Cipher.

classmethod new(key, mode, IV=None, **kwargs)[source]

Returns a ciphering object, using the secret key contained in the string key, and using the feedback mode mode, which must be one of MODE_* defined in this module.

If mode is MODE_CBC or MODE_CFB, IV must be provided and must be a string of the same length as the block size. Not providing a value of IV will result in a ValueError exception being raised.

encrypt(string)[source]

Encrypts a non-empty string, using the key-dependent data in the object, and with the appropriate feedback mode.

The string’s length must be an exact multiple of the algorithm’s block size or, in CFB mode, of the segment size.

Returns a string containing the ciphertext.

decrypt(string)[source]

Decrypts string, using the key-dependent data in the object and with the appropriate feedback mode.

The string’s length must be an exact multiple of the algorithm’s block size or, in CFB mode, of the segment size.

Returns a string containing the plaintext.

AES

class wolfcrypt.ciphers.Aes(key, mode, IV=None)[source]

The Advanced Encryption Standard (AES), a.k.a. Rijndael, is a symmetric-key cipher standardized by NIST.

Example:

>>> from wolfcrypt.ciphers import Aes, MODE_CBC
>>>
>>> cipher = Aes(b'0123456789abcdef', MODE_CBC, b'1234567890abcdef')
>>> ciphertext = cipher.encrypt('now is the time ')
>>> ciphertext
b'\x95\x94\x92W_B\x81S,\xcc\x9dFw\xa23\xcb'
>>> cipher.decrypt(ciphertext)
b'now is the time '

Triple DES

class wolfcrypt.ciphers.Des3(key, mode, IV=None)[source]

Triple DES (3DES) is the common name for the Triple Data Encryption Algorithm (TDEA or Triple DEA) symmetric-key block cipher, which applies the Data Encryption Standard (DES) cipher algorithm three times to each data block.

Example:

>>> from wolfcrypt.ciphers import Des3, MODE_CBC
>>>
>>> cipher = Des3(b'0123456789abcdeffedeba98', MODE_CBC, b'12345678')
>>> ciphertext = cipher.encrypt('now is the time ')
>>> ciphertext
b'l\x04\xd0$\xe8\x0c1\xd6\x1b\x07}V\xa6ty\xe8'
>>> cipher.decrypt(ciphertext)
b'now is the time '