03 Chaotic Encryption
03 Chaotic Encryption
1
Randomness
Randomness is the lake of pattern or predictability in events
A random sequence of events has no order and does not follow an intelligible pattern
Individual random events are by definition unpredictable. But in many cases the
frequency of different outcomes over a large number of events is predictable. For
example when throwing two dice, the outcome of any particular roll is unpredictable,
but a sum of 7 will occur twice as often as 4
2
The Theory of Chaos
Chaotic is an adjective that comes from the noun "chaos," meaning complete and total
confusion or lack of order.
Chaos theory concerns deterministic systems whose behavior can, in principle, be
predicted. Chaotic systems are predictable for a while and then 'appear' to become
random.
Chaos are highly sensitive to the initial conditions (such as those due to errors in
measurements or due to rounding errors)
3
Natural chaos
4
The map or the graph obtained by plotting the values which is
again found by infinite iteration of that function is called
Chaotic Map for that function.
5
Wiki: An animation of a double-rod pendulum at an
intermediate energy showing chaotic behavior.
Starting the pendulum from a slightly
different initial condition would result in a vastly
different trajectory. The double-rod pendulum is
one of the simplest dynamical systems with chaotic
solutions.
6
• periodic orbits : a special type of solution for a
dynamical system, namely one which repeats itself
in time.
• For a chaotic system to have dense periodic
orbits means that every point in the space is
approached arbitrarily closely by periodic orbits
7
Logistic Mapping as an Example
The formulation for its state evolution is: x(n +1) = x(n)(1- x(n)), where x(n) (0,1) is
the state of system in time n and is control parameter (growth rate) for updating
which takes the value ranged within [3.7,4).
In other words, the state level at any given time is a function of the control parameter
and the previous time step’s state level.
Next table shows the result of running the logistic map for 20 time steps for control
parameters of 0.5, 1.0, 1.5, 2.0, 2.5, 3.0, and 3.5.
https://geoffboeing.com/2015/03/chaos-theory-logistic-map/ 8
This table shows the result of running the logistic map for 20
time steps for control parameters of 0.5, 1.0, 1.5, 2.0, 2.5, 3.0,
and 3.5, assuming that x0 = 0.5
x(n +1) = x(n)(1- x(n))
9
…
= 0.5, it quickly drops to zero. The population dies out. It has a fixed-point attractor = 0
= 2.0, it stays steady at a population level of 0.5.
= 2.5, slowly converging toward a stable value
= 3.5, seems to oscillates between four values
> 3.5, the system oscillates forever, never repeating itself or settling into a steady state of
behavior. It never hits the same point twice.
10
…
Think of this bifurcation diagram as 1,000
discrete vertical slices, each one corresponding to
one of the 1,000 control parameters (the growth
rate) between 0 and 4.
For each of these slices, I ran the model 200 times
then threw away the first 100 values, so we’re left
with the final 100 generations for each growth
rate.
Thus, each vertical slice depicts the population
values that the logistic map settles toward for that
parameter value.
In other words, the vertical slice above each
Bifurcations Diagram
growth rate is that growth rate’s attractor.
11
…
import numpy as np for i in range(100):
drawLogisticmap.py
import matplotlib.pyplot as plt x = r*x*(1-x)
R = np.linspace(0, 4, 1000) for i in range(rep):
X = [] x = r*x*(1-x)
rep = 100 Y.append(x)
plt.figure(figsize=(7, 5)) plt.plot(X, Y, marker=",", linestyle="", color="b")
for r in R: plt.xlabel("Growth rate")
x = 0.5 plt.ylabel("Population")
X = np.ones(rep)*r plt.title("Bifurcations Diagram")
Y = [] plt.show()
12
…
For growth rates less than 1.0, the system always collapses to zero.
For growth rates between 1.0 and 3.0, the system always settles into an exact, stable level.
At the vertical slice above growth rate 3.0, the possible values fork into two discrete paths.
For growth rate 3.5, the diagram bifurcates again into four paths.
After growth rate 3.5, it bifurcates again into eight paths
At ≈ 3.56995is the start of chaos
◦ for some growth rates, such as 3.9, the diagram shows 100 different values – in other words, a different
value for each of its 100 generations.
◦ It never settles into a fixed point or a limit cycle.
◦ The system now jumps, seemingly randomly, between all population values
13
…
Let’s zoom in again, to the narrow slice of
growth rates between 3.7 and 3.9:
Between the growth rate parameters of 3.82
and 3.84:
◦ the system moves from chaos back into order,
oscillating between just three population values
(approximately 0.15, 0.55, and 0.95).
◦ But then it bifurcates again and returns to chaos
at growth rates beyond 3.86.
14
…
Sensitive dependence on initial conditions.
Let’s run the logistic model with
two very similar initial population values.
◦ The blue line represents a x0 = 0.5.
◦ The red line represents a x0 = 0.50001.
15
…
If our knowledge of these two systems started at generation 50, we would have no way of
guessing that they were almost identical in the beginning.
In the line chart above, a tiny fluctuation of 0.00001 makes an enormous difference in the
behavior and state of the system 50 generations later.
This is famously known as the butterfly effect.
16
DrawPhaseDiagramLogisticMap2D.py
…
DrawPhaseDiagramLogisticMap3D.py
Chaos vs Randomness
chaotic system (in red)
random data (in blue)
17
Chaos-based Image Crypto System
The chaos-based image cryptosystem architecture generally comprises two phases:
◦ Confusion phase: is also known as the pixel permutation(scrambling), in which the pixel
positions are rearranged over the whole image while the pixel values remain unchanged,
converting the image into an unidentifiable form
◦ Diffusion phase: the pixel values are changed by using some inverse able function.
The diffusion phase is executed with the help of a chaotic map, the values of the pixels of the
whole image are changed consecutively by the sequence generated from the chaotic systems.
Multiple iterations of the confusion–diffusion process are performed until a satisfactory level
of security is attained
The block diagram of the architecture is shown next Figure
18
Though there are several Chaotic
Map based Image Encryption
Techniques are available, but the
whole process or techniques can be
generalized into three different
phases.
1. Selection of chaotic maps
2. Confusion
3. Diffusion
The diffusion phase is applied, as the confusion phase is not secure enough and can be easily hacked by an attacker.
19
…
The chaos-based cryptographic methods provide several advantages such as
◦ High security,
◦ Less computational overheads,
◦ Less computing power
◦ Ease of implementation.
◦ Increased flexibility:
◦ a variety of chaotic maps are available that can be incorporated in chaos-based
cryptosystems, which in turn increase the options to choose from.
These characteristics make them a promising alternative to conventional cryptographic
algorithms for encrypting and decrypting a wide range of digital data.
20
Chaotic Maps Examples
One dim maps
◦ Logistic map
◦ Baker Map
◦ Tent map
◦ Arnold cat Map
Multi dim maps
◦ Henon map
◦ 2D logistic Map
21
Performance Measurements
1. Key Sensitivity Analysis
2. Histogram Analysis
3. UACI and NPCR Tests
4. MSE and PSNR
5. (Not Explained) Lyapunov Exponent: one way to measure chaos
Have been discussed before:
6. Adjacent Pixel Auto-Correlation Test
7. Information Entropy Tests
22
1- Key Sensitivity Analysis
A secure cipher should be sensitive to the encryption key. Such sensitivity is
commonly addressed with respect to two aspects:
◦ Encryption: how different are two cipher-image C1 and C2 with respect to the same plain-
image using two encryption key K1 and K2, which are different only in one bit.
◦ Decryption: how different are two decrypted image D1 and D2 with respect to the same
cipher-image using two encryption key K1 and K2, which are different only in one bit .
23
…
Experiments can be performed like this
◦ Choose K1 and K2 such that they are different only for one bit,
◦ C1 = Enc(P, K1)
P: Plain-image
◦ C2 = Enc(P, K2)
◦ Cipher-image difference |C1-C2| or calculate MSE(C1, C2)
◦ D1 = Dec(C1, K1)
◦ D2 = Dec(C1, K2)
◦ Decipher-image difference |D1 -D2| or calculate MSE(D1, D2)
24
2- Histogram Analysis
The cipher-image histogram analysis is one of the most straight-forward methods of
illustrating the image encryption quality.
Since a good image encryption method tends to encrypt a plain-image to random-like,
it is desired to see a uniformly-distributed histogram for a cipher-image.
Next Figs. shows several cipher-Image histograms from the encrypted images.
◦ Note that, the cipher-Image images are random-like
25
Differential Attack (Differential cryptanalysis)
Objective of the attack:
◦ An unauthorized third-party preforms differential attack to find out secret key or
original image.
Method of attack:
◦ Original image is altered and observed for any random results in the encrypted
image
◦ This attack compares the variations in the input with variations in the encrypted
output to find the desired key or plain image
26
…
Differential cryptanalysis is a form of the basic chosen plaintext attack
That is, you’ve got a family of texts T0, T1, T2, …, where each text Ti is
equal to T0 plus some small difference. For many differential attacks,
that small difference is one bit.
http://www.goodmath.org/blog/2008/10/02/differential-cryptanalysis/ 27
3- UACI and NPCR Tests
The number of changing pixel rate (NPCR) and the unified averaged changed intensity
(UACI) are two most common quantities used for evaluating the resistance of
deferential attacks for an image encryption algorithm.
Mathematically, the NPCR and the UACI scores between two cipher-images C1 and C2,
whose plain-images are slightly different, can be defined as see next slide
28
• NPCR concentrates on the
absolute number of pixels
which changes values in
differential attacks
• UACI focuses on the averaged
difference between the paired
cipher-images.
29
… EvalCriteria.py
30
4- MSE and PSNR
Mean square error (MSE): For two images A, B you take
the square of the difference between every pixel in A
and the corresponding pixel in B, sum that up and divide
it by the number of pixels.
◦ MSE(img1, img1) = 0
Peak Signal-to-Noise Ratio (PSNR): The signal in this
case is the original data, and the noise is the image
MAXI=Maximum value of pixel in
produced by operation (encryption for example).
◦ The term peak signal-to-noise ratio (PSNR) is an expression original image (255 or 1 for floating
for the ratio between the maximum possible value (power) based image)
of a signal and the power of distorting noise that affects the
quality of its representation
◦ The higher the PSNR, the better the quality of the
reconstructed image
31
… EvalCriteria.py
def MSE(I1, I2): # I1 and I2 are two numPy objects Note: convert the images
R, C = I1.shape from unsigned 8-bit
return np.sum((I1.astype(‘float')-I2.astype(‘float'))**2,
integers to float point,
axis=None) / (R*C) that way we don’t run
into any problems with
modulus operations
def PSNR(I1, I2): “wrapping around”.
return 10 * math.log10(255*255 / MSE(I1,I2))
32
XOR Operation
The XOR encryption algorithm is an example of symmetric encryption where the same key is
used to both encrypt and decrypt a message
xorEnc.py
def xoring(arr, key):
R, C, D = arr.shape
rng = np.random.default_rng(seed=key)
keyImg = rng.integers(0, 256, size=(R, C, D), dtype="uint8")
return arr ^ keyImg
https://www.pcmag.com/encyclopedia/term/xor 33
x(n +1) = x(n)(1- x(n))
34
…
def logistic_key(x, r, ignoreS, size): image = Image.open(r"..\images\
key = np.zeros(size, dtype="uint8") pepper.jpg")
for i in range(ignoreS): image = image.convert('L')
x = r*x*(1-x)
arr = np.asarray(image, dtype="uint8")
for i in range(size):
x = r*x*(1-x)
R, C = arr.shape
EncLogistic.py
key[i] = (x*pow(10, 16)) % 256 key = logistic_key(0.001, 3.95, 100, R*C)
return key
arr1 = diffusion(arr, key)
def diffusion(arr, key):
R, C = arr.shape
arr2 = diffusion(arr1, key)
result = arr ^ key.reshape(R, C)
return result
35
… (Performance Measurements)
P Row CC = 0.96
P Col CC = 0.94
Entropy P = 7.58
C Row CC = -0.004
C Col CC = 0.0469
Entropy C = 7.996
36
…
Key Sensitivity : after changing the start point during creating key
◦ MSE (C1,C2)= 10939.3
◦ MSE (D1,D2)= 8319.9
◦ What if we change one bit after creating the key?
Withstand against differential attack: after changing the starting point during
creating key
◦ NPCR=99.5%
◦ UACI=33.4%
Withstand against differential attack (changing one bit in the plain image):
◦ NPCR=0.5847953216374269% week (why?)
◦ UACI=0.009186750035411482%
37
Image Scrambling using Logistic Map
How to shuffle the pixel positions but keeping its values
intact?
◦ The heart of the program is to generate a list of indices that have a
random nature Idx
◦ The list will be used to shuffle the image column-wise and/or row-wise
38
… ShuffleLogistic.py
def generateKey(x, r, ignoreS, size): def scramble(arr, index): column-wise scrambling
key = np.zeros(size)
R, C = arr.shape
for i in range(ignoreS):
b = np.zeros((R, C), dtype="uint8")
x = r*x*(1-x)
for i in range(size):
b = arr[:, index]
x = r*x*(1-x) return b
key[i] = x
index = np.argsort(key) def unscarmble(arr, index):
return index
R, C = arr.shape
b = np.zeros((R, C), dtype="uint8")
index = generateKey(0.001, 3.95, 100, C)
arr = scramble(arr, index) b[:, index] = arr
arr = unscarmble(arr, index) return b
39
Chaotic Discretization: Many Methods
Method1: For image encryption using XOR operation of pixel ranges from [0,255]
◦ D = (math.floor(x*(math.pow(2,35)-1)))%256
Method2: For shuffling image columns
◦ D = (math.floor(x*(math.pow(2,35)-1)))%Width
Method3: Extract particular numbers from x (part from the fraction)
◦ D = x*10000 – math.floor(x*10000) # return the fraction part
40
Other Maps: JSMP
JSMP generate chaotic values in the range
[0 to 1)
41
Other Maps: Tent Map
Tent generate chaotic values in the range [0 to 1)
42
Other Maps: Bakers Map
It is a 2D chaotic map from the unit square into itself
Here the control parameters are the initial values for x and y
For example: We can use x for confusion and y for diffusion , or x for column
scrambling and y for row scrambling
43
Other Maps: Henon Map
2D Map
Classical Henon map have values of a = 1.4 and b = 0.3 in order to behave as
chaotic.
44
Other Maps: 2D Logistic Map logistic2d.py
45