A Novel Chaotic Image Encryption Algorithm Based on Latin Square and Random Shift

To realize the safe transmission of images, a chaotic image encryption algorithm based on Latin square and random shift is proposed. The algorithm consists of four parts: key generation, pixel scrambling, pixel replacement, and bit scrambling. Firstly, the key is generated from the plain image to improve the sensitivity of the encryption method. Secondly, each pixel in each row of the image matrix is moved cyclically to the right, in turn, to change the position of the image pixel and realize pixel position scrambling. Then, a 256-order Latin square matrix composed of a chaotic sequence is used as a lookup table, and the replacement coordinates are calculated based on the image pixel value and the chaotic sequence value, replacing the corresponding coordinate elements in the image matrix. Finally, decompose the bitplane of the image matrix and combine it into two-bit matrices, scramble the two bit matrices, respectively, with the Latin square matrix, recombine the scrambled two-bit matrices, and convert them into decimal to obtain the ciphertext image. In the proposed encryption method, all the Latin square matrices used are generated by chaotic sequences, further enhancing the complexity of the generated Latin square matrix and improving the algorithm's security. Experimental results and security analysis show that the proposed algorithm has good security performance and is suitable for image encryption.


Introduction
With the rapid development of modern communication technology, more and more digital images are transmitted on social networks. ese images carry personal information. erefore, how to protect this private information has become a research hotspot [1]. Due to the inherent characteristics of strong correlation and high redundancy between adjacent pixels of an image, some traditional encryption methods such as Data Encryption Standard (DES) or Advanced Encryption Standard (AES) are not suitable for performing digital images encryption because traditional encryption algorithms have the disadvantage of low efficiency when encrypting digital images [2].
Because some characteristics of the chaotic system are very suitable for the development of image encryption algorithms, such as sensitivity to initial conditions, unpredictability, and ergodicity, among the currently proposed image encryption methods, chaos systems have been widely used [3][4][5]. Wang [6] proposed an image encryption method based on a one-dimensional chaotic system, which improved the structure of control parameters and the sensitivity of one-dimensional chaotic mapping and improved its ability to resist differential attacks. Zhou [7] combined two existing one-dimensional chaotic maps and proposed a new one-dimensional chaotic map, which improved the performance against various attacks. However, some inherent shortcomings of one-dimensional chaotic mapping, such as relatively simple structure and small keyspace, are difficult to eliminate. In contrast, high-dimensional chaotic systems have more complex dynamics and high ergodicity, so people apply high-dimensional chaotic systems to image encryption [8][9][10]. Gan [11] proposed a color image encryption algorithm based on high-dimensional chaos and three-dimensional bit-plane arrangement, which can effectively resist known plaintext attacks and selected plaintext attacks. Zhang [12] proposed a new 3D bit matrix replacement algorithm, and the encryption method developed a new replacement method based on the Chen system to improve the randomness of the scrambling process. However, the encryption scheme is vulnerable to attack by the selected plaintext. Image encryption algorithms based on high-dimensional chaos often scramble the pixels of the image by obtaining the index vector of the chaotic sequence. e security of these scrambling methods only depends on the index vector because the attacker may analyze the difference between the cipher image and the plain image. e relationship to obtain the index vector causes the scrambling operation to be invalid.
In recent years, due to the excellent performance of the Latin square in image encryption methods, it has received extensive attention from researchers. e Latin square is a special square matrix with uniformity. Shannon first pointed out the relationship between the Latin square and cryptography [13]. Latin squares have some good features that are very suitable for image encryption: the number of Latin squares is huge, and the number of Latin squares of the 10 th order is about 10 37 , so its keyspace is large and can prevent brute force attacks.
e Latin square has a unified histogram, which means that using the Latin square for image encryption can effectively resist statistical analysis. Because of the good characteristics of the Latin square, Wu [14] proposed a symmetric encryption algorithm and designed a new loom-like 2D substitutionpermutation network. is network maintains good confusion and diffusion characteristics while also with additional fault tolerance. Panduranga [15] used a chaotic system and Latin square to construct an image encryption method, which was later cracked by Ahmad M. and Ahmad F. [16].
Aiming at the characteristics of chaotic system and Latin square matrix, an image encryption algorithm (LSRS) based on Latin square and random shift is designed. e algorithm is divided into four parts: key generation, pixel scrambling, pixel replacement, and bit scrambling. Latin squares are all produced by chaotic sequences, which further enhances the complexity of Latin squares. In the pixel scrambling part, the cyclic shift step length of the pixel is controlled by the chaotic sequence, which makes the pixel distribution more random. e histogram of the 256-order Latin square matrix is evenly distributed; using it as a lookup table to replace the pixels can effectively increase the Shannon entropy of the cipher image. At the same time, the image pixel value and the rounded chaotic sequence jointly calculate the coordinates of the replaced pixels, which effectively improves the randomness of the algorithm, through the Latin square matrix to scramble the bit plane of the image matrix to enhance the security of the algorithm. e security analysis of the encrypted cipher image shows that the LSRS algorithm has good security performance and is suitable for practical applications. e rest of this article is organized as follows: in Section 2, the related concepts of Latin squares and chaotic systems are introduced. Section 3 gives the detailed scheme of the LSRS method. Section 4 analyzes the safety of the proposed method. Finally, the conclusions are given in Section 5.

Related Work
e famous mathematician and physicist Euler used the Latin alphabet as the symbol of the elements in the Latin square, and the Latin square got its name. For an N × N matrix with only N different elements and each element appears only once in any row or column, the matrix is called a Latin square matrix. e application of the Latin square matrix is to double control the row vector and column vector of the image matrix to promote the uniform distribution of pixels and improve the balance of pixels in the matrix. Figure 1 shows examples of Latin squares with different symbol sets.

Encryption Scheme
In the encryption algorithm, P represents a plain image with a size of N × N, and C represents the corresponding cipher image. e frame diagram of the encryption scheme is shown in Figure 3.

Generate Key.
e plain image to generate the key can associate the plain image with the cipher image and enhance the sensitivity of the key. e method of generating the key in this paper is as follows: divide the input image matrix P into four blocks; calculate the sum of the elements in each matrix; and obtain LL 1 , LL 2 , LL 3 , and LL 4 to generate keys x 0 , y 0 , z 0 , and w 0 . e method of its generation is 2 Computational Intelligence and Neuroscience where LL � mod (x, y) returns the remainder after dividing x by y, x is the dividend, y is the divisor, and floor (x) means rounding the elements of x to the negative infinity. e key generation method is where bitxor represents the bitwise exclusive or between two values and |x| represents rounding of x. Input the generated

e Generation of Latin Square Matrix.
Given two sequences Q 1 and Q 2 of equal length and sorting them respectively, the corresponding index sequences Q seed and Q shift are obtained. According to Algorithm 1 [18], generate Latin square matrix L.

Pixel Scrambling.
Pixel scrambling can effectively break the correlation between adjacent pixels and improve the security of encryption algorithms. In this paper, adaptive shifting is used to realize pixel position scrambling, and each pixel in each row (column) is cyclically moved to the right in order from left to right. e step length of each pixel shift in the same row (column) is controlled by the element value of the chaotic sequence, and it is also affected by the previous pixels. is method can increase the complexity of scrambling; even if a chaotic sequence of a certain length is deciphered, it is difficult to restore the image pixels after scrambling correctly. e N × N elements before the chaotic sequence X and Y are, respectively, intercepted, and two equal sequences Q r1 and Q r2 are obtained. e preprocessing is performed according to formula (4) to get the sequences Q u1 and Q u2 , and they are, respectively, converted into N × N. e matrix Q row and the matrix Q col are used for row scrambling and column scrambling of the input image matrix: where k � 1, 2, 3, ..., N. First, use the matrix Q row to scramble each row of the image matrix P to obtain the matrix P ′ , and then use the matrix Q col to scramble each column of the matrix P ′ to get the scrambled matrix P s . Figure 4 shows an example of the scrambling process of the image matrix P. To better explain this scrambling method, the first row of the image matrix P and the first row of the matrix Qrow shown in Figure 4 are taken as an example to introduce the adaptive scrambling process in detail, where the adaptive shifting process of the remaining rows (columns) of the image matrix P is similar to the scrambling process of the first row of adaptive shifting and will not be described in detail. e scrambling method is as follows: (1) e sequence of the first row of the image matrix P is sequence T � [101, 33, 44, 55, 12], and the sequence of the first row of the matrix Q row used to shift the pixels is sequence U � [2, 0, 4, 2, 3]; (2) e first element in the sequence T is 101, and its first element in the corresponding pixel shift sequence U is 2; then, the first element 101 in the sequence T is cyclically moved two positions to the right, getting sequence T 1 � [33, 44, 101, 55, 12]; (3) e second element in sequence T 1 is 44, and its second element in the corresponding pixel shift sequence U is 0; then, the second element 44 in sequence T 1 is cyclically moved zeros positions to the right, getting sequence T 2 � [ 33, 44, 101, 55, 12]; (4) e third element in sequence T 2 is 101, and its third element in the corresponding pixel shift sequence U is 4; then, the third element 101 in sequence T 2 is cyclically moved four positions to the right, getting sequence T 3 � [ 33, 101, 44, 55, 12]; (5) e fourth element in sequence T 3 is 55, and its fourth element in the corresponding pixel shift sequence U is 2; then, the fourth element 55 in sequence T 3 is cyclically moved two positions to the right, getting sequence T 4 � [55, 33, 101, 44, 12]; (6) e fifth element in sequence T 4 is 12, and its fifth element in the corresponding pixel shift sequence U is 3; then, the fifth element 12 in sequence T 4 is cyclically moved three positions to the right, getting sequence T 5 � [55, 33, 12, 101, 44]; (7) Repeat the scrambling processes (1)-(6) to the remaining rows of the image matrix P, until each row is scrambled to obtain the matrix P ′ . (8) Similar to processes (1)-(7), use matrix Q col to scramble each column of matrix P ′ to obtain the scrambled matrix P S .

Pixel Replacement.
In the encryption algorithm, replacing the pixels of the plain image can effectively hide the original information of the plain image. However, some encryption algorithms use the replacement method of addition, subtraction, and xor, which is too simple to protect the image information effectively. To solve this defect,  generate a 256-order Latin square matrix, and use the elements in this Latin square matrix to replace the pixels of the image. e number of Latin squares of order 256 is about 256! ≈ 2 1684 , so its keyspace is large enough, and its histogram is evenly distributed, which is difficult to crack using statistical attack analysis. Given two sequences Q t1 and Q t2 of length 256, generate a Latin square matrix L table as a lookup table. According to formula (5), get Q s1 and Q s2 and respectively converted into N × N matrices L c and L r : where Z[a: b] means to intercept the elements whose index values are between a and b from the sequence Z (including the elements corresponding to the index values a and b).
rough the matrix L c and the corresponding coordinate element of the image matrix to be replaced to do the addition operation, take the remainder so that the elements in the resulting remainder matrix L c ′ are between 0 and 255. Using L c ′ as the row coordinate index matrix and the matrix L r as the column coordinate index matrix, from the lookup table, search the corresponding element for replacement, and get the pixel replacement matrix P r . e replacement method is j) indicates the row index and L r (i, j) indicates the column index. Algorithm 2 is the Latin square replacement process.

Bit Scrambling.
e pixel value of grayscale images ranges from 0 to 255, and an 8-bit binary sequence can represent each pixel. erefore, the grayscale image can be decomposed into eight bit-planes, where the ith bit plane is composed of the ith bit of all pixels (i � 1, 2, ..., 8). e high bit-plane contains the visual information of the plain image, and the low-bit plane contains the detailed information of the plain image [19]. Mixing the bits in the high-bit plane and the low-bit plane of the plain image can hide the Input: Q 1 and Q 2 are two sequences of equal length Output: Latin matrix with order equal to the length of the input sequence [∼, Q seed ] � Sort (Q 1 ); [∼, Q shift ] � Sort (Q 2 ); for i � 0 : 1: length (Q shift )-1 L � Rowshift (Q seed , Q shift (i)) end where Sort (x) is a sorting function, which can sort the sequence x in ascending order and return the sorted sequence and its position index sequence in the original sequence. Q seed and Q shift are the position index sequences obtained after sorting the sequences Q 1 and Q 2 , length (x) represents the length of the sequence x, and Rowshift (x, y) means to move the sequence Q seed loop to the left by y positions.  Computational Intelligence and Neuroscience information of the plain image and improve the security of the algorithm.
To better scramble the bits of the image, we decompose the image into eight bit-planes and then recombine and scramble them. First, the image matrix P is decomposed into eight bit-planes P (1)-P (8). Secondly, the four bit-planes P (1), P (3), P (5), and P (7) of the image matrix P combine into a 2N × 2N bit matrix PA, and the four bit-planes P (2), P (4), P (6), and P (8) of the image matrix P are combined into a 2N × 2N bit matrix PB. en, intercept 8N elements from the chaotic sequence W and divide them into four equal sequences QD 1 , QD 2 , QD 3 , and QD 4 . Finally, use the sequences QD 1 and QD 2 to generate the Latin square matrix LH 1 , and use the sequences QD 3 and QD 4 to generate the Latin square matrix LH 2 , LH 1 , and LH 2 , respectively used for bit scrambling of matrix PA and matrix PB. Figure 5 shows the bit plane of the image matrix P is scrambled. Decompose the image matrix P into eight bit-planes, and the bit plane P (1) as shown in Figure 5(a). Arrange according to the method shown in Figure 5(b) to obtain a bit matrix PA and a bit matrix PB. Use the matrix LH 1 to perform row scrambling on the bit matrix PA to get PA ′ , use LH 2 to perform column scrambling on the bit matrix PB to obtain PB ′ , as shown in Algorithm 3, the scrambling process. e bit matrix PA ′ and bit matrix PB ′ as shown in Figure 5(c) are obtained. Finally, through the bit matrix PA ′ and bit matrix PB ′ recombined into eight bit-planes and converted to decimal, the resulting image matrix P d is shown in Figure 5(d).

Image Encryption Process.
e LSRS algorithm proposed in this paper consists of four parts: key generation, pixel scrambling, pixel replacement, and bit scrambling. e steps of the encryption algorithm are described as follows: Step 1.
rough the image matrix P of size N × N, generate the initial parameters x 0 , y 0 , z 0 , and w 0 of the keys. Input the generated keys parameters x 0 , y 0 , z 0 , and w 0 into the hyperchaotic Lorenz system, iterate 999 + 3M × M times (when N < 256, M � 256, when N ≥ 256, M � N), and discard the first 999 times; the chaotic sequence X, Y, Z, W was obtained.
Step 2. Intercept the first N × N elements of the chaotic sequence X and Y, respectively, as the sequences Q r1 and Q r2 ; preprocess them according to formula (4); convert them, respectively, into N × N matrix Q row and matrix Q col ; and perform row scrambling and column scrambling of matrix P on the image to obtain the scrambling image matrix P s .
Step 3. Intercept the first 512 elements of the chaotic sequence Z, divide them into two equal sequences Q t1 and Q t2 , and use them to generate a Latin square matrix Q table as a lookup table. According to formula (5) to obtain Q s1 and Q s2 , respectively convert into N × N matrices L c and L r .
rough the matrix L c and the corresponding coordinate element of the image matrix to be replaced to do the addition operation, take the remainder so that the elements in the resulting remainder matrix L ' c are between 0 and 255. e matrix L ' c and the matrix L r form a two-dimensional index matrix, and the index matrix is used to select elements from the corresponding coordinates in the matrix Q table and replace the elements in the matrix P s to obtain the pixel replacement matrix P r .
Step 4. Intercept 8N elements from the chaotic sequence W , divide them into four sequences QD 1 , QD 2 , QD 3 , and QD 4 of equal length. Use QD 1 and QD 2 to generate Latin square matrix LH 1 , QD 3 , and QD 4 to generate Latin square matrix LH 2 . Divide the matrix P r into eight bit-planes P (1)-P (8), where the four bit-planes P (1), P (3), P (5), and P (7) are combined into a 2N × 2N bit matrix PA, and P (2), P (4), P (6), and P (8) four bit-planes are combined into a 2N × 2N bit matrix PB. rough matrix LH 1 to scramble the rows of matrix PA, LH 2 performs column scrambling on the matrix PB.
e scrambled matrix PA ′ and matrix PB ′ are, respectively, divided into four bit-planes, and the resulting bit planes are combined into a bit plane matrix P bit ′ . Convert the elements in the bit plane matrix P bit ′ to decimal data and finally obtained the cipher image C. e reverse process of the encryption algorithm is the decryption process, which will not be repeated.

Simulation Results.
In order to study the confidentiality performance of the LSDR method, use MATLAB 2019a to simulate the encryption algorithm. e configuration environment of the computer is Windows 10, 8.00 GB RAM, Intel (R) Core (TM) i7-4510 CPU @ 2.00 GHz. Figure 6 shows the plain image, cipher image, and decrypted image of Lena, Boat, Hill, and Peppers. By directly observing the cipher image, no valid information can be identified, so the algorithm is feasible. Input: Plain image P, L c , L r , and L table. Output: Image P r after pixel replacement. for i � 0 : 1: N−1 do for j � 0:1: 1, 2, . . ., 8) represents obtaining the ith bit plane of the plain image P.

Keyspace.
e key is the most critical part of the encryption scheme. e larger the keyspace, the stronger the ability to resist brute force attacks, so the key should have enough space to resist brute force attacks. In theory, when the keyspace reaches 2 100 , it is enough to resist the brute force attacks that currently exist. e algorithm proposed has four parameters x 0 , y 0 , z 0 , and w 0 . e calculation accuracy of these four parameters is 10 −15 , and the keyspace can reach 10 60 ≈ 2 190 , which is much larger than 2 100 . erefore, the keyspace of the LSRS algorithm is large enough to protect the security of the image.

Differential Attack Analysis.
Differential attack refers to studying the influence of the difference between plain images on their cipher image and establishing a relationship between the plain image and its corresponding cipher image, thereby cracking the encryption method. e number of Pixel Change Rate (NPCR) and Unified Pixel Average Change Intensity (UACI) are two methods to test whether the encryption method can resist differential attacks [23]. NPCR reflects the ratio of the number of unequal pixels in the same position of two images to the number of all pixels in the image. UACI is the overall average change density, which represents the average change intensity of the planar image. e ideal values of NPCR and UACI are, respectively, 99.6094% and 33.4635%. Assuming that P 1 and P 2 are two cipher images and their plain images only have a one-bit difference, their NPCR and UACI values are calculated as shown in the following formula: Taking Lena image as an example, Table 1 compares the test results of NPCR and UACI under different algorithms. It can be seen that the encryption method we propose can resist differential attacks more effectively.

Key Sensitivity Analysis.
To ensure the security of the encryption algorithm, the encryption algorithm should be highly sensitive to the input key. When the wrong key is entered to decrypt the cipher image, the output is an image that cannot identify any information. e LSDR algorithm proposed in this paper uses the Lena image as a test. First, input the image into the LSDR algorithm to obtain the keys � [x 0 , y 0 , z 0 , w 0 ] and the cipher image. e element x 0 in the keys is slightly modified by adding 1 to the 15 th digit after the decimal point and then using the modified keys ′ � [x 0 + 10 −15 , y 0 , z 0 , w 0 ] to decrypt. Figure 7 shows the decrypted image under the correct key and the decrypted image obtained after modifying the key parameters x 0 , y 0 , z 0 , and w 0 , respectively. It can be seen that even if the decryption key changes slightly, the correct decrypted image cannot be obtained. erefore, the key sensitivity in the LSDR algorithm is high enough to resist all types of brute force attacks.

Histogram Analysis.
e histogram can intuitively reveal the distribution of pixel values in the image. Cipher image has a unified histogram, which can effectively resist statistical analysis, making it difficult for an attacker to obtain valuable information. e more even the pixel distribution in the cipher image, the more ideal the encryption algorithm. As shown in Figure 8, the plain image is input to the LSRS algorithm for encryption, and the histogram of the cipher image obtained is evenly distributed. In addition, the flatness of the histogram can be quantified numerically. e standard method is the chi-square test [24], which is defined as where V 0 � ((M × N)/256), M × N is the size of the image, and V i and V o , respectively, represent the actual and expected frequency of each gray level. Set the significance level as α � 0.05. If X 2 test is less than X 2 α � 293.25 [25], the histogram can be considered to be uniformly distributed. In this paper, test the three images of Lena, Boat, and Hill. From the results in Table 2, it can be seen that the proposed method is   lower than the theoretical value of 293.25. erefore, it can be considered that the program passed the chi-square test.

Correlation
Analysis. e adjacent pixels of the plain image have a high correlation in the horizontal, vertical, and diagonal directions. e ideal encryption algorithm can reduce the correlation of adjacent pixels in the cipher image, thereby effectively resisting statistical attacks. e correlation coefficient calculation formula is where x and y are pixel values, cov (x, y) is the covariance, D (x) is the variance, E (x) is the mean, and ρ xy is the correlation coefficient.
To analyze the correlation between adjacent pixels in the plain image and the cipher image, take Lena image as an example, randomly select 2000 pairs of adjacent pixels in the plain image and the cipher image to test. As shown in Table 3, the distribution of adjacent pixels in the plain image of Lena is highly concentrated, so the correlation between adjacent pixels of the plain image is very high. e distribution of adjacent pixels in the cipher image of Lena is random, which means that after encryption, the correlation between adjacent pixels of the cipher image of Lena is low. By comparing with the other three encryption methods, the results of the LSDR algorithm are also satisfactory.

Global Shannon Entropy and Local Shannon Entropy.
Global Shannon Entropy (GSE) is a disordered statistical measure that reflects the randomness of information. e calculation formula of GSE [26] is where Q is the gray level of the image. For 8-bit grayscale images, Q � 255. m i is the ith gray value on the image, and P(m i ) is the probability of m i . In an entirely randomly generated image, the ideal value of GSE is 8. e closer the GSE of the image is to 8, the more random the image information. Local Shannon Entropy (LSE) is proposed by Wu [27] to measure the randomness of encrypted images. For image P, randomly select k nonoverlapping image blocks S 1 , S 2 ,. . ., S k and T B pixels, and LSE is defined as where H (S i ) is the Shannon entropy of the image block S i . is article selects (k, T B ) � (30, 1936) to test the cipher image. If the value of LSE is in the interval [h l * α left , h l * α right ], it means that the randomness is passed inspection; it can be considered that the cipher image has high randomness. Tested by encrypting the images in the USC-S IPI image database, the test results are shown in Table 4. It can be seen from Table 4 that after LSRS algorithm encryption, the GSE of the cipher image is very close to the ideal value, and most of the cipher images have passed the LSE critical value test. It can be considered that the generated cipher images have high randomness.

Antinoise Attack Analysis and Cropping Attack Analysis.
Digital images may lose data or be disturbed by noise due to various reasons during the transmission process. An effective image encryption method can reconstruct an identifiable decrypted image in the case of noise interference or data loss. Add 1%, 5%, and 10% salt and pepper noise to cipher image of Lena, and then decrypt it. As shown in Figure 9, even if a certain salt and pepper noise is added to the cipher image, the information of the decrypted image can still be identified. As shown in Figure 10, cipher images of Lena images are cropped 1/64, 1/16, and 1/4, respectively, and then decrypted, which can identify the information of the decrypted image, so the algorithm can resist the analysis of cropping attacks.

Efficiency Analysis.
e efficiency of image encryption is also one of the important indicators to measure the quality of image encryption methods. e main time-consuming parts of the LSRS algorithm are the iteration of the chaotic sequence, Latin square scrambling, Latin square replacement, and Latin square diffusion. In the simulation, take Lena as an example, we compared the encryption time of four different algorithms. Table 5 lists the running time of each algorithm, and the simulation result shows that the encryption efficiency of the LSRS algorithm is higher.   [20] −0.0226 0.0041 0.0368 Ref. [21] −0.0059 −0.0146 0.0211 Ref. [22] 0.0220 0.01792 7E−06

Conclusion
is paper proposes a novel chaotic image encryption algorithm based on Latin square and random shift. e LSRS algorithm adopts the structure of pixel scrambling-replacement-bit scrambling. e generation of Latin squares in the encryption process is related to the chaotic sequence, which improves the security of the entire encryption system. Since the histogram of the Latin square is evenly distributed, use the elements in the Latin square lookup table to replace pixels, and the algorithm can effectively resist differential attacks. Each cipher image corresponds to a Latin square lookup table, which increases the difficulty of the algorithm to decipher. e simulation results prove the safety and effectiveness of the LSRS algorithm.

Data Availability
e data used to support the findings of this study are included within the article.

Conflicts of Interest
e authors declare that they have no conflicts of interest.