# Enhancing Visual Data Security: A Novel FSM-Based Image Encryption and Decryption Methodology
## 3 Materials and Methods
### Encryption Algorithm Using Finite State Machines
If there is a cycle, then the finite automaton is not an invertible automaton with finite memory; if there is no cycle, the finite automaton is an invertible automaton with finite memory;
* Find the delay \(\mu=1+1\), where l is the longest path of the graph G from step 3. For example:  
The finite state machine is given in the form of Table 1.  
We build testing Table 2 for the proposed finite state machine. According to testing Table 2, a testing graph is constructed, which is shown in Figure 1.  
Based on testing Table 2, a graph adjacency matrix is constructed. The graph adjacency matrix is shown in Figure 2. To check the testing graph for the presence of loops or cycles, there is \begin{table}
\begin{tabular}{c c c} \hline \hline \multirow{2}{*}{**PS**} & \multicolumn{2}{c}{**NS, z**} \\ \cline{2-3}  & **x = 0** & **x = 1** \\ \hline
0 & 0,0 & 1,0 \\
1 & 2,0 & 3,0 \\
2 & 3,1 & 2,1 \\
3 & 1,1 & 0,1 \\ \hline \hline \end{tabular}
\end{table} 
Table 1: State machine for checking for invertibility. \begin{table}
\begin{tabular}{c c c} \hline \hline \multicolumn{1}{c}{**S**} & \multicolumn{1}{c}{**z = 0**} & \multicolumn{1}{c}{**z = 1**} \\ \hline
0 & (0,1) & - \\
1 & (2,3) & - \\
2 & - & (2,3) \\
3 & - & (0,1) \\ (0,1) & (0,2), (0,3) & - \\  & (1,2), (1,3) & - \\ (2,3) & - & (0,2), (0,3) \\ \hline \hline \end{tabular}
\end{table} 
Table 2: Test table for checking the finite state machine for invertibility. Figure 1: Testing graph. Based on the testing table, an adjacency matrix of the graph G with dimension \(\mathrm{p}\times\mathrm{p}\) is constructed (\(\mathrm{p}\) is the number of vertices of the graph);
2. The row that has 0 in all positions is deleted, and the corresponding column is deleted. If there are no such lines, go to step 4;
3. Step 2 is repeated;
4. If the matrix has not completely disappeared, then the graph has a cycle. If the matrix has disappeared, then the graph does not have a cycle. According to Step 1, an adjacency matrix is constructed, and then in Step 2, zero rows and their corresponding columns are removed. As a result, our matrix disappears. Therefore, the graph has no cycles. It is worth noting that the number of iterations required to remove all rows of zeros represents the finite delay \(\tau\). After checking, we can say that the considered machine is invertible with a delay of 2. In addition, it should be noted that the upper estimate of the delay \(\mu\), which was determined by [PERSON], where \(\mu\leq\frac{|\mathrm{S}|(|\mathrm{S}|-1)}{2}\). Indeed, the automaton considered in the example has four states, then we obtain \(\frac{|\overline{4}|(|\overline{4}|-1)}{2}=6\geq\mu\). This statement was investigated and demonstrated in [15]; [PERSON] tested the possibility of constructing invertible automata with a delay \(\mu\). When choosing a delay \(\mu\) for more states of the automaton, the automaton was not invertible. At the decryption stage, the encrypted file is submitted for transformation to a finite state machine, which is the inverse of the machine used in the encryption process. The decryption process using invertible FSMs involves several key steps:  
1. The decryption begins with the FSM in a known initial state. The encrypted data (output sequence of the encryption process) is then fed into the FSM;
2. As the FSM processes the input data, it transitions between states according to its transition function, which is designed to be reversible. The transition functions and the output functions are crafted such that each input and output symbol leads to a unique next state;
3. Since the FSM is invertible, the sequence of states it transitions through can be used to uniquely determine the original input sequence. This sequence represents the decrypted data. The invertible finite state machine was built using the finite state machine algorithm described by [PERSON] [38]. After the intermediate file is received, it is fed to the inverse permutation finite state machine, which is built according to the rule described in the article [34].