Here, we are encrypting the file arrow.png and saving the encrypted image in the file encrypted_arrow.png. Please note that we are here reading and writing binary data from or to a file. So, the mode in the open() function should be “rb” for reading binary data and “wb” for writing binary data.
We are firstly initializing the cipher using the key and the IV. We are using AES CBC mode here. Now, AES is a block cipher. It divides the read data into equal-sized blocks and the last block needs to be padded. We are using the pad() function to pad the read data and passing the padded data as a parameter in the cipher.encrypt() function.
The decryption function looks like the following:
def decrypt_image(filename, key, iv): BLOCKSIZE = 16 decrypted_filename = "decrypted_" + filename with open(filename, "rb") as file1: data = file1.read() cipher2 = AES.new(key, AES.MODE_CBC, iv) decrypted_data = unpad(cipher2.decrypt(data), BLOCKSIZE) with open(decrypted_filename, "wb") as file2: file2.write(decrypted_data) return decrypted_filename
Here, we are reading binary data from the encrypted file and then, we are decrypting the data using the same key and the IV that we used at the time of encryption. Please note that we would need to unpad the data after decryption and write the unpadded data in the image file.
The complete code of encryption and decryption of an image using Python is given below:
import os from Crypto.Util.Padding import pad, unpad from Crypto.Cipher import AES def getKey(keysize): key = os.urandom(keysize) return key def getIV(blocksize): iv = os.urandom(blocksize) return iv def encrypt_image(filename, key, iv): BLOCKSIZE = 16 encrypted_filename = "encrypted_" + filename with open(filename, "rb") as file1: data = file1.read() cipher = AES.new(key, AES.MODE_CBC, iv) ciphertext = cipher.encrypt(pad(data, BLOCKSIZE)) with open(encrypted_filename, "wb") as file2: file2.write(ciphertext) return encrypted_filename def decrypt_image(filename, key, iv): BLOCKSIZE = 16 decrypted_filename = "decrypted_" + filename with open(filename, "rb") as file1: data = file1.read() cipher2 = AES.new(key, AES.MODE_CBC, iv) decrypted_data = unpad(cipher2.decrypt(data), BLOCKSIZE) with open(decrypted_filename, "wb") as file2: file2.write(decrypted_data) return decrypted_filename KEYSIZE = 16 BLOCKSIZE = 16 filename = "arrow.png" key = getKey(KEYSIZE) iv = getIV(BLOCKSIZE) encrypted_filename = encrypt_image(filename, key, iv) decrypted_filename = decrypt_image(encrypted_filename, key, iv)
I hope this helps. However, readers who want to know more about how different cryptographic algorithms work and how they are used in various secure network protocols can refer to the book “Cryptography And Public Key Infrastructure.”
![Share on Facebook Facebook](https://www.thesecuritybuddy.com/wordpress/bdr/plugins/social-media-feather/synved-social/image/social/regular/64x64/facebook.png)
![Share on Twitter twitter](https://www.thesecuritybuddy.com/wordpress/bdr/plugins/social-media-feather/synved-social/image/social/regular/64x64/twitter.png)
![Share on Reddit reddit](https://www.thesecuritybuddy.com/wordpress/bdr/plugins/social-media-feather/synved-social/image/social/regular/64x64/reddit.png)
![Pin it with Pinterest pinterest](https://www.thesecuritybuddy.com/wordpress/bdr/plugins/social-media-feather/synved-social/image/social/regular/64x64/pinterest.png)
![Share on Linkedin linkedin](https://www.thesecuritybuddy.com/wordpress/bdr/plugins/social-media-feather/synved-social/image/social/regular/64x64/linkedin.png)
![Share by email mail](https://www.thesecuritybuddy.com/wordpress/bdr/plugins/social-media-feather/synved-social/image/social/regular/64x64/mail.png)
0 Comments