# Design, Implementation, and Analysis of a Block Cipher Based on a Secure Chaotic Generator
## 2 Proposed Chaos-Based Cryptosystem
### Diffusion Process and Its Inverse
#### 2.3.2 Horizontal and Vertical Addition Diffusion HAD and VAD and Their Inverses  
The operations of horizontal and vertical addition diffusion [26] on a given permuted block \(sp_{l}(i,j)\) are described by the following mathematical model, and shown in Figure 5:  
\[ \begin{split} sph_{l}(i,j)=HAD[sp_{l}(i,j)]=\left\{\begin{array}{ ll}sp_{l}(i,j)+sp_{l}(i,j-1)\ mod\ 256&for\ 1\leq i\leq M\ and\ 1<j\leq M\\ sp_{l}(i,j)+sp_{l}(i-1,\ M)\ mod\ 256&for\ 1<i\leq M\ and\ j=1\\ sp_{l}(i,j)+sp_{l}(M,M)mod\ 256&for\ i=j=1\\ sph_{l}(i,j)+sph_{l}(i-1,j)\ mod\ 256&for\ 1<i\leq M\ and\ 1\leq j\leq M \\ sph_{l}(i,j)+sph_{l}(M,j-1)\ mod\ 256&for\ i=1\ and\ 1<j\leq M\\ sph_{l}(i,j)+sph_{l}(M,M)mod\ 256&for\ i=j=1\\ \end{array}\right.\end{split}  \tag{23}\]  
\[ \begin{split} c_{l}(i,j)=VAD[sph_{l}(i,j)]=\left\{\begin{array}{ ll}sp_{l}(i,j)+sp_{l}(i,j-1)\ mod\ 256&for\ 1<i\leq M\ and\ 1<j\leq M\\ sp_{l}(i,j)-c_{l}(M,j-1)\ mod\ 256&for\ i=1\ and\ 1<j\leq M\\ c_{l}(i,j)-c_{l}(M,M)\ mod\ 256&for\ i=j=1\\ \end{array}\right.\end{split}  \tag{24}\]  
The inverse relations of the vertical and horizontal addition diffusion are given by:  
\[ \begin{split} sph_{l}(i,j)=InvVAD[c_{l}(i,j)]=\left\{\begin{array}{ ll}c_{l}(i,j)-c_{l}(i-1,j)\ mod\ 256&for\ 1<i\leq M\ and\ 1\leq j\leq M\\ c_{l}(i,j)-c_{l}(M,j-1)\ mod\ 256&for\ i=1\ and\ 1<j\leq M\\ c_{l}(i,j)-c_{l}(M,M)\ mod\ 256&for\ i=j=1\\ \end{array}\right.\end{split}  \tag{25}\]  
\[ \begin{split} sp_{l}(i,j)=InvHAD[sph_{l}(i,j)]=\left\{\begin{array}{ ll}sp_{l}(i,j)-sph_{l}(i,j-1)\ mod\ 256&for\ 1\leq i\leq M\ and\ 1<j\leq M\\ sp_{l}(i,j)-sph_{l}(i-1,M)\ mod\ 256&for\ 1<i\leq M\ and\ j=1\ \ and\ j=1\\ sp_{l}(i,j)-sph_{l}(M,M)mod\ 256&for\ i=j=1\\ \end{array}\right.\end{split}  \tag{26}\]  
Finally, we summarize in Algorithms 1 and 2, the full operation of encryption and decryption process of the proposed chaos-based cryptosystem, respectively. Figure 5: Horizontal and vertical addition diffusion. ```
Input :\(IV=\) Initial Vector of \(PRNG-CS\) ; \(K=\) Secret key of the \(PRNG-CS\); \(ivb=\) Initialization vector used in the first block; \(b_{l}=\) Number of blocks; \(p_{l}=\) Plaintext block; \(PRNG-CS(IV,K)=(Kc,Kd)\). Output :The block under test is encrypted. 1 begin
2 Generate the needed dynamic keys \(|Kc|\) to construct the S-box and \(|Kd|\) to perform the permutation;  Produce the initial vector \(ivb\) with a prng to encrypt the first block and produce \(h,m_{h}\) values (with a prng);  Construct the S-box using the four steps of Figure 4; for\(m=1\) to \(r\)do
3 for\(l=1\) to \(b_{l}\)do
4 Substitution process for\(k=1\) to \(|p_{l}|\)do
5 Calculate \(y_{l}(k)\) by Equations (15) and (16);  Perform the substitution process using Equation (14) to obtain \(s_{l}(k)\);
6
7 end for
8 Permutation process for the diffusion \(M=\sqrt{|p_{l}|}\); for\(i=1\) to \(M\)do
9 for\(j=1\) to \(M\)do
10 Calculate \(k=(i-1)\times M+j\);  Perform the permutation of pixels: \(sp_{l}(i_{n},j_{n})=s_{l}(i,j)\);
11
12 end for
13
14 end for
15
16 end for
17 VAD diffusion for\(i=1\) to \(M\)do
18 for\(j=1\) to \(M\)do
19 Calculate vertical diffusion using Equation (24);
20
21 end for
22
23 end for
24
25 end for
26
27 end for
28
29 end for
30
31 end for
```  
**Algorithm 1** Encryption process. ```
Input :\(IV\) = Initial Vector of \(PRNG-CS\); \(K\) = Secret key of the \(PRNG-CS\); \(ivb\) = Initialization vector used in the first block; \(b_{l}\) = Number of blocks; \(p_{l}\) = Plaintext block; \(PRNG-CS(IV,K)=(Kc,Kd)\). Output : The block under test is decrypted.