- Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchaoticKeyGen.py
45 lines (40 loc) · 2.05 KB
/
chaoticKeyGen.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
importitertools
classKeyGen:
''' chaos is based on the ability of some dynamic \n
systems to produce of numbers that are random in nature and use as a keys for \n
encrypt and decrypt every pixel of an image '''
deflogisticMapKeyGen(self, initCondition, controlParameter, img) ->list:
# Get the image dimensions
height, width, ch=img.shape
fory, xinitertools.product(range(height), range(width)):
ifnotisinstance(initCondition, (int, float)):
raiseValueError("Invalid initial condition")
ifnotisinstance(controlParameter, (int, float)):
raiseValueError("Invalid control parameter")
# Ensure initCondition is within [0, 1]
initCondition=max(0, min(1, initCondition))
# Ensure controlParameter is within a reasonable range
controlParameter=max(0, min(4, controlParameter))
# Logistic map for key generation
try:
initCondition=controlParameter*initCondition* (1-initCondition)
exceptZeroDivisionError:
# Handle potential division by zero
initCondition=0.5
# .`. x = initialCondition , r = controlParameter
# x = r * x * (1-x) logistic map
# logistic map for key geniration
initCondition=controlParameter* \
initCondition*(1-initCondition)
# Check if the image is grayscale
iflen(img.shape) ==2:
# Grayscale image
#Updating Image pixels with generated keys
img[y, x] =img[y, x] ^int((initCondition*pow(10, 16)) %256)
else:
forchnlinrange(ch):
# this is a key for for encryption as well decryption
# taking mod 256 because it is for 8bit Color image
#Updating Image pixels with generated keys
img[y, x, chnl] =img[y, x, chnl] ^int((initCondition*pow(10, 16)) %256)
returnimg