1. Basic: image encryption and decryption based on XOR operation
Generally, the process of image encryption and decryption is realized by bitwise XOR operation. The encryption can be realized by bitwise XOR between the original image and the key image, and the decryption process can be realized by bitwise XOR between the encrypted image and the key image.
Opencv Python code implementation
import cv2 import numpy as np demo = cv2.imread("E:\matlab_file\picture\picture.jpg", 0) r, c = demo.shape key = np.random.randint(0, 256, size=(r, c), dtype=np.uint8) # Generate random key image cv2.imwrite("E:\matlab_file\picture\key.jpg", key) # Save key image cv2.imshow("demo", demo) # Display original image cv2.imshow("key", key) # Display key image encryption = cv2.bitwise_xor(demo, key) # encryption cv2.imwrite("E:\matlab_file\picture\encryption.jpg", encryption) # Save the encrypted image decryption = cv2.bitwise_xor(encryption, key) # decrypt cv2.imwrite("E:\matlab_file\picture\decryption.jpg", decryption) # Save the decrypted image cv2.imshow("encryption", encryption) # Display ciphertext image cv2.imshow("decryption", decryption) # Displays the decrypted image cv2.waitKey(-1) cv2.destroyAllWindows()
2. Advanced: form an XOR template based on chaotic sequence to realize image encryption and decryption
Chaotic system is a nonlinear system, which shows very complex pseudo randomness and conforms to the confusion rule. It is very sensitive to the initial conditions and control parameters. Any small initial deviation will be exponentially amplified and comply with the diffusion rule. At the same time, it is deterministic and can be completely determined by the equations, parameters and initial conditions of the nonlinear system. Therefore, the change of initial state and a small number of parameters can produce chaotic cipher sequences that meet the basic characteristics of cryptography. The combination of chaos theory and encryption technology can form a good image encryption system. At present, chaotic systems commonly used in image encryption include Logistic chaotic map, Chebychev map, piecewise linear chaotic map, Cubic map, standard map, Henon map, Lorenz chaotic map, Chua's chaos, Rossler chaotic system, two-dimensional Sinai map, Chen's chaotic system, etc. Among the image encryption methods based on chaos, some use chaotic system to generate pseudo-random sequence for encryption in the form of sequence cipher. Some use the ergodicity of chaos to process the generated pseudo-random sequence to obtain the pixel position after scrambling, and then scrambling the pixel position. Some use the reversible characteristics of some chaotic calculation expressions to substitute pixel values into chaotic calculation expressions for pixel substitution and diffusion. Compared with the traditional encryption algorithm, chaotic image encryption algorithm has the advantages of large key space, simple implementation and fast encryption speed. However, image encryption based on chaos has the following shortcomings: ① the limited accuracy of computer may lead to the short period and poor randomness of chaotic sequence. ② Most of the existing chaotic encryption technologies are based on one-dimensional or two-dimensional chaotic systems, which are vulnerable to phase space reconstruction methods. ③ Some chaotic encryption algorithms adopt complex chaotic systems, which are slow and can not realize real-time encryption.
In this paper, the composite chaotic encryption algorithm is used to encrypt and decrypt the image Venus. The detailed principle is shown in: Digital image encryption method based on compound chaotic system
#!/usr/bin/env python # _*_ coding:utf-8 _*_ """ @Author : LitraLin @File : Cryption.py @CreateTime : 2021/10/07 @Description : Compound chaos Encryption and Decryption of image """ import cv2 import math import numpy as np def int2bin8(x): # Integer to 8-bit binary result=""; for i in range(8): y=x&(1) result+=str(y) x=x>>1 return result[::-1] def int2bin16(x): # Integer to 8-bit binary result=""; for i in range(16): y=x&(1) result+=str(y) x=x>>1 return result def Encryption(img,j0,g0,x0,EncryptionImg): x = img.shape y = img.shape c = img.shape g0 = int2bin16(g0) for s in range(x): for n in range(y): for z in range(c): m = int2bin8(img[s][n][z]) # Pixel value to octet binary ans="" # print("ok") for i in range(8): ri=int(g0[-1]) # Take the last digit of the manual cipher machine qi=int(m[i])^ri # XOR with pixel value qi xi = 1 - math.sqrt(abs(2 * x0 - 1)) # f1(x) chaotic iteration if qi==0: # If qi=0, use x0i+x1i=1; xi=1-xi; x0=xi # xi iteration t=int(g0)^int(g0)^int(g0) # Primitive polynomial x^15+x^3+1 g0=str(t)+g0[0:-1] # gi iteration ci=math.floor(xi*(2**j0))%2 # Nonlinear transformation operator ans+=str(ci) re=int(ans,2) EncryptionImg[s][n][z]=re # Write new image def Decryption(EncryptionImg, j0, g0, x0, DecryptionImg): x = EncryptionImg.shape y = EncryptionImg.shape c = EncryptionImg.shape g0 = int2bin16(g0) for s in range(x): for n in range(y): for z in range(c): cc = int2bin8(img[s][n][z]) ans = "" # print("no") for i in range(8): xi = 1 - math.sqrt(abs(2 * x0 - 1)) x0 = xi ssi = math.floor(xi * (2 ** j0)) % 2 qi=1-(ssi^int(cc[i])) ri = int(g0[-1]) mi=ri^qi t = int(g0) ^ int(g0) ^ int(g0) g0 = str(t) + g0[0:-1] ans += str(mi) re = int(ans, 2) DecryptionImg[s][n][z] = re if __name__ == "__main__": img = cv2.imread(r"E:\matlab_file\picture\Correlation_matrix.png", 1) # Read original image EncryptionImg = np.zeros(img.shape, np.uint8) Encryption(img,10,30,0.123345,EncryptionImg) # encryption cv2.imwrite(r"E:\matlab_file\picture\Correlation_matrix-EncryptionImg.png",EncryptionImg) # Save the encrypted image img = cv2.imread(r"E:\matlab_file\picture\Correlation_matrix-EncryptionImg.png", 1) # Read encrypted image DecryptionImg = np.zeros(img.shape, np.uint8) Decryption(img, 10, 30, 0.123345, DecryptionImg) # decrypt cv2.imwrite(r"E:\matlab_file\picture\Correlation_matrix-DecryptionImg.png", DecryptionImg) # Save the decrypted image cv2.waitKey(0)