fork download
  1. from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
  2. from cryptography.hazmat.backends import default_backend
  3. from binascii import hexlify as hexa
  4. from os import urandom
  5.  
  6. k=urandom(16)
  7. iv=urandom(16)
  8. print("k=",hexa(k))
  9. cipher = Cipher(algorithms.AES(k), modes.CBC(iv), backend = default_backend)
  10. aes_encrypt = cipher.encryptor()
  11. p1=urandom(16)
  12. p2=urandom(16)
  13. p=p1+p2+p1
  14. c=aes_encrypt.update(p) +aes_encrypt.finalize()
  15. print("p= ",hexa(p))
  16. print("c=",hexa(c))
  17. aes_decrypt = cipher.decryptor()
  18. p3=aes_decrypt.update(c) +aes_decrypt.finalize()
  19. print("p3=",hexa(p3))
Runtime error #stdin #stdout #stderr 0.14s 43144KB
stdin
Standard input is empty
stdout
('k=', 'cf5dfd8bce624f25cb0bddff450c5584')
stderr
Traceback (most recent call last):
  File "prog.py", line 9, in <module>
  File "/usr/lib/python2.7/dist-packages/cryptography/hazmat/primitives/ciphers/base.py", line 84, in __init__
    _Reasons.BACKEND_MISSING_INTERFACE
cryptography.exceptions.UnsupportedAlgorithm: Backend object does not implement CipherBackend.