├── .gitignore ├── Images ├── camera.jfif ├── glas-quadratisch-ga21492-taj-mahal-30x30cm-final-einzel.jpg └── logistic-mandelbrot.gif ├── Logistic map ├── logisticKey.py └── substitutionEncryption.py ├── Lorenz encryption ├── lorenzEncryption.py ├── lorenzSystem.py └── sustitutionLorenz.py ├── README.md └── Reports ├── Calc Project.pdf └── ODE Report.pdf /.gitignore: -------------------------------------------------------------------------------- 1 | __pycache__ 2 | .idea -------------------------------------------------------------------------------- /Images/camera.jfif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Saransh-cpp/Chaotic-Encryption/64d842b2dc17d0481addbff479236751aaee1faa/Images/camera.jfif -------------------------------------------------------------------------------- /Images/glas-quadratisch-ga21492-taj-mahal-30x30cm-final-einzel.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Saransh-cpp/Chaotic-Encryption/64d842b2dc17d0481addbff479236751aaee1faa/Images/glas-quadratisch-ga21492-taj-mahal-30x30cm-final-einzel.jpg -------------------------------------------------------------------------------- /Images/logistic-mandelbrot.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Saransh-cpp/Chaotic-Encryption/64d842b2dc17d0481addbff479236751aaee1faa/Images/logistic-mandelbrot.gif -------------------------------------------------------------------------------- /Logistic map/logisticKey.py: -------------------------------------------------------------------------------- 1 | def logistic_key(x, r, size): 2 | """ 3 | This function accepts the initial x value, 4 | r value and the number of keys required for 5 | encryption. 6 | The function returns a list of pseudo-random 7 | numbers generated from the logistic equation. 8 | """ 9 | 10 | key = [] 11 | 12 | for i in range(size): 13 | x = r*x*(1-x) # The logistic equation 14 | key.append(int((x*pow(10, 16))%256)) # Converting the generated number between 0 to 255 15 | 16 | return key 17 | -------------------------------------------------------------------------------- /Logistic map/substitutionEncryption.py: -------------------------------------------------------------------------------- 1 | import logisticKey as key # Importing the key generating function 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | import matplotlib.image as img 5 | 6 | # Accepting an image 7 | path = str(input('Enter the path of image\n')) 8 | image = img.imread(path) 9 | 10 | # Displaying the image 11 | plt.imshow(image) 12 | plt.show() 13 | 14 | # Generating dimensions of the image 15 | height = image.shape[0] 16 | width = image.shape[1] 17 | print(height, width) 18 | 19 | # Generating keys 20 | # Calling logistic_key and providing r value such that the keys are pseudo-random 21 | # and generating a key for every pixel of the image 22 | generatedKey = key.logistic_key(0.01, 3.95, height*width) 23 | print(generatedKey) 24 | 25 | # Encryption using XOR 26 | z = 0 27 | 28 | # Initializing the encrypted image 29 | encryptedImage = np.zeros(shape=[height, width, 3], dtype=np.uint8) 30 | 31 | # Substituting all the pixels in original image with nested for 32 | for i in range(height): 33 | for j in range(width): 34 | # USing the XOR operation between image pixels and keys 35 | encryptedImage[i, j] = image[i, j].astype(int) ^ generatedKey[z] 36 | z += 1 37 | 38 | # Displaying the encrypted image 39 | plt.imshow(encryptedImage) 40 | plt.show() 41 | 42 | # Decryption using XOR 43 | z = 0 44 | 45 | # Initializing the decrypted image 46 | decryptedImage = np.zeros(shape=[height, width, 3], dtype=np.uint8) 47 | 48 | # Substituting all the pixels in encrypted image with nested for 49 | for i in range(height): 50 | for j in range(width): 51 | # USing the XOR operation between encrypted image pixels and keys 52 | decryptedImage[i, j] = encryptedImage[i, j].astype(int) ^ generatedKey[z] 53 | z += 1 54 | 55 | # Displaying the decrypted image 56 | plt.imshow(decryptedImage) 57 | plt.show() 58 | -------------------------------------------------------------------------------- /Lorenz encryption/lorenzEncryption.py: -------------------------------------------------------------------------------- 1 | """ 2 | Encrypting an image first by shuffling 3 | it's pixels and then substituting the 4 | shuffled pixels using pseudo-random 5 | numbersngenerated from Lorenz system 6 | of differential equations 7 | """ 8 | 9 | # Importing all the required libraries 10 | import matplotlib.image as img 11 | import matplotlib.pyplot as plt 12 | import numpy as np 13 | import lorenzSystem as key 14 | 15 | # Accepting Image using it's path 16 | path = str(input('Enter path of the image\n')) 17 | image = img.imread(path) 18 | 19 | # Displaying original image 20 | plt.imshow(image) 21 | plt.show() 22 | 23 | # Storing the size of image in variables 24 | height = image.shape[0] 25 | width = image.shape[1] 26 | 27 | # Using lorenz_key function to generate 3 lists of keys 28 | xkey, ykey, zkey = key.lorenz_key(0.01, 0.02, 0.03, height*width) 29 | 30 | # Initializing empty index lists to store index of pixels 31 | xindex = [] 32 | yindex = [] 33 | 34 | # Initializing an empty image to store the encrypted image 35 | encryptedImage = np.zeros(shape=[height, width, 3], dtype=np.uint8) 36 | l = 0 37 | 38 | # Populating xindex 39 | for i in range(width): 40 | xindex.append(i) 41 | 42 | # Populating yindex 43 | for i in range(height): 44 | yindex.append(i) 45 | 46 | # Re-arranging xindex and xkey to increase randomness 47 | for i in range(width): 48 | for j in range(width): 49 | if xkey[i] > xkey[j]: 50 | xkey[i], xkey[j] = xkey[j], xkey[i] 51 | xindex[i], xindex[j] = xindex[j], xindex[i] 52 | 53 | # Re-arranging yindex and ykey to increase randomness 54 | for i in range(height): 55 | for j in range(height): 56 | if ykey[i] > ykey[j]: 57 | ykey[i], ykey[j] = ykey[j], ykey[i] 58 | yindex[i], yindex[j] = yindex[j], yindex[i] 59 | 60 | # Shuffling original image's pixels and storing them 61 | # in an empty image 62 | for i in range(height): 63 | k = 0 64 | for j in range(width): 65 | encryptedImage[i][j] = image[yindex[k]][j] 66 | k += 1 67 | 68 | # Shuffling original image's pixels and storing them 69 | # in an empty image 70 | for i in range(height): 71 | k = 0 72 | for j in range(width): 73 | encryptedImage[i][j] = encryptedImage[i][xindex[k]] 74 | k += 1 75 | 76 | # Displaying the shuffled image 77 | plt.imshow(encryptedImage) 78 | plt.show() 79 | 80 | # XORing each pixel with a pseudo-random number generated above/Performing the 81 | # substitution algorithm 82 | l = 0 83 | for i in range(height): 84 | for j in range(width): 85 | # Converting the pseudo-random nuber generated into a number between 0 and 255 86 | zk = (int((zkey[l]*pow(10, 5))%256)) 87 | # Performing the XOR operation 88 | encryptedImage[i, j] = encryptedImage[i, j]^zk 89 | l += 1 90 | 91 | # Displaying the shuffled then substituted encrypted image 92 | plt.imshow(encryptedImage) 93 | plt.show() 94 | 95 | 96 | 97 | 98 | # decryptedImage = np.zeros(shape=[height, width, 3], dtype=np.uint8) 99 | 100 | # l = 0 101 | # for i in range(height): 102 | # for j in range(width): 103 | # zk = (int((zkey[l]*pow(10, 5))%256)) 104 | # decryptedImage[i, j] = encryptedImage[i, j]^zk 105 | # l += 1 106 | 107 | # print(decryptedImage[0][0]) 108 | 109 | # plt.imshow(decryptedImage) 110 | # plt.show() 111 | 112 | 113 | # for i in range(height): 114 | # k = 0 115 | # for j in range(width): 116 | # encryptedImage[i][xindex[k]] = encryptedImage[i][j] 117 | # k += 1 118 | 119 | # print(image[70][0]) 120 | # print(encryptedImage[0][0]) 121 | 122 | 123 | # for i in range(height): 124 | # k = 0 125 | # for j in range(width): 126 | # decryptedImage[yindex[k]][j] = encryptedImage[i][j] 127 | # k += 1 128 | 129 | # print(image[70][0]) 130 | # print(encryptedImage[0][0]) 131 | 132 | 133 | # plt.imshow(decryptedImage) 134 | # plt.show() 135 | -------------------------------------------------------------------------------- /Lorenz encryption/lorenzSystem.py: -------------------------------------------------------------------------------- 1 | # Importing the required libraries 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | 5 | 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 28 | dt = 0.01 29 | 30 | # Initializing 3 empty lists 31 | xs = np.empty(num_steps + 1) 32 | ys = np.empty(num_steps + 1) 33 | zs = np.empty(num_steps + 1) 34 | 35 | # Initializing initial values 36 | xs[0], ys[0], zs[0] = (xinit, yinit, zinit) 37 | 38 | # Initializing constants 39 | s = 10 40 | r = 28 41 | b = 2.667 42 | 43 | # System of equations 44 | for i in range(num_steps): 45 | xs[i + 1] = xs[i] + (s * (ys[i] - xs[i]) * dt) 46 | ys[i + 1] = ys[i] + ((xs[i] * (r - zs[i]) - ys[i]) * dt) 47 | zs[i + 1] = zs[i] + ((xs[i] * ys[i] - b * zs[i]) * dt) 48 | 49 | # Uncomment to plot Lorenz system 50 | 51 | # fig = plt.figure() 52 | # ax = fig.gca(projection='3d') 53 | 54 | # ax.plot(xs, ys, zs) 55 | # plt.show() 56 | 57 | return xs, ys, zs 58 | 59 | # Uncomment to plot Lorenz system 60 | 61 | # lorenz_key(0.01, 0.02, 0.03, 1000) 62 | 63 | -------------------------------------------------------------------------------- /Lorenz encryption/sustitutionLorenz.py: -------------------------------------------------------------------------------- 1 | """ 2 | Encrypting an image through substitution algorithm 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 decrypted 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() -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Chaotic Encryption 2 | This repository contains the code for encrypting an image using various techniques written completely in python. 3 | 4 | ## Chaotic systems used 5 | ### 1. Logistic map equation 6 | ### 2. Lorenz system of differential equations 7 | 8 | ## Encryption methods used 9 | ### 1. Only substitution (Confusion method) 10 | Here, the code substitutes every pixel of the image with another pixel (which is obtained be XORing the initial pixel's value with a pseudo-random number (different for every pixel) generated using one of the above mentioned PRNG) thus making the encryption very secure. 11 | ### 2. Shuffling and then Substitution (Diffusion then Confusion method) 12 | Here the code first shuffles the pixels of an image using random numbers (one for every pixel) generated using one of the PRNG and then it performs the above described substitution method with another set of keys. Thus this encryption approach is very secure. 13 | 14 | ## Connecting chaos with fractals 15 | The code also aims to connect chaos with fractals which is inspired from [here](https://github.com/jonnyhyman/Chaos) 16 | 17 | ## Final reports 18 | ### 1. Report for encryption using Lorenz system of differential equations 19 | The report can be found [here](https://github.com/Saransh-cpp/Chaotic-Encryption/blob/master/Reports/ODE%20Report.pdf). 20 | ### 2. Report for encryption using logistic map and then connecting chaos with fractals 21 | The report can be found [here](https://github.com/Saransh-cpp/Chaotic-Encryption/blob/master/Reports/Calc%20Project.pdf). 22 | 23 | -------------------------------------------------------------------------------- /Reports/Calc Project.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Saransh-cpp/Chaotic-Encryption/64d842b2dc17d0481addbff479236751aaee1faa/Reports/Calc Project.pdf -------------------------------------------------------------------------------- /Reports/ODE Report.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Saransh-cpp/Chaotic-Encryption/64d842b2dc17d0481addbff479236751aaee1faa/Reports/ODE Report.pdf --------------------------------------------------------------------------------