Code example C for password encryption and decryption in the program
Question proposal
In actual software projects, password processing is often involved, such as user logon password, database password, and FTP password. To increase software flexibility, you generally need to put these passwords in a configuration file. However, the original password is easy to record. If the password is obtained by software intruders, the consequences are unimaginable. Therefore, we cannot directly enter the original password in the configuration file. Instead, we need to encrypt the password first, then enter the encrypted ciphertext in the configuration file, and decrypt it after the program reads the configuration.
The entire process 1 is shown.
Figure 1 Overall encryption and decryption process
Sample procedure
To demonstrate the entire encryption and decryption process, a demo program is designed. The execution process is shown in step 2.
Figure 2 sample procedure
Sample Code
Configuration File example
The configuration file is named Config. ini, as shown in figure 3.
Figure 3 Configuration File Content
Program Execution result
1. When iDecryptFlag is 0:
Result 4:
Figure 4 execution result when iDecryptFlag is 0
View the configuration file. The password value is the encrypted ciphertext.
2. When iDecryptFlag is 1:
Result 5:
Figure 5 execution result when iDecryptFlag is 1
View the configuration file. The password value goes through the process of "original text --> ciphertext --> original text.