-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassignment3.py
More file actions
executable file
·32 lines (26 loc) · 1.05 KB
/
assignment3.py
File metadata and controls
executable file
·32 lines (26 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
from Crypto.Cipher import DES
from Crypto import Random
from PIL import Image
def get_iv():
return Random.new().read(DES.block_size)
def encrypt_image(path, path_out, cipher):
with open(path, "rb") as f:
image = f.read()
header, image = image[:64], image[64:]
image, ign = image[:(len(image) // 8) * 8], image[(len(image) // 8) * 8:]
encrypted_image = cipher.encrypt(image)
encrypted_image = encrypted_image + ign
encrypted_image = header + encrypted_image
with open(path_out, "wb") as f:
f.write(encrypted_image)
if __name__ == '__main__':
path = input()
if path.endswith('.png') or path.endswith('.jpg'):
image = Image.open(path)
imag = image.convert('L').point(lambda x: 255 if x > 128 else 0, mode='1')
imag.save(path[:-4] + '.bmp')
key = bytes(input()[:8], 'ascii')
cipher_ecb = DES.new(key, mode=DES.MODE_ECB)
cipher_cbc = DES.new(key, DES.MODE_CBC, get_iv())
encrypt_image(path[:-4] + '.bmp', path[:-4] + '_ecb' + '.bmp', cipher_ecb)
encrypt_image(path[:-4] + '.bmp', path[:-4] + '_cbc' + '.bmp', cipher_cbc)