# TEDL: A Text Encryption Method Based on Deep Learning
## 6 Encryption and Decryption
### Time-Varying Codebook  
Obviously, if the word vector table (\(T_{\gamma}\)) is directly used for encryption, the above conditions are not enough for security, calling for more careful and sophisticated design. Nevertheless, it remains difficult to determine whether the design is safe or not. Here we adopt a relatively more trustworthy way: process \(T_{\gamma}\) with SHA-256 function, for its avalanche effect and irreversibility. Although there are some algorithms which are safer and faster than SHA-256, the ciphertext will occupy more space. For example, SHA-512 will occupy space as two times as SHA-256. Therefore, we select SHA-256 on the basis of balancing the security and space performance. Since Sigmoid function \(\sigma(x)=\frac{1}{1+e^{-3}}\) is used in the SGHS model, the derived real vectors are irrational vectors, precisely. In theory, irrational numbers are infinitely long but limited by the computational accuracy of a computer, the results are finite and should be kept as a few effective numbers. To simplify the discussion, double-precision floating-point numbers are specified in the program, which means that the real numbers in \(T_{\gamma}\) are truncated to 16-digit precision or 53-bit precision. To send the word vector to SHA-256 function, a simple method is to convert the first 16 significant digits of each dimension into a 16-digit integer. For example,  
\[0.000642163111111111_{10}\Rightarrow 642163111111111_{10}\]  
\[642163111111111112341_{10}\Rightarrow 642163111111111_{10}\]  
If the dimension \(D=200\), all 16-digit integers are spliced to obtain a 3200-digit integer, feeding SHA-256. However, it is extremely time-consuming, degrading the encryption efficiency. Conversely, feeding a short integer string causes a significant waste of space. For a 32-digit integer, at most \(10^{32}\) different results can be generated, far less than \(2^{256}\), the space of the message digest generated by SHA-256. Therefore, we consider connecting five 16-digit integers together, the space of which is \(10^{80}\approx 864\times 2^{256}\). Therefore, the transformation for a vector of word \(w_{i}\) is illustrated as Figure 4.  
Figure 4: Basic transformation. As information is transmitted, the primary process of communication is shown in Figure 5, where \(D=200\), \(h^{0}_{i,j}\) denotes the \(j\)-dimensional in the vector shown as the fourth line in Figure 4. Its corresponding word is \(w_{i}\). In essence, it is similar to the polyalphabetic cipher [42] for resistance to frequency analysis. In other words, in the case where the same word is used multiple times, the corresponding hash, always the first component in a hash vector, is indexed from a different vector table each time. However, it is not safe enough due to the relatively limited vector tables. Therefore, we divide the \(D^{\prime}\)-dimensional vector into two parts, a \((N_{3}+1)\)-dimensional vector and a 1-dimensional vector, which are named loop vector and reserved vector, respectively. The more complex and safer processing is shown in Figure 6. \(h^{k}_{i,j}\) denotes a value in loop vectors while \(rh_{i}\) denotes a value in reserved vectors. \[rh_{i}=h^{0}_{i,D^{\prime}-1} \tag{14}\]  
\[h^{k}_{i,j}=hash\Big{(}h^{k-1}_{i,j}||rh_{i}\Big{)},\quad k=1,2,\cdots \tag{15}\]  
where \(||\) stands for concatenating. The concatenation of two 256-bit values results in a 512-bit number. _hash_ denotes the SHA-256 function. Figure 5: Primary processing for word vector table. Each time only the first dimension hash acts as ciphertext. The difference lies in that as the information interacts, the hash vector table keeps changing. Therefore, it is namely a time-varying codebook. Such a design can greatly extend the replacement table. Ideally, since the space of the hash is \(2^{256}\), there are \(2^{256}\) alternatives to the same word. In summary, as the two parties communicates, the sender will find the encrypted form of messages in codebook word by word, and replace the encrypted word with the original word to send to the receiver.