1 Introduction

With the rapid development of the Internet and multimedia technologies, people are paying more and more attention to information security issues. Among them, images, as carriers of information security, have high requirements for security and confidentiality. Although traditional encryption algorithms (such as DES and RSA) can be used for image encryption, they have large capacity, high redundancy, and high pixel correlation, which are not suitable for image encryption. Therefore, a good image encryption algorithm is essential. Good image encryption schemes have been proposed, for example, DNA sequences [6, 13, 29], coupled map lattices [8, 15, 32], S-boxes [3, 7, 36], wavelet transforms [4], chaotic systems and the like [2, 5, 22, 28]. Among them, chaos plays an important role. Chaos is a theoretical system that is very sensitive to the initial state, and has a high degree of randomness and mixing. It is the most widely used system for image encryption.

In recent years, more and more chaotic systems have been used for image encryption, and some chaotic-based image encryption algorithms have also been well developed. For example, based on DNA sequences [23, 34, 35], based on a hybrid chaotic map or a high-dimensional chaotic system [24, 25, 31], based on Fisher-Yates [26], and image encryption algorithm based on bit-level permutation [11, 14, 16]. Compared with pixel-level permutation, bit-level permutation not only changes the position of the pixel, but also changes the value of the pixel, which has better encryption effect. At present, more and more bit-level-based encryption schemes have been proposed. For example, Zhou et al. proposed Bit-level quantum color image encryption scheme [37], Xu et al. proposed bit-level image encryption based on chaotic mapping [30], Zhang et al. proposed a bit-level image encryption algorithm based on rotation matrix and block diffusion [33].

In recent years, neural network has been widely integrated into image encryption, and its distributed parallel information processing has improved the efficiency of image encryption. For example, Wang et al. proposed a color image encryption algorithm based on hopfield chaotic neural network [20], Ratnavelu et al. proposed image encryption method based on chaotic fuzzy cellular neural networks [12], Wei et al. proposed stability of stochastic impulsive reaction-diffusion neural networks with S-type distributed delays and its application to image encryption [27], Ahmad et al. proposed cryptanalysis of an image encryption algorithm based on PWLCM and inertial delay neural network [1]. and Wang et al. proposed schemes of applying boolean networks to image encryption [17, 18]. Gray code is a kind of reliability coding of binary conversion. It will be a good combination to apply it synchronously with neural network in image encryption.

This paper proposes a bit-level image encryption algorithm based on BP neural network and Gray code. In the permutation, the Gray code is used for the bit-level permutation operation. In the diffusion, the bit-level reverse-order output is first adopted, and then the chaotic map is outputted through the BP neural network model for the divergence xor operation. After a lot of experimental tests, the algorithm can be used for image encryption and it works well.

The rest of the paper is arranged as follows. The second chapter introduces the chaotic system and replacement methods used in this paper. The third chapter introduces the process description of the encryption algorithm. The fourth chapter shows the simulation results of image encryption. The fifth chapter has carried out a large number of experimental tests on the algorithm. The sixth chapter is the conclusion of this paper.

2 Related method

2.1 System and chaotic map

2.1.1 Hyperchaotic Lorenz system

The Lorenz system is a hyperchaotic system with a positive Lyapunov exponent [21]. The dynamic behavior of the system is difficult to predict. The dynamic equation is:

$$ \left\{\begin{array}{c}x=a\left(y-x\right)+w\\ {}y= cx-y- xz\\ {}\begin{array}{c}z= xy- bz\\ {}w=- yz+ rw\end{array}\end{array}\right.. $$
(1)

When a = 10, b = 8/3, c = 28, and −1.52 < r ≤  − 0.06, the hyperchaotic Lorenz system has a positive Lyapunov exponent in a hyperchaotic state. The resulting sequence is aperiodic, non-convergent and very sensitive to initial values.

2.1.2 Logistic map

The Logistic map is a one-dimensional chaotic system [10], whose equation is:

$$ {x}_{n+1}={ux}_n\left(1-{x}_n\right). $$
(2)

Among them xn ∈ (0, 1), when the parameters are μ ∈ (3.5699456, 4], the Logistic map is chaotic, and the generated sequence {xn, n = 0, 1, 2, 3…} is aperiodic, non-converged, and very sensitive to the initial value. In order to avoid the periodic window, this paper uses the parameters in the range of u ∈ (3.89,4].

2.1.3 PWLCM map

The PWLCM map is a map composed of multiple linear segments [19], which has a more balanced nature than the Logistic map. The dynamic equation is:

$$ {x}_{n+1}=F\left({x}_n\right)=\left\{\begin{array}{c}{x}_n/p,0\le {x}_n<p\\ {}\left({x}_n-p\right)/0.5-p,p\le {x}_n<0.5\\ {}F\left[1-{x}_n\right],0.5\le {x}_n<1\end{array}\right.. $$
(3)

The control parameters P ∈ (0,0.5), xn and xn + 1, are the input and output states of the chaotic map during system iteration, both of which are within the interval of (0.1). In this paper, the control parameters are limited to (0.2,0.3) and have better chaotic characteristics than the entire control range.

2.2 BP neural network

The Back Propagation [9], proposed by the team of scientists led by Rumelhart and McCelland in 1986, is a multi-layer feedforward network trained by error inverse propagation algorithm and is one of the most widely.

used neural network models. The BP neural network consists of three parts: input layer, hidden layer and output layer. In this paper, the pixels of the plaintext image are processed and used as the input of the neural network,

while the hidden layer is placed into Eqs. (2) and (3) as the transfer function. Finally, the output layer outputs a.

set of random sequences as the key stream of the encryption process. Its BP neural network model is shown in Fig. 1.

Fig. 1
figure 1

BP neural network

2.3 Bit level image

In grayscale images, the brightness between black and white is quantized to an integer number of levels between 0 and 255. Each pixel value can be converted to an 8-bit binary. The plaintext image can be divided into 8 bit-level images according to the binary position. The position of the bit value is different, and the amount of information stored is different. The high 4-bit image stores 94.125% of the plaintext information, and the other 6% of the information is stored in the lower 4-bit image. We selected the “Lena” image to test the 8-bit-level image. The 8 bit-level images of Lena image be shown in Fig. 2.

Fig. 2
figure 2

The 8 bit-level images of Lena image (a) Bit plane 1 (b) Bit plane 2 (c) Bit plane 3 (d) Bit plane 4 (e) Bit plane 5 (f) Bit plane 6 (g) Bit plane 7 (h) Bit plane 8

2.4 Gray code

Gray code, also known as cyclic binary code or reflection binary code, it is a coding method that we often encounter in engineering. Its basic feature is that any two adjacent codes have only one binary number. This paper takes the conversion between binary and Gray code. The binary code is converted into Gray code, the rule is to reserve the highest bit of the binary code as the highest bit of the Gray code, and the second highest bit Gray code is obtained by xoring the high and the second highest bits of the binary code. The Gray code is converted into a binary code, the rule is to reserve the highest bit of the Gray code as the highest bit of the natural binary code, and the second highest natural binary code is obtained by xoring the high natural binary code with the next highest order Gray code. Table 1 shows the conversion relationship between Gray code and binary code.

Table 1 The conversion relationship between Gray code and binary code

3 Encryption process description

3.1 High 4-bit image permutation

Due to the high 4-bit image stores most of the plaintext information, the high 4-bit image is first replaced.

  1. Step 1:

    The plaintext image P of M × N is converted into a binary image according to Eq. (4), and the high 4-bit and the lower 4-bit are separated according to Eq. (5), the high 4-bit image is A1, and the lower 4-bit image is A2.

$$ P= de2 bi(P). $$
(4)
$$ {A}_1=P\left(:,1:4\right),{A}_2=P\left(:,5:8\right). $$
(5)
  1. Step 2:

    The sum of all the values of the high 4-bit matrix is obtained according to the Eq. (6), and the sum is processed according to the Eq. (7) to generate the variable a as the input value of the BP neural network.

$$ sum= sum\left({A}_1\left(:\right)\right). $$
(6)
$$ a=\mathit{\operatorname{mod}}\left(\left(1/ sum\right),1\right). $$
(7)
  1. Step 3:

    Performing a Gray code permutation operation on the rows and columns of the high 4-bit image according to Eqs. (8) and (9) to generate a high 4-bit change matrix B1.

$$ {B}_1\left(:,\mathrm{j}\right)= xor\left({A}_1\left(:,\mathrm{j}\right),{A}_1\left(:,\mathrm{j}-1\right)\right). $$
(8)
$$ {B}_1\left(i,:\right)= xor\left({B}_1\left(i,:\right),{B}_1\left(i-1,:\right)\right). $$
(9)
  1. Step 4:

    The high 4-bit change matrix B1 and the low 4-bit matrix A2 are combined into the matrix B according to the Eq. (10).

$$ B=\left[{B}_1,{A}_2\right]. $$
(10)

3.2 Global permutation

Due to the Gray code permutation has the defect that the first value does not change, and the lower 4-bit image still stores 6% of the plaintext information, this paper chooses to perform a global permutation on the matrix B.

  1. Step 1:

    First, the Lorentz system is iterated M × N + 500 times into a set of chaotic sequence AA according to Eq. (1), and the first 500 chaotic values are discarded to avoid transient effects. Due to the chaotic sequence generated by the Lorentz system have minus, then the chaotic sequence is normalized to the range of (0, 1), and the index matrix I is generated according to the Eq. (11).

$$ \left[S,I\right]= sort(AA). $$
(11)
  1. Step 2:

    The matrix B is globally permuted according to Eq. (12), and a permutation matrix B2 is generated.

$$ {B}_2\left(j,i\right)=B\left(I\left(i,j\right),i\right). $$
(12)
  1. Step 3:

    The permutation matrix B2 is converted into a decimal matrix according to a Eq. (13).

$$ {B}_2= bi2 de\left({B}_2\right). $$
(13)

3.3 Bit-level diffusion

  1. Step 1:

    The decimal matrix B2 is converted into a bit-level matrix G according to Eq. (14).

$$ G= dec2 bin\left({B}_2\right). $$
(14)
  1. Step 2:

    Each bit of the bit-level matrix G is output in reverse order according to Eq. (15) to generate a diffusion matrix G1, and then the bit-level matrix is converted into a decimal matrix according to Eq. (16).

$$ {G}_1=G\left(:,8:-1:1\right). $$
(15)
$$ {G}_1= bin2 dec\left({G}_1\right). $$
(16)
  1. Step 3:

    The input value a is iterated M × N + 500 times by Eq. (2) to generate a set of sequence, and the first 500 chaotic values are discarded to avoid transient effects. The generated sequence is passed by the second hidden layer according to Eq. (3) to generate a set of M × N sequences X.

  2. Step 4:

    The X is processed according to Eq. (17) to generate a key stream K required for diffusion.

$$ K= floor\left(\operatorname{mod}\left(X\times {10}^{16}\right),256\right). $$
(17)

Step 5: Due to the bit reverse order diffusion operation cannot change the pixel with the value of 255, a round of diffusion operation is performed in this paper, and the diffusion matrix G1 and the key stream K are xored according to the Eq. (18) to generate the ciphertext matrix C.

$$ C= bitxor\left({G}_1,K\right). $$
(18)

Since the decryption process is the inverse of encryption, it will not be described here.

4 Simulation results and performance analysis

4.1 Simulation results

This paper uses Matlab 2017 to encrypt five plaintext images of “Lena”, “Peppers”, “Cameraman”, “Girl” and “Finger”. Figure 3 shows the effect of encryption and decryption of three images. It can be seen from Fig. 3 that the encrypted image can not see any information of the original image at all, so visually, the algorithm has achieved a better encryption effect.

Fig. 3
figure 3

Encryption and decryption of gray-scale image (a) Plaintext Lena (b) Plaintext Pepper (c) Plaintext Cameraman (d) Plaintext Girl (e) Plaintext Finger (f) Ciphertext of Lena (g) Ciphertext of Pepper (h) Ciphertext of Cameraman (i) Ciphertext of Girl (j) Ciphertext of Finger (k) Decryption of Lena (l) Decryption of Pepper (m) Decryption of Cameraman (n) Decryption of Girl (o) Decryption of Finger

4.2 Key space analysis

In this paper, the non-integer key precision can reach 10−14, and the key space can be greater than 2100, which can achieve theoretical non-violent cracking. In addition, this paper also conducted related experiments on the sensitivity of the key. The experiment uses the original key to encrypt the plaintext to obtain the ciphertext image, and decrypts it using the correct key and the slightly changed error key. Figure 4 shows the test results. In Fig. 4a, the correct key is used for decryption, and the resulting image is identical to the original plaintext image. In Fig. 4b, the key is decrypted using a slightly changed key, and the resulting image is completely different from the original plaintext. As can be seen from the decryption results, even if the key has a very small change, the result obtained by the algorithm decryption is completely different. Therefore, the algorithm has high sensitivity to the initial key and has good security against statistical analysis attacks.

Fig. 4
figure 4

Sensitivity of secret key (a) Correct key restoring diagram (b) Error key restoring diagram

4.3 Statistical analysis

Statistical analysis is to use statistical methods to verify the encryption effect of images. The statistical methods used in this paper include histogram analysis and correlation analysis.

4.3.1 Histogram analysis

The histogram is a graph reflecting the frequency of the pixel value information in the image. When the histogram of the encrypted image shows a state in which all the pixel values are uniform, the encryption effect is good and the statistical analysis can be resisted. Figure 5 shows the plaintext and ciphertext histograms for the five images “Lena”, “Peppers”, “Cameraman”, “Girl” and “Finger”. It can be seen from Fig. 6 that all the encrypted histograms of five images selected in this paper are in a straight line state, that is, the pixel value information of the encrypted image is uniformly distributed, and the algorithm achieves a better encryption effect.

Fig. 5
figure 5

Histograms of plain images and ciphered images (a) Histogram of Lena (b) Histogram of Peppers (c) Histogram of Cameraman (d) Histogram of Girl (e) Histogram of Finger (f) ciphered Lena (g) ciphered Peppers (h) ciphered Cameraman (i) ciphered Girl (j) ciphered Finger

Fig. 6
figure 6

Correlation coefficients of Lena (a) Horizontal correlation of plain image (b) Vertical correlation of plain image (c) Diagonal correlation of plain image (d) Horizontal correlation of ciphered image (e) Horizontal correlation of ciphered image (f) Diagonal correlation of ciphered image

4.3.2 Correlation analysis

In the plaintext image, adjacent pixels have a high correlation. To avoid image information being attacked, the correlation between adjacent pixels should be reduced. When the correlation coefficient is close to zero, the algorithm can resist statistical analysis. Pixel correlation analysis includes three directions, vertical, horizontal and diagonal.

$$ rxy=\frac{\operatorname{cov}\left(x,y\right)}{\sqrt{D(x)}\sqrt{D(y)}} $$
(19)

among them.

$$ \operatorname{cov}\left(x,y\right)=\frac{1}{N}\sum \limits_{i=1}^N\left({x}_i-E(x)\right)\left({y}_i-E(y)\right),D(x)=\frac{1}{N}\sum \limits_{i=1}^N{\left({x}_i-E(x)\right)}^2,E(x)=\frac{1}{N}\sum \limits_{i=1}^N{x}_i. $$

x, y are the gray value of two adjacent pixels. This paper selects 2000 pairs of pixels according to the Eq. (19) to test the “Lena”, “Peppers”, “Cameraman” image of the plaintext image and the ciphertext image horizontal, vertical, diagonal direction correlation, as shown in Figs. 6, 7, 8. It can be seen from the ciphertext correlation in Figs. 6, 7, 8 that the pixel values of ciphertext images are uniformly distributed in both horizontal, vertical and diagonal directions.

Fig. 7
figure 7

Correlation coefficients of Peppers (a) Horizontal correlation of plain image (b) Vertical correlation of plain image (c) Diagonal correlation of plain image

Fig. 8
figure 8

Correlation coefficients of Cameraman (d) Horizontal correlation of ciphered image (e) Horizontal correlation of ciphered image (f) Diagonal correlation of ciphered image

Table 2 shows the correlation coefficients of the five plaintext images and the ciphertext images of “Lena”, “Peppers”, “Cameraman”, “Cirl” and “Finger”. Experiments show that the algorithm can resist statistical analysis when the correlation coefficient is close to zero. Compared with the Lena image in Ref. [3], Ref. [2], Ref. [1], we can see that there is no significant difference between the algorithm and the three documents. Therefore, It can be seen from the table that the algorithm is feasible.

Table 2 Correlation coefficients of images

4.4 Information entropy analysis

Information entropy reflects the degree of confusion of image pixels. When the information entropy is close to 8, it indicates that the pixel values in the image are disordered (Table 3).

$$ H(s)=\sum \limits_{i=0}^{2^L-1}p\left({s}_i\right){\mathit{\log}}_2\frac{1}{p\left({s}_i\right)}. $$
(20)
Table 3 Information entropy of images

Where p(si) represents the probability of si occurring. The information entropy of the “Lena”, “Peppers”, “Cameraman”, “Cirl” and “Finger” plaintext images and ciphertext images was tested according to Eq. (20), It can.

be seen from the table data that the entropy value of image information encrypted by this algorithm has reached 7.99, which is equivalent to infinite close to 8, which indicates that the pixel value has reached a good degree of chaos. Compared with the Lean image in Ref. [3], Ref. [2] and Ref. [1], the data in the table further verify the validity of the algorithm.

4.5 Differential attack analysis

The differential attack is to make a slight change to a pixel value in the plaintext image, and then encrypt it with the encryption scheme of this paper. The differential attack includes NPCR and UACI. NPCR represents the rate of change in the number of pixels of the image, and UACI represents the average intensity of the difference between the normal image and the encrypted image. When the NPCR reaches 99.6% and the UACI reaches 33.4%, it can resist differential attacks (Table 4).

Table 4 The average of NPCR and UACI and comparison with other algorithms
$$ NPCR=\frac{\sum_{i,j}D\left(i,j\right)}{W\times H}\times 100. $$
(21)
$$ UACI=\frac{1}{W\times H}=\left[\sum \limits_{i,j}\frac{\left|{c}_1\left(i,j\right)-{c}_2\left(i,j\right)\right|}{255}\right]\times 100. $$
(22)

Where W and H represent the width and height of the image, respectively, c1 and c2 are the two ciphertext images after the original plaintext image changes by one pixel value. If c1(i, j) ≠ c2(i, j), then D(i, j) = 1, otherwise D(i, j) = 0. According to Eqs. (21) and (22), we tested NPCR and UACI of five grayscale images of “Lena”, “Peppers”, “Cameraman”, “Cirl” and “Finger”, The data in the table show that the NPCR and UACI of the encrypted image reach the standard value. And compared with the lena image in Ref. [3], Ref. [2] and Ref. [1], both data can effectively verify that the algorithm is feasible.

4.6 Cropping attack and noise attack analysis

Images are easily hijacked during communication, and once the information in the image has been tampered with, it will be irreparable. To prevent this, the ciphertext should have good resistance to cropping attacks. Test ciphertext’s anti-cutting attack capabilities, cropping attacks and noise attacks are the two most common methods. In this paper, the ciphertext image is cut by 1/2, 1/4, 1/8 information to test the robustness of the encryption system. This paper tested the cropping and noise attacks of the “Lena” image. Figures 9 and 10 show the clipping and noise attacks of the “Lena” image respectively. Whether clipping or noise attacks on ciphertext images, the algorithm can still obtain plaintext information after decryption of ciphertext images. It is effective to show that the algorithm is feasible.

Fig. 9
figure 9

Cropping attack analysis (a) Full encryption 1/8 block attack (b) Full encryption 1/4 block attack (c) Full encryption 1/2 block attack (d) Decryption image of (a) (e) Decryption image of (b) (f) Decryption image of (c)

Fig. 10
figure 10

Noise attack analysis (g) Decryption image of 0.01 salt & pepper (h) Decryption image of 0.05 salt & pepper

5 In conclusion

This paper proposes a bit-level image encryption algorithm based on BP neural network and Gray code. In the permutation, the Gray code is used for the bit-level permutation operation. In the diffusion, the bit-level reverse-order output is first adopted, and then the chaotic map is outputted through the BP neural network model for the divergence xor operation. After a lot of experimental tests, the algorithm can be used for image encryption and it works well.