# Hash Based DNA Computing Algorithm for Image Encryption
## 3 Methods
### Design of DNA Hash Encoding Module
```
Input: Initial Key \(K\), Plain Image \(I\) Output: Rule Selection Sequence \(Q_{rule}\), DNA encoded Sequence \(Q_{1}\)
1\(H,W=\) original height and original width of \(I\);
2 Randomly generate \(H^{\prime}\) and \(W^{\prime}\), where \(H<H^{\prime}<2\times H\) and \(W<W^{\prime}<2\times W\);
3 Convert the integer numbers \(H^{\prime}\) and \(W^{\prime}\) into a 16 bit binary bitstream \(HW_{bit}\) ;
4 Convert the plain image \(I\) into a binary bitstream \(I_{bit}\);
5 Randomly generate a binary bitarray \(R_{bit}\) whose length is \(H^{\prime}\times W^{\prime}\times 8-H\times W\times 24-16\);
6\(I_{bit}=HW_{bit}\oplus I_{bit}\oplus R_{bit}\) (where \(\oplus\) indicates concatenate operation);
7\(length=\) Length(\(I_{bit}\));
8\(HASH_{K}=\) SHA256(K);
9\(H_{1}=HASH_{K}(0:63)\);
10\(H_{2}=HASH_{K}(64:127)\);
11\(x_{1}=modf\big{(}H_{1}/10^{15}\big{)}\);
12\(p=modf\big{(}H_{2}/10^{15}\big{)}/2\);
13 For(t = 0 to \(length/2\));
14\(x_{t+1}=PWLCM(x_{t},p)\);
15\(X=[x_{1},x_{2},...,x_{n}]\);
16\(Q_{rule}=mod\big{(}floor\big{(}X\times 10^{15}\big{)},8\big{)}\);
17 Introduce the DNA encoding table \(T\) ;
18 For(t = 0 to \(length/2\));
19\(Q_{1}(t)=T(Q_{rule}(t),I_{bit}(2t,2t+1))\);
20 Output Rule Selection Sequence \(Q_{rule}\), DNA encoded sequence \(Q_{1}\);
```  
**Algorithm 1**DNA hash encoding process  
For readers' convenience, we further offer a step-by-step explanation of hash encoding in Figure 3, which performs an encryption process on a sample image, as follows:  
Figure 3: An example of DNA hash encoding by inputting an image with size \(8\times 8\). **Step 1. ** Obtain the height and width of the original image, where \(H\) and \(W\) equals 8 for the sample image. Convert the original image to a bitstream containing 512 bits of data. **Step 2. ** Save the original height and width as 16-bit data. For example, if \(H\) is 8, its corresponding bitstream is 00000000 00001000. Therefore, we use 32 bits of data to save the original height and width for further restoration. **Step 3. ** Randomly generate a new height and width, where both values should be larger but twice as small as the the original ones. For display, we set the new height \(H^{\prime}\) and width \(W^{\prime}\) to 12 and 10, respectively. **Step 4. ** It is noted that there exist \(H^{\prime}\times W^{\prime}\times 8=960\) bits of data for encoding, where the first 32 bits represent the original height and width, and the other 512 bits refer to image content. It's noted that 416 bits remain unused, where they are randomly filled to make up a total 960-bit bitstream for further computation. **Step 5. ** We generate a 256-bit bit sequence HashKey through the SHA256 hash algorithm based on the input initial key. Note that \(H1\) and \(H2\) can be viewed as 64-bit numbers for computing. **Step 6. ** With \(H1\) and \(H2\), we calculate \(x_{1}=(H1/10^{15})\%1\) and \(p=((H1/10^{15})\%1)/2\). **Step 7. ** Then, \(x_{1}\) and \(p\) are fed into the PWLCM function to generate a sequence, where \((H^{\prime}\times W^{\prime}\times 8)/2=(12\times 10\times 8)/2=480\) numbers are generated. Each of them is multiplied by \(10^{15}\), modulated with 8, achieving results with a rule sequence containing 480 numbers. **Step 8. ** We further match 960 bits of original data with rule sequence, where we match every 2 bits of data with 1 rule number based on DNA mapping rules in Table 1. After converting, we could obtain results as a DNA hash-encoded sequence, which would be sent to the content-aware encryption module for permutation and diffusion.