/examples/ciphers.py

https://github.com/PyCQA/bandit · Python · 76 lines · 68 code · 8 blank · 0 comment · 0 complexity · 2d550d95d6e4a66ee022eb8efa4d2849 MD5 · raw file

  1. from Crypto.Cipher import ARC2 as pycrypto_arc2
  2. from Crypto.Cipher import ARC4 as pycrypto_arc4
  3. from Crypto.Cipher import Blowfish as pycrypto_blowfish
  4. from Crypto.Cipher import DES as pycrypto_des
  5. from Crypto.Cipher import XOR as pycrypto_xor
  6. from Cryptodome.Cipher import ARC2 as pycryptodomex_arc2
  7. from Cryptodome.Cipher import ARC4 as pycryptodomex_arc4
  8. from Cryptodome.Cipher import Blowfish as pycryptodomex_blowfish
  9. from Cryptodome.Cipher import DES as pycryptodomex_des
  10. from Cryptodome.Cipher import XOR as pycryptodomex_xor
  11. from Crypto.Hash import SHA
  12. from Crypto import Random
  13. from Crypto.Util import Counter
  14. from cryptography.hazmat.primitives.ciphers import Cipher
  15. from cryptography.hazmat.primitives.ciphers import algorithms
  16. from cryptography.hazmat.primitives.ciphers import modes
  17. from cryptography.hazmat.backends import default_backend
  18. from struct import pack
  19. key = b'Sixteen byte key'
  20. iv = Random.new().read(pycrypto_arc2.block_size)
  21. cipher = pycrypto_arc2.new(key, pycrypto_arc2.MODE_CFB, iv)
  22. msg = iv + cipher.encrypt(b'Attack at dawn')
  23. cipher = pycryptodomex_arc2.new(key, pycryptodomex_arc2.MODE_CFB, iv)
  24. msg = iv + cipher.encrypt(b'Attack at dawn')
  25. key = b'Very long and confidential key'
  26. nonce = Random.new().read(16)
  27. tempkey = SHA.new(key+nonce).digest()
  28. cipher = pycrypto_arc4.new(tempkey)
  29. msg = nonce + cipher.encrypt(b'Open the pod bay doors, HAL')
  30. cipher = pycryptodomex_arc4.new(tempkey)
  31. msg = nonce + cipher.encrypt(b'Open the pod bay doors, HAL')
  32. iv = Random.new().read(bs)
  33. key = b'An arbitrarily long key'
  34. plaintext = b'docendo discimus '
  35. plen = bs - divmod(len(plaintext),bs)[1]
  36. padding = [plen]*plen
  37. padding = pack('b'*plen, *padding)
  38. bs = pycrypto_blowfish.block_size
  39. cipher = pycrypto_blowfish.new(key, pycrypto_blowfish.MODE_CBC, iv)
  40. msg = iv + cipher.encrypt(plaintext + padding)
  41. bs = pycryptodomex_blowfish.block_size
  42. cipher = pycryptodomex_blowfish.new(key, pycryptodomex_blowfish.MODE_CBC, iv)
  43. msg = iv + cipher.encrypt(plaintext + padding)
  44. key = b'-8B key-'
  45. plaintext = b'We are no longer the knights who say ni!'
  46. nonce = Random.new().read(pycrypto_des.block_size/2)
  47. ctr = Counter.new(pycrypto_des.block_size*8/2, prefix=nonce)
  48. cipher = pycrypto_des.new(key, pycrypto_des.MODE_CTR, counter=ctr)
  49. msg = nonce + cipher.encrypt(plaintext)
  50. nonce = Random.new().read(pycryptodomex_des.block_size/2)
  51. ctr = Counter.new(pycryptodomex_des.block_size*8/2, prefix=nonce)
  52. cipher = pycryptodomex_des.new(key, pycryptodomex_des.MODE_CTR, counter=ctr)
  53. msg = nonce + cipher.encrypt(plaintext)
  54. key = b'Super secret key'
  55. plaintext = b'Encrypt me'
  56. cipher = pycrypto_xor.new(key)
  57. msg = cipher.encrypt(plaintext)
  58. cipher = pycryptodomex_xor.new(key)
  59. msg = cipher.encrypt(plaintext)
  60. cipher = Cipher(algorithms.ARC4(key), mode=None, backend=default_backend())
  61. encryptor = cipher.encryptor()
  62. ct = encryptor.update(b"a secret message")
  63. cipher = Cipher(algorithms.Blowfish(key), mode=None, backend=default_backend())
  64. encryptor = cipher.encryptor()
  65. ct = encryptor.update(b"a secret message")
  66. cipher = Cipher(algorithms.IDEA(key), mode=None, backend=default_backend())
  67. encryptor = cipher.encryptor()
  68. ct = encryptor.update(b"a secret message")