-
Notifications
You must be signed in to change notification settings - Fork 1
/
encryption.py
56 lines (43 loc) · 1.55 KB
/
encryption.py
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
from PIL import Image
def encrypt(path):
key = int(input('Enter Key for encryption of Image : '))
print('The path of file : ', path)
print('Key for encryption : ', key)
fin = open(path, 'rb')
image = fin.read()
fin.close()
image = bytearray(image)
for index, values in enumerate(image):
image[index] = values ^ key
fin = open(path, 'wb')
fin.write(image)
fin.close()
print('Encryption Completed Successfully')
def decrypt(path):
try:
key = int(input('Enter Key for dencryption of Image : '))
print('The path of file : ', path)
print('Key for Decryption : ', key)
fin = open(path, 'rb')
image = fin.read()
fin.close()
image = bytearray(image)
for index, values in enumerate(image):
image[index] = values ^ key
fin = open(path, 'wb')
fin.write(image)
fin.close()
display = Image.open(path)
display.show()
print('Decryption Done...')
except Exception:
print('Error caught : ', Exception.__name__)
#______Main_______
p=input("Enter the image location:")
n=int(input("Press 1 for encryption and 2 for decryption:"))
if(n==1):
encrypt(p)
elif(n==2):
decrypt(p)
else:
print("Invalid Choice")