1 Introduction

The remarkable growth in computational power has made the use of digital images over open networks more popular than ever before. However, this widespread use and the growth of the amount of data are also a great challenge for the security design of image encryption algorithms, especially for such private images as medical images [1, 2]. Traditional encryption methods such as the Data Encryption Standard, the International Data Encryption Algorithm (DES) [3], and the Advanced Encryption Standard (AES) [4] are not suitable for image encryption; those methods including chaotic encryption method perform poorly due to their low efficiency [5]. Therefore, improving the security and efficiency of image encryption algorithms has become a key issue.

It is known that a cellular automaton (CA) can be used to encrypt images efficiently [6,7,8,9,10,11], and using an image scrambling algorithm can make the entire encryption system safer and more reliable [12,13,14]. In computer science and mathematics, the Josephus problem is a theoretical problem related to a certain counting-out game [15]. The scrambling algorithm becomes an important part of the entire encryption system if the particular periodic characteristics of the algorithm are based on a Josephus ring [16,17,18].

The proposed algorithm is a row and column scrambling method based on transformation sequences. A plain image’s row and column configuration is called the original sequence, and new sequences are generated by particular times of a Josephus cycle. The following is a brief introduction to the Josephus ring and Josephus problem.

The Josephus ring is a classic problem in mathematics: A known number of individuals (designated 1, 2, 3, ..., n) sit around a round table. One by one, players call out numbers in increments of 1 beginning at the one whose serial number is m0. The number X is the step number in the game, when someone’s number is X they leave the table. The next player then counts from 1 again; this rule is repeated until only one player remains. The problem is to determine in advance what the serial number of the last player will be in a sequence of length n. Usually, we assign a number from 0 to n − 1 as a solution to such a problem. For a digital image, the final column number is f(n) + 1: it is also the original problem’s solution:

$$ {\displaystyle \begin{array}{l}f(1)=0\\ {}f(n)=\left[f\left(n-1\right)+ step\right]\kern0.5em \operatorname{mod}\kern0.5em n\end{array}} $$
(1)

2 Periodic characteristics of Josephus ring

2.1 Experimental methods

The Josephus problem has been solved by the recursion method, but the focus of this study is the periodic characteristics of the Josephus ring. We propose a new image scrambling algorithm with the following characteristics:

m0 starting position

step interval step

n length of the original sequence

A n original sequence

Josephus displacement: a out  = f(m0, step, A n ). This operation chooses an element from the original sequence as output, represented as a out . In Eq. (2), A n 'is the state of the original sequence after a Josephus replacement operation and m' is the starting position for the next Josephus replacement:

$$ {\displaystyle \begin{array}{l}{a}_{out}={a}_{m_0+\left( step-1\right)}\\ {}{A_n}^{\hbox{'}}=\left({a}_1,{a}_2,.\dots {a}_{out-1},{a}_{out+1},\dots {a}_{n-1}\right)\\ {}{m}^{\hbox{'}}=\mathrm{out}+1\end{array}} $$
(2)

Josephus cycle: First, set the length of the original sequence as n. Then multiply n by the Josephus displacement in the original sequence and place each a out in order. The new sequence B n emerges:

$$ {B}_n=\left({a}_{out_1},{a}_{out_2},{a}_{out_3}\dots {a}_{out_n}\right) $$
(3)

\( {B}_{n_j} \): A new sequence generated by j times the Josephus cycle from the original sequence. It is commonly used in the sections of this paper that discuss the periodicity of the Josephus problem.

At first, the original sequence takes the Josephus replacement (F), and every replacement generates an element. Put each element in order until all the elements are out. This obtains a new sequence. This operation is called one Josephus cycle, denoted t. Then determine the period of the Josephus cycle when m0 and step are given. Also, determine the change of t when m0 = 1 and step = 1, 2, 3,. .. . Then, explore whether the change of t is periodic with the change of step. If so, call it the “period step,” or the period of the Josephus rule, denoted as T. Finally, this paper discusses a scrambling algorithm based on a ring structure, for which the location of the starting point can be arbitrary. Here, for the sake of narration and argumentation, the starting point is m0 = 1. When m0 takes other values, its nature and the general rules described in the article do not change.

To facilitate the presentation, we define a new operation as a Josephus cycle, which can also be regarded as a kind of computation. Apparently, the Josephus cycle is composed of N times the Josephus displacement, so the Josephus ring in this problem contains only one regular operation: Josephus displacement f. Therefore, the problem belongs to the category of abstract algebra group theory: in group theory the analysis method is reasonable.

Meanwhile, to observe the periodic characteristics of the Josephus ring, we use natural numbers 1, 2, 3, ... to represent the original sequence; these numbers are only the element subscript number that shows the position information. They do not represent the content of the element as do other new sequences transformed from the original sequence.

Figure 1 is a schematic diagram of the entire encryption system. As shown in the figure, the Josephus scrambling algorithm is a pretreatment element for entering the encryption algorithm system.

Fig. 1
figure 1

Overall structure of the encryption algorithm

2.2 The period of the Josephus cycle

The period of the Josephus cycle is represented by t. To determine the period of each Josephus cycle, we need to multiply j by the Josephus cycle on the original sequence (A n ).

If the following equation is established:

$$ {A}_n={B}_{n_j} $$
(4)

then we have

$$ t=j $$
(5)

The period of this Josephus cycle is the value of j.

Experiment 1: When n = 4, m0 = 1, and step = 2, the Josephus cycle states are shown in Table 1.

Table 1 Josephus cycle states in experiment 1

For \( {A}_n={B}_{n_3} \), the period of the Josephus cycle is 3.

In Table 1, element 1 corresponds to element 2, element 2 corresponds to element 4, element 4 corresponds to element 1, and element 3 corresponds to itself.

So under these n, m0, and step values, n is countable: this Josephus cycle has two cycle sets,

{1,2,4} and {3}.

Experiment 2: When n = 20, m0 = 1, and step = 3, each Josephus cycle state is shown in Table 2.

Table 2 Each Josephus cycle state of experiment 2

It can be found in Table 2 that \( {A}_n={B}_{n_{30}} \), so the period of this Josephus cycle is 30.

Table 2 shows that in the same way as experiment 1, this Josephus cycle has six cycle sets:

{1,3,9,10,14,17,8,5,15,7}, {2,6,18}, {4,12}, {19,13,11}, {20}, and {16}.

By observing many results with different n, m0, and step values, a calculation method for the Josephus cycle’s period can be found. To use this method based on experiment 2 (n = 20, m0 = 1, and step = 3):

{1,3,9,10,14,17,8,5,15,7}: total number of elements is 10; {2,6,18}: total number of elements is 3; {4,12}: total number of elements is 2; {19,13,11}: total number of elements is 3; {20}: total number of elements is 1; {16}: total number of elements is 1.

Record each different total number of elements in each cycle set: A = {10,3,2,1}. The minimum common multiple of the elements in the A set is calculated, that is, the period of the Josephus cycle under these parameter values.

The least common multiple of 10,3,2,1 is 30, so t = 30.

This period calculation method is consistent with some properties of the n-order symmetric group S n and its r-cycles in abstract algebra [19], so this problem can be analyzed from the point of view of abstract algebra, especially the part of the symmetric group and its r-cycles.

This n degree symmetric group can be represented in r-cycles formed as 10-cycles {1,3,9,10,14,17,8,5,15,7}; 3-cycle, {2,6,18}; 2-cycle {4,12}; 3-cycle {19,13,11}; 1-cycle {20}; and 1-cycle {16}.

With the three parameters n, m0, and step determined, calculating the period of this Josephus cycle is equivalent to determining the order of one certain permutation group of this n-degree symmetric group, which is equal to the order of the product of all its disjoint r-cycles.

In experiment 2, all the r-cycles are disjoint. Obviously, each r-cycle’s order equals r. Furthermore, the r-cycles of S n have this property: the order of the product of disjoint r-cycles equals the minimum common multiple of each factor’s order.

So in summary, the order of this certain permutation group equals the order of {1,3,9,10,14,17,8,5,15,7}{2,6,18}{4,12}{19,13,11}{20}{16}. Then, determining the lowest common multiple of all the factor’s orders, the LCM of 10,3,2,3,1,1 is 30.

The results of this calculation coincide with the calculated results obtained by the observation, and the theorems and properties quoted in this analysis are well proved mathematically. Therefore, when the parameter values of n, m0, and step are fixed, the periodic problem of the Josephus cycle is effectively a problem of solving the order of a specific n permutation group of the certain n order symmetric group S n .

2.3 The period of the Josephus rule

Josephus rule: For each of the original sequences, certain n, m0, and step values correspond to a certain period of the Josephus cycle. For simplicity, we use J(n, m0, step) to represent the Josephus rule. Figure 2 shows the structural levels among the Josephus displacement, the Josephus cycle, the Josephus rule, and the final scrambling algorithm.

Fig. 2
figure 2

Hierarchy diagram of the operations

2.3.1 Periodic analysis of the step parameter

We find that a change in step changes the Josephus cycle. Does this change also show periodicity? That is to say, whether there is a T to make Eq. (6):

$$ J\left(n,{m}_0, step\right)=J\left(n,{m}_0, step+T\right) $$
(6)

To analyze the effect of the step value in the Josephus rule, keep parameter settings like m0 unchanged (the value of m0 can be any positive integer); for example, m0 = 1, step = 1, 2, 3,..., and then observe changes in the Josephus cycle and the Josephus rule. We may pay more attention to the Josephus rule’s periodic characteristics caused by step’s change.

After the parameters n, m0, and step are determined, according Eq. (2), the subscripts of a out (the value of out) are determined after it. Also, the launch of the new series \( {B}_{n_1} \) to \( {B}_{n_j} \) is determined, so we use \( {B}_{n_1} \) obtained by one Josephus cycle to represent all the states of that Josephus cycle. Because for \( {B}_{n_1} \) to \( {B}_{n_j} \), they all correspond to the same Josephus rule.

Experiment 3: m0 = 1, n = 4, and the original sequence is A n  = (1, 2, 3, 4):

As the value of step increases from 1, \( {B}_{n_1} \) changes with it. According to Table 3, a different \( {B}_{n_1} \) means a different Josephus rule. Record the value of step when J(n, m0, step) is consistent with J(n, m0, 1), and then we obtain T = step − 1 (T is the period of step).

In experiment 3, when step = 13, \( {B}_{n_1} \)returns to the original state, as the first line shows (step = 1). That means the value of step has gone through a cycle, and it is easy to see that the period of step’s cycle is 12:

Table 3 Josephus cycle state of each step value in experiment 3
$$ {\displaystyle \begin{array}{l}J\left(n,{m}_0,13\right)=J\left(n,{m}_0,1\right)\\ {}T=12\end{array}} $$
(7)

Experiment 4: m0 = 1, n = 7, and the original sequence is A n  = (1, 2, 3, 4, 5, 6, 7) (Table 4).

Table 4 Josephus cycle state of each step value in experiment 4

Experiments 3 and 4 show a change in the step value, which leads to a periodic change of \( {B}_{n_1} \), representing the J(n, m0, step):

$$ J\left(n,{m}_0, step\right)=J\left(n,{m}_0, step+T\right) $$
(8)

When we explore the general method of computing the value of T, we notice that there is some relation between that value and the sequence length n. Therefore, we set up a sequence of length N, redefine its step’s cycle T as T n , and then do the following analysis:

The T4 = 12 of experiment 3 and the T7 = 420 of experiment 4 are solved by the programming method of cyclic traversal. The periods of the step’s cycle T1 to approximately T11 and of n = 1 to approximately n = 11’s sequence are solved by the same method. The results are as follows:

$$ {\displaystyle \begin{array}{l}n=1\\ {}n=2\\ {}n=3\\ {}n=4\\ {}n=5\\ {}n=6\\ {}n=7\\ {}n=8\\ {}n=9\\ {}n=10\\ {}n=11\end{array}}\kern0.5em {\displaystyle \begin{array}{l}1!=1\kern1em {T}_1=1\\ {}2!=2\times 1\kern1em {T}_2=2\\ {}3!=3\times 2\times 1\kern1em {T}_3=6\\ {}4!=4\times 3\times 2\times 1\kern1em {T}_4=12\\ {}5!=5\times 4\times 3\times 2\times 1\kern1.5em {T}_5=60\\ {}6!=6\times 5\times 4\times 3\times 2\times 1\kern1.5em {T}_6=60\\ {}7!=7\times 6\times 5\times 4\times 3\times 2\times 1\kern1.5em {T}_7=420\\ {}8!=8\times 7\times 6\times 5\times 4\times 3\times 2\times 1\kern1.5em {T}_8=840\\ {}9!=9\times 8\times 7\times 6\times 5\times 4\times 3\times 2\times 1\kern1.5em {T}_9=2520\\ {}10!=10\times 9\times 8\times 7\times 6\times 5\times 4\times 3\times 2\times 1\kern1.5em {T}_{10}=2520\\ {}11!=11\times 10\times 9\times 8\times 7\times 6\times 5\times 4\times 3\times 2\times 1\kern1.5em {T}_{11}=27720\end{array}} $$
(9)

First, express each N’s factorial in the form of n !  = n × (n − 1)! (n ≥ 1), and then change the form of every N from an integer to the product of several certain prime factors. The results are:

$$ {\displaystyle \begin{array}{l}n=1\\ {}n=2\\ {}n=3\\ {}n=4\\ {}n=5\\ {}n=6\\ {}n=7\\ {}n=8\\ {}n=9\\ {}n=10\\ {}n=11\end{array}}\kern0.5em {\displaystyle \begin{array}{l}1!=1\\ {}2!=2\times 1!\\ {}3!=3\times 2!\\ {}4!={2}^2\times 3!\\ {}5!=5\times 4!\\ {}6!=3\times 2\times 5!\\ {}7!=7\times 6!\\ {}8!={2}^3\times 7!\\ {}9!={3}^2\times 8!\\ {}10!=5\times 2\times 9!\\ {}11!=11\times 10!\end{array}}\kern0.5em {\displaystyle \begin{array}{l}{T}_1=1\\ {}{T}_2=2\\ {}{T}_3=6\\ {}{T}_4=12\\ {}{T}_5=60\\ {}{T}_6=60\\ {}{T}_7=420\\ {}{T}_8=840\\ {}{T}_9=2520\\ {}{T}_{10}=2520\\ {}{T}_{11}=27720\end{array}} $$
(10)

Now observe the relation between T n and Tn − 1:

$$ {\displaystyle \begin{array}{l}{T}_1=1\\ {}{T}_2={T}_1\times 2\\ {}{T}_3={T}_2\times 3\\ {}{T}_4={T}_3\times 2\\ {}{T}_5={T}_4\times 5\\ {}{T}_6={T}_5\times 1\end{array}}\kern0.5em {\displaystyle \begin{array}{l}{T}_7={T}_6\times 7\\ {}{T}_8={T}_7\times 2\\ {}{T}_9={T}_8\times 3\\ {}{T}_{10}={T}_9\times 1\\ {}{T}_{11}={T}_{10}\times 11\end{array}} $$
(11)

As shown in Eq. (6), n is the sequence length. First, we do the prime factor decomposition for n and obtain the prime factors x i , keeping x i  ≤ n. In the equation, p is the number of different qualitative factors and a i is the exponent of x i :

$$ n=\prod \limits_{i=1}^p{x}_i^{a_i} $$
(12)

The following inference is made from the above experimental data:

When p = 1, that is to say, n can be represented only as the power of a certain prime number, there are

$$ {T}_n={x}_i{T}_{n-1} $$
(13)

When p > 1, that is, n can be expressed as the product of the power of several prime factors, there are

$$ {T}_n={T}_{n-1} $$
(14)

2.3.2 Formula generalization of step’s period

In Section 3.1, we researched the period of step (T n ), and T n was found to have a direct relation with the sequence’s length. Step and m0 are two parallel parameters of the Josephus cycle, so it could be asked whether T n is also the period of m0, or whether T n is related only to the sequence’s length.

It has been found that, for a sequence of length N, step’s periodic characteristic is also suitable for m0. The research method is the same as what Section 3.1 shows. The difference is only that keeping the value of step unchanged, we increase m0 from 1 to n.

Therefore, we get

$$ J\left(n,{m}_0, step\right)=J\left(n,{m}_0+{T}_n, step\right) $$
(15)

With the conclusion of Section 3.1, we can get the following relation:

$$ J\left(n,{m}_0, step\right)=J\left(n,{m}_0+{T}_n, step+{T}_n\right) $$
(16)

In conclusion, T n is the period of both step and m0, and it is related only to the sequence’s length. Therefore, we propose that T n is the period of the Josephus rule.

2.3.3 Space of the Josephus rule

A different J(n, m0, step) means a different Josephus cycle: each Josephus cycle corresponds to a certain different Josephus rule. Now we discuss the question of how many different rules exist; the scrambling algorithm should have a very large space of rules. Because T n is the period of both step and m0 according to Section 3.2, we can easily find that the space of the Josephus rule is the square of T n . So we get

$$ space={T}_n^2 $$
(17)

3 Results and discussion

3.1 Number of pixels change rate and unified averaged changed intensity tests

Number of pixels change rate (NPCR) and the unified averaged changed intensity (UACI) are two important parameters for judging the strength of an encryption algorithm [20]. The values of NPCR and UACI are computed using the following equations:

$$ NPCR=\left(\frac{1}{NM}\sum \limits_{i=1}^N\sum \limits_{j=1}^MD\left[i,j\right]\right)\times 100\% $$
(18)
$$ UACI=\left(\frac{1}{NM}\sum \limits_{i=1}^N\sum \limits_{j=1}^M\frac{\left|{C}_1\left[i,j\right]-{C}_2\left[i,j\right]\right|}{255}\right)\times 100\% $$
(19)

where N and M denote the image’s width and height respectively. C1[i,j] is the obtained cipher image from the original plain image, whereas C2[i,j] is obtained after one bit of the plain image is modified. For each (i,j) position, if C1[i,j] = C2[i,j], then D[i,j] = 0, else D[i,j] = 1.

We incorporated the Josephus scrambling algorithm into the encryption algorithm system, and then tested the Josephus scrambling algorithm’s ability to enhance the effect of the partial X cell automaton encryption algorithm. We used the 256 × 256 cameraman as an example. In Fig. 3, the pictures from left to right are the plain image, the direct CA-encrypted image, the scrambled image, and the cipher image after scrambling. Figure 4 are gray histograms of above images. As we can see from Fig. 4, the cipher image’s pixel distribution is more uniform than plain image, which proves this encryption system can protect the information of plain image.

Fig. 3
figure 3

a Plain image (cameraman). b Directly CA-encrypted image (cameraman). c Scrambled image (cameraman). d The cipher image after scrambling the plain image (cameraman)

Fig. 4
figure 4

a Gray histograms of Lena. b Gray histograms of cipher Lena image

To analyze the security of the entire encryption algorithm intuitively [21,22,23,24,25], we took 1 × 104 pixel points of each experimental image as samples. Next, we should demonstrate that using the scrambling algorithm can improve the security of the entire encryption algorithm.

Figure 5 shows several standard images used as experimental materials. To prove the hypothesis above, we did NPCR and UACI tests on pairs of plain images, comparing each encrypted image before scrambling, with the same encrypted image after scrambling. We called them the sample image pairs.

Fig. 5
figure 5

a Cameraman. b Lena. c Couple. d Boats. e Baboon. f Town. g Barb. h Peppers

At the same time, we used the hypothesis testing method to calculate the p value of the NPCR test results. If the proposed algorithm was able to encrypt images that were indistinguishable from random images under the NPCR and UACI measures, the p values simply represented the possibility that our tested images were indeed random-like. Take the significance level α = 0.05, and then judge the randomness and credibility of the sample image pairs according to the sizes of the p value and α. When the p value is less than α, the sample image pair is strongly random-like, but when the p value is greater than or equal to α, the sample image pair has low randomness. Figure 6 shows the p value test results. Table 5 shows NPCR, UACI, and p value testing results of standard images. As shown in Fig. 7, in this encryption system, the NPCR value of the scrambled images is generally higher than that of the nonscrambled images. UACI results for both of the two series are similar: as Fig. 8 shows, the red line is just slightly higher than the blue line. It is a normal test result.

Fig. 6
figure 6

NPCR test

Table 5 NPCR and UACI p values
Fig. 7
figure 7

P values of NPCR tests

Fig. 8
figure 8

UACI test

Fig. 9
figure 9

a Vertical correlations of original images. b Horizontal correlations of original images. c Diagonal correlations of original images

Fig. 10
figure 10

a Vertical correlations of scrambled images. b Horizontal correlations of scrambled images. c Diagonal correlations of scrambled images

Fig. 11
figure 11

a Vertical correlations of scrambled and encrypted images. b Horizontal correlations of scrambled and encrypted images. c Diagonal correlations of scrambled and encrypted images

3.2 Confidence intervals

Due to the normal distribution of the cipher image samples, we get X ~ N(μ,σ2), X is the average value of NPCR, under significance level α = 0.05, and confidence degree is 1−α = 0.95.

Confidence intervals:

$$ \left[\overline{X}\pm \frac{\sigma }{\sqrt{n}}\times {Z}_{\frac{\alpha }{2}}\right] $$
(20)

Look-up the normal distribution table with the confidence degree of 0.95, we get:

$$ {Z}_{\frac{\alpha }{2}}=1.96 $$
(21)

So confidence intervals is [0.984347,1.008183]. Details are displayed in the (Table 6).

Table 6 Confidence intervals

3.3 Correlation detection

Correlation detection is a conventional detection method for encrypted images [26, 27]. It is also used to evaluate the performance of encryption or scrambling algorithms. Table 7 shows the details, and Figs. 9, 10, and 11 show the correlation detection results of the cameraman picture.

Table 7 Correlation detection results for the cameraman picture

4 Conclusions

In this study, starting from the classic Josephus problem we used the Josephus loop algorithm to generate scrambled sequences from the original sequences. These new sequences were then applied to the image scrambling of columns and rows. The scrambling operation is the preprocessing part of the entire encryption system, which has a great influence on the security of the encryption algorithm. Then we focused on the period of the Josephus cycle (t) and the period of the Josephus rule (T n ), both of which are the core of the Josephus scrambling algorithm and guarantee the reversibility and rule space complexity of that algorithm. Different selections of m0 and step also have a direct impact. To obtain a general conclusion, we encrypted experimental group images with the Josephus scrambling treatment and a control group of images without it. Both image groups used the cameraman, Lena, and other standard images. Then we did NPCR and UACI tests and correlation detection for two sets of samples. The experimental data show that compared with the control group, the images encrypted with Josephus scrambling had better randomness and higher NPCR values; the lower p value also shows the reliability of the experimental results. Therefore, we found that the Josephus scrambling algorithm can improve the randomness of image enciphering and the reliability of an encryption system.