Python cryptography fails with "Expected interface of CipherAlgorithm" -


i'm trying use cryptography python module (cryptography.io) cannot implement working example. from example in documentation.

this code:

from cryptography.hazmat.primitives.ciphers import cipher, algorithms, modes cryptography.hazmat.backends import openssl cryptography.hazmat.backends import default_backend  dbackend = default_backend iv = 'ababababcdcdcdcd1212121234343434'.encode('hex') cipher = cipher(modes.cbc(iv), algorithms.aes('aabbccddaabbccdd1122334411223344'.decode('hex')), backend=dbackend)  e = cipher.encryptor() ct = e.update("secret messagexx") + e.finalize()  d = cipher.decryptor()  clear = d.update(ct) + d.finalize() 

fails with:

cryptography.exceptions.unsupportedalgorithm: backend object not implement cipherbackend. 

i try openssl backend:

obackend = openssl.backend cipher = cipher(modes.cbc(iv), algorithms.aes('aabbccddaabbccdd1122334411223344'.decode('hex')), backend=obackend) 

and fails with:

typeerror: expected interface of cipheralgorithm. 

i've been trying read docs can't example code work. appreciated.

update - solved:

in case stumbles on add working example here (where use ecb mode wanted).

from cryptography.hazmat.primitives.ciphers import cipher, algorithms, modes cryptography.hazmat.backends import default_backend  backend = default_backend() cipher = cipher(algorithms.aes('aabbccddaabbccdd1122334411223344'.decode('hex')), modes.ecb(), backend=backend)  e = cipher.encryptor() ct = e.update("secret messagexx") + e.finalize()  d = cipher.decryptor()  clear = d.update(ct) + d.finalize()  print clear 

you passing default_backend backend argument, that's function. call default_backend() , return backend object can pass in.

the non-hazmat layer contain symmetric encryption recipe (known fernet), may want consider using if meets needs.


Comments