Skip to content

Instantly share code, notes, and snippets.

@CameronLonsdale
Created March 9, 2018 04:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CameronLonsdale/a2daa927f2556d14b16745a68f6ed430 to your computer and use it in GitHub Desktop.
Save CameronLonsdale/a2daa927f2556d14b16745a68f6ed430 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
from PIL import Image
from Crypto.Cipher import AES
KEY = "YELLOW SUBMARINE"
INPUT = "logo.png"
OUTPUT = "encrypted.png"
def pad(plaintext: bytearray, block_size=AES.block_size):
"""PKCS#7 Padding"""
num_pad = block_size - (len(plaintext) % block_size)
return plaintext + bytearray([num_pad for x in range(num_pad)])
plaintext_img = Image.open(INPUT)
cipher = AES.new(KEY, AES.MODE_ECB)
encrypted_image = cipher.encrypt(bytes(pad(plaintext_img.tobytes())))
Image.frombytes(plaintext_img.mode, plaintext_img.size, encrypted_image).save(OUTPUT)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment