When we use local data to store CCUserDefault, there will be a defect, that is, the storage method used by CCUserDefault is plain text storage, which is very convenient for gamers to modify the archived data. What we want to learn today is how to use the SHA1 message digest algorithm to encrypt the data stored in CCUserDefault.
SHA1 library files: http://www.tamale.net/enter the web page, find the SHA-1 click to download, decompress the two files sha1.h and sha1.cpp copy to the project directory.
The encryption process using the information digest algorithm is as follows:
1. First, use CCUserDefault to store a data
2. Use the addBytes method of sha1 to write the data to be verified.
3. create one more key. The key still needs to be written, but it will be hidden in the encoding. When we retrieve the stored data, we will also retrieve the key value and then compare the verification code, if they are the same, the data is not modified. Of course, if someone finds the key in your project compilation, it's another thing... but the average player doesn't have this strength.
See the code below:
First, introduce the header file:
# Include sha1.h
Then I added the following code in the init () method of HelloWorld:
Console output result:
If the verification result is the same, the data is not modified.