-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsubstitutionEncryption.py
57 lines (45 loc) · 1.57 KB
/
substitutionEncryption.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
57
import logisticKey as key # Importing the key generating function
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as img
# Accepting an image
path = str(input('Enter the path of image\n'))
image = img.imread(path)
# Displaying the image
plt.imshow(image)
plt.show()
# Generating dimensions of the image
height = image.shape[0]
width = image.shape[1]
print(height, width)
# Generating keys
# Calling logistic_key and providing r value such that the keys are pseudo-random
# and generating a key for every pixel of the image
generatedKey = key.logistic_key(0.01, 3.95, height*width)
print(generatedKey)
# Encryption using XOR
z = 0
# Initializing the encrypted image
encryptedImage = np.zeros(shape=[height, width, 3], dtype=np.uint8)
# Substituting all the pixels in original image with nested for
for i in range(height):
for j in range(width):
# USing the XOR operation between image pixels and keys
encryptedImage[i, j] = image[i, j].astype(int) ^ generatedKey[z]
z += 1
# Displaying the encrypted image
plt.imshow(encryptedImage)
plt.show()
# Decryption using XOR
z = 0
# Initializing the decrypted image
decryptedImage = np.zeros(shape=[height, width, 3], dtype=np.uint8)
# Substituting all the pixels in encrypted image with nested for
for i in range(height):
for j in range(width):
# USing the XOR operation between encrypted image pixels and keys
decryptedImage[i, j] = encryptedImage[i, j].astype(int) ^ generatedKey[z]
z += 1
# Displaying the decrypted image
plt.imshow(decryptedImage)
plt.show()