2

I want to encrypt and decrypt a file (any type of file) using aes 128 in cbc mode in python.

I am quite new to cryptography and i have tried some tutorials but all work only on texts, and i need it for files.

Could anyone suggest me a solution?

idk
  • 71
  • 1
  • 1
  • 7
  • possible duplicate of [How to AES encrypt/decrypt files using Python/PyCrypto in an OpenSSL-compatible way?](http://stackoverflow.com/questions/16761458/how-to-aes-encrypt-decrypt-files-using-python-pycrypto-in-an-openssl-compatible) – Ken Y-N May 31 '15 at 23:36

1 Answers1

7

A quick Google search guided me to the Crypto package. It comes with the iPython that I am using, but the installation should be trivial anyway.

I just repost the example here for your information.

>>> from Crypto.Cipher import AES
>>> obj = AES.new('This is a key123', AES.MODE_CBC, 'This is an IV456')
>>> message = "The answer is no"
>>> ciphertext = obj.encrypt(message)
>>> ciphertext
'\xd6\x83\x8dd!VT\x92\xaa`A\x05\xe0\x9b\x8b\xf1'
>>> obj2 = AES.new('This is a key123', AES.MODE_CBC, 'This is an IV456')
>>> obj2.decrypt(ciphertext)
'The answer is no'

Here is the documentation of AES.

If you try to encrypt file, you can either use the openSSL or a Python solution using Crypto contributed by Thijs. Click here for more information.

Community
  • 1
  • 1
B.Mr.W.
  • 18,910
  • 35
  • 114
  • 178