# Two-Layer Security Algorithms to Prevent Attacks on Data in Cyberspace
## 3 Proposed Algorithm
### Data Encryption Process  
A _plaintext_ and a static key are first taken to encrypt the data, as shown in Figure 3. The plaintext is represented by \"[y]\", while the _static key_ is represented by \"[x]\". A _static key_ is a user-defined key that is the same in any text's encryption and decryption. First, the position of all the characters in the _static key_ \"[x]\" is found, for which Equation (1) is used. _Static key_ indexes are represented by \"[x_i]\". Different mathematical notations are shown in Table 1.  
\[[x_{i}]\ = \begin{pmatrix}[x_{i0}]&[x_{i1}]&[x_{i2}]&\dots&\dots&[x_{i2}]\\ &[K_{0}]&[K_{1}]&[K_{2}]&\dots&\dots&[K_{n}]\end{pmatrix}  \tag{1}\]  
Figure 3: The encryption algorithm. After finding the _static key_ position \"[\(x_{i}\)]\", all characters are represented in index form. \([K_{0}]\)\([K_{1}]\)\([K_{2}]\)\(\ldots\ldots\ldots\)\([K_{n}]\) are all the indexes of \"[\(x_{i}\)]\", while [\(K_{i}\)] represents all indexes of \"[\(x_{i}\)]\". As shown in Equation (2). \[[K_{i}]\ =\ [K_{0}]\ [K_{1}]\ [K_{2}]\ \ldots\ldots\ldots\ [K_{n}] \tag{2}\]  
After finding index positions [\(K_{i}\)] from the _static key_ \"[\(x_{i}\)]\", the length of the _static key_ is calculated. To calculate the _length_ of [\(K_{i}\)], Equation (3) is used. \[m=[K_{i}]+1 \tag{3}\]  
The _static key length_ is represented by \"\(m\)\". After identifying the _indexes_ and _length_ of the _static key_ \"[\(x\)]\", the position and _length_ of _plaintext_ \"[\(y\)]\" are calculated. First, the index identification of the _plaintext_ is made. The position of _plaintext_ \"[\(y\)]\" is determined with the help of Equation (4). \[[y_{i}]\ =\ \left( \begin{array}{c}[y_{i0}]\\ [T_{0}]\end{array} [T_{1}]\ [T_{2}]\ \ldots\ldots\ldots\ [T_{n}]\end{array}\right) \tag{4}\]  
The reason for finding the _indexes_ of _plaintext_ \"[\(x_{i}\)]\" and _static key_ position \"[\(y_{i}\)]\" is to implement a security mechanism on each _letter_ and obtain an _encrypted text_ that cannot be decrypted without the proposed paper mechanism. After finding the _indexes_ of the _text_, all characters are represented in separate _index form_\([T_{0}]\)\([T_{1}]\)\([T_{2}]\)\(\ldots\ldots\ldots\)\([T_{n}]\), as shown in Equation (5). \[[T_{i}]=[T_{0}]\ [T_{1}]\ [T_{2}]\ldots\ldots\ldots\ [T_{n}] \tag{5}\]  
After finding the _text indexes_ [\(T_{i}\)], the length of [\(T_{i}\)] is calculated by using formula \"[\(T_{i}\)] = [\(T_{i}\)] + 1\", and then lengths of [\(K_{i}\)] and [\(T_{i}\)] are compared. If _both lengths are_ equal, then the _elements_ of [\(T_{i}\)] and [\(K_{i}\)] are converted to _binary form_. If the _lengths_ are not equal, then [\(K_{i}\)] is repeated [\(T_{i}\)] times. After conversion to _binary form_, the _XOR mechanism_ is implemented with the help of Equation (6). \[Z_{\oplus[i]}=([T_{0}]\oplus[K_{0}])\ ([T_{1}]\oplus[K_{1}])\ \ldots\ \ldots\ \ ([T_{n}]\oplus[K_{n}]) \tag{6}\]  
All text indexes [\(T_{i}\)] and key indexes [\(K_{i}\)] of \"[\(Z_{\oplus[i]}\)\" (from index = 0 to index = n) are XORed, as shown in Equation (7). \[Z\oplus_{[i]}=\oplus_{n}^{i=0}\ T_{i}\ K_{i} \tag{7}\]  
After _XOR_, the values of \"[\(Z_{\oplus[i]}\)]\" (Equation (6)) are converted to _Decimal_ \"[\(D_{[i]}\)\". As shown in Equation (8). \[D_{[i]}\ =\ [D_{1}]\ [D_{2}]\ [D_{3}]\ \ldots\ldots\ldots\ [D_{n}] \tag{8}\]  
\"[\(D_{[i]}\)]\" is the _decimal result_ that is obtained from the [\(D_{1}\)] _to_ [\(D_{n}\)] _indexes_. After obtaining different _decimal values_ \"[\(D_{[i]}\)]\", the _GCD_ of _key length (m)_ and _text length (n)_ is calculated. The reason for finding the _length of the key_ and _text_ is to calculate and use _greatest common