Light Mode

Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit cbca90d

Browse files
committed
Added Substitution through lorenz equations
1 parent b5ba7e3 commit cbca90d

File tree

5 files changed

+125
-62
lines changed
  • __pycache__
    • lorenzSystem.cpython-38.pyc
  • glas-quadratisch-ga21492-taj-mahal-30x30cm-final-einzel.jpg
  • lorenzEncryption.py
  • lorenzSystem.py
  • sustitutionLorenz.py

5 files changed

+125
-62
lines changed

__pycache__/lorenzSystem.cpython-38.pyc

0 Bytes
Binary file not shown.

glas-quadratisch-ga21492-taj-mahal-30x30cm-final-einzel.jpg

82 KB
Loading

lorenzEncryption.py

Lines changed: 34 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,37 @@
1+
# Importing all the required libraries
12
import matplotlib.image as img
23
import matplotlib.pyplot as plt
34
import numpy as np
45
import lorenzSystem as key
56

7+
# Accepting Image using it's path
68
path = str(input('Enter path of the image\n'))
79
image = img.imread(path)
810

11+
# Displaying original image
912
plt.imshow(image)
1013
plt.show()
1114

15+
# Storing the size of image in variables
1216
height = image.shape[0]
1317
width = image.shape[1]
14-
print(width)
1518

19+
# Using lorenz_key function to generate 3 lists of keys
1620
xkey, ykey, zkey = key.lorenz_key(0.01, 0.02, 0.03, height*width)
17-
# print(xkey, ykey, zkey)
1821

1922
l = 0
2023
x = []
2124
y = []
2225
xindex = []
2326
yindex = []
27+
28+
# Initializing an empty image to store the encrypted image
2429
encryptedImage = np.zeros(shape=[height, width, 3], dtype=np.uint8)
2530
l = 0
2631

2732
for i in range(width):
2833
xindex.append(i)
2934

30-
# print(xindex)
31-
# print(xkey)
32-
3335
for i in range(height):
3436
yindex.append(i)
3537

@@ -39,98 +41,74 @@
3941
xkey[i], xkey[j] = xkey[j], xkey[i]
4042
xindex[i], xindex[j] = xindex[j], xindex[i]
4143

42-
# print(xkey)
43-
4444
for i in range(height):
4545
for j in range(height):
4646
if ykey[i] > ykey[j]:
4747
ykey[i], ykey[j] = ykey[j], ykey[i]
4848
yindex[i], yindex[j] = yindex[j], yindex[i]
4949

50-
# print(yindex)
51-
5250
for i in range(height):
5351
k = 0
5452
for j in range(width):
55-
encryptedImage[i][j] = image[yindex[k]][j]
53+
encryptedImage[i][j] = image[yindex[k]][j]
5654
k += 1
5755

58-
print(image[70][0])
59-
print(encryptedImage[0][0])
60-
6156
for i in range(height):
6257
k = 0
6358
for j in range(width):
6459
encryptedImage[i][j] = encryptedImage[i][xindex[k]]
6560
k += 1
6661

67-
print(image[70][0])
68-
print(encryptedImage[0][0])
69-
7062
plt.imshow(encryptedImage)
7163
plt.show()
7264

7365
l = 0
74-
# print(zkey)
7566
for i in range(height):
7667
for j in range(width):
77-
# print(zkey[l])
7868
zk = (int((zkey[l]*pow(10, 5))%256))
79-
# print(zk)
80-
# print(encryptedImage[i, j])
8169
encryptedImage[i, j] = encryptedImage[i, j]^zk
82-
# print(encryptedImage[i, j])
8370
l += 1
84-
# plt.imshow(encryptedImage)
8571

8672
plt.imshow(encryptedImage)
8773
plt.show()
8874

8975

9076

9177

92-
decryptedImage = np.zeros(shape=[height, width, 3], dtype=np.uint8)
78+
# decryptedImage = np.zeros(shape=[height, width, 3], dtype=np.uint8)
9379

94-
l = 0
95-
# print(zkey)
96-
for i in range(height):
97-
for j in range(width):
98-
# print(zkey[l])
99-
zk = (int((zkey[l]*pow(10, 5))%256))
100-
# print(zk)
101-
# print(encryptedImage[i, j])
102-
decryptedImage[i, j] = encryptedImage[i, j]^zk
103-
# print(encryptedImage[i, j])
104-
l += 1
105-
# plt.imshow(encryptedImage)
80+
# l = 0
81+
# for i in range(height):
82+
# for j in range(width):
83+
# zk = (int((zkey[l]*pow(10, 5))%256))
84+
# decryptedImage[i, j] = encryptedImage[i, j]^zk
85+
# l += 1
10686

107-
plt.imshow(decryptedImage)
108-
plt.show()
87+
# print(decryptedImage[0][0])
10988

110-
# print(yindex)
89+
# plt.imshow(decryptedImage)
90+
# plt.show()
11191

112-
for i in range(height):
113-
k = 0
114-
for j in range(width):
115-
encryptedImage[i][xindex[k]] = encryptedImage[i][j]
116-
k += 1
11792

118-
print(image[70][0])
119-
print(encryptedImage[0][0])
120-
# print(decryptedImage[0][0])
93+
# for i in range(height):
94+
# k = 0
95+
# for j in range(width):
96+
# encryptedImage[i][xindex[k]] = encryptedImage[i][j]
97+
# k += 1
12198

99+
# print(image[70][0])
100+
# print(encryptedImage[0][0])
122101

123-
for i in range(height):
124-
k = 0
125-
for j in range(width):
126-
encryptedImage[yindex[k]][j] = encryptedImage[i][j]
127-
k += 1
128102

129-
print(image[70][0])
130-
print(encryptedImage[0][0])
131-
# print(decryptedImage[0][0])
103+
# for i in range(height):
104+
# k = 0
105+
# for j in range(width):
106+
# decryptedImage[yindex[k]][j] = encryptedImage[i][j]
107+
# k += 1
132108

109+
# print(image[70][0])
110+
# print(encryptedImage[0][0])
133111

134112

135-
plt.imshow(encryptedImage)
136-
plt.show()
113+
# plt.imshow(decryptedImage)
114+
# plt.show()

lorenzSystem.py

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,41 @@
1+
# Importing the required libraries
12
import numpy as np
23
import matplotlib.pyplot as plt
34

45
def lorenz_key(xinit, yinit, zinit, num_steps):
6+
"""
7+
This function returns 3 lists of pseudo-random
8+
numbers generated using Lorenz system of differential
9+
equations.
10+
11+
Parameters:
12+
xinit: float
13+
initial value of x
14+
yinit: float
15+
initial value of y
16+
zinit: float
17+
initial value of z
18+
num_steps: int
19+
number of keys required
20+
in a single list
21+
22+
Returns:
23+
3 lists with pseudo-random numbers
24+
as their elements
25+
"""
26+
27+
# Initializing dt to a small value
528
dt = 0.01
629

30+
# Initializing 3 empty lists
731
xs = np.empty(num_steps + 1)
832
ys = np.empty(num_steps + 1)
933
zs = np.empty(num_steps + 1)
1034

11-
# Initial values
35+
# Initializing initial values
1236
xs[0], ys[0], zs[0] = (xinit, yinit, zinit)
1337

38+
# Initializing constants
1439
s = 10
1540
r = 28
1641
b = 2.667
@@ -21,18 +46,17 @@ def lorenz_key(xinit, yinit, zinit, num_steps):
2146
ys[i + 1] = ys[i] + ((xs[i] * (r - zs[i]) - ys[i]) * dt)
2247
zs[i + 1] = zs[i] + ((xs[i] * ys[i] - b * zs[i]) * dt)
2348

49+
# Uncomment to plot Lorenz system
50+
2451
# fig = plt.figure()
2552
# ax = fig.gca(projection='3d')
2653

2754
# ax.plot(xs, ys, zs)
2855
# plt.show()
2956

3057
return xs, ys, zs
31-
# Plotting
32-
# fig = plt.figure()
33-
# ax = fig.gca(projection='3d')
3458

35-
# ax.plot(xs, ys, zs)
36-
# plt.show()
59+
# Uncomment to plot Lorenz system
3760

3861
# lorenz_key(0.01, 0.02, 0.03, 1000)
62+

sustitutionLorenz.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
"""
2+
Encrypting an image through substitution alogorithm
3+
using pseudo-random numbers generated from
4+
Lorenz system of differential equations
5+
"""
6+
7+
# Importing all the necessary libraries
8+
import matplotlib.image as img
9+
import matplotlib.pyplot as plt
10+
import numpy as np
11+
import lorenzSystem as key
12+
13+
# Accepting Image using it's path
14+
path = str(input('Enter path of the image\n'))
15+
image = img.imread(path)
16+
17+
# Displaying original image
18+
plt.imshow(image)
19+
plt.show()
20+
21+
# Storing the size of image in variables
22+
height = image.shape[0]
23+
width = image.shape[1]
24+
25+
# Using lorenz_key function to generate a key for every pixel
26+
x, y, keys = key.lorenz_key(0.01, 0.02, 0.03, height*width)
27+
28+
l = 0
29+
30+
# Initializing an empty image to store the encrypted image
31+
encryptedImage = np.zeros(shape=[height, width, 3], dtype=np.uint8)
32+
33+
# XORing each pixel with a pseudo-random number generated above/ Performing the
34+
# substitution algorithm
35+
for i in range(height):
36+
for j in range(width):
37+
# Converting the pseudo-random nuber generated into a number between 0 and 255
38+
zk = (int((keys[l]*pow(10, 5))%256))
39+
# Performing the XOR operation
40+
encryptedImage[i, j] = image[i, j]^zk
41+
l += 1
42+
43+
# Displaying the encrypted image
44+
plt.imshow(encryptedImage)
45+
plt.show()
46+
47+
# Initializing an empty image to store the encrypted image
48+
decryptedImage = np.zeros(shape=[height, width, 3], dtype=np.uint8)
49+
50+
# XORing each pixel with the same number it was XORed above above/
51+
# Performing the reverse substitution algorithm
52+
l = 0
53+
for i in range(height):
54+
for j in range(width):
55+
zk = (int((keys[l]*pow(10, 5))%256))
56+
decryptedImage[i, j] = encryptedImage[i, j]^zk
57+
l += 1
58+
59+
# Displaying the decrypted image
60+
plt.imshow(decryptedImage)
61+
plt.show()

0 commit comments

Comments
(0)