C ++ uses the Tea Algorithm for encryption and decryption.
Recently, a cocos2dx project involves file encryption, and decryption is performed when the game is loaded. Therefore, the encryption function must be as fast as possible. Therefore, the tea encryption algorithm is selected. The encryption process uses the commond project of window to encrypt folders or files. Let's talk less about the code. The encryption section is rough because it is not very familiar with the win commond line project. // Tea. cpp: defines the entry point of the console application. // # Include "stdafx. h "# include" Tea/TeaEd. h "# include <fstream> # include <string> # include <iostream> # include <io. h> # include <windows. h> # include <cstring> # include <fstream> using namespace std; // identifies a folder as inline bool IsFolder (const string & strWorkSpace, string & fileName, string & filePath) {bool bCheck = false; fileName = strWorkSpace; filePath = ""; size_t pos = strWorkSpace. find_last_of ("\"); if (po S! = Std: string: npos) {filePath = strWorkSpace. substr (0, pos + 1); fileName = strWorkSpace. substr (pos + 1); pos = fileName. find_last_of (". "); if (pos = std: string: npos) {bCheck = true; fileName =" "; filePath = strWorkSpace ;}} return bCheck ;} int doMethod (const string & str) {// test option. Input 1 to decrypt a file and output the decrypted content if (str = "1") {cout <"input of the file to be decrypted: "<endl; string strFileNameTmp; cin> strFileNameTmp; string str1; string str2; bool bCheck = IsFolder (strFileNameTmp, str1, str2); if (bCheck) {cout <"the decryption test must be a file. Press enter to continue the operation" <endl; system ("PAUSE "); cout <"-if you select to decrypt a file for testing, enter 1 and press Enter." <endl; cout <"-if you select to encrypt the file, enter another character and press Enter." <endl; string strInputTmp; cin> strInputTmp; doMethod (StrInputTmp); return 0;} // This is a singleton Encryption Class. Encrypts and decrypts GetCryTool ()-> deCryWithFileName (strFileNameTmp, ""); return 0 ;}cout <"1. drag the file "<endl; cout <" to create a folder with the rule [file name + '_ cryfile'] under the same directory of the file "<endl; cout <endl; cout <"2. drag the folder "<endl; cout <" to create a cryfolder folder under the directory of the same level in the folder. the encrypted files are all here. "<endl; cout <"\ n drag a file or folder to the bottom:" <endl; string strWorkPath; cin> strWorkPath; string strFileName = ""; string strFilePath = ""; cout <"Pa Th: "<strWorkPath <endl; bool bCheck = IsFolder (strWorkPath, strFileName, strFilePath); if (bCheck) {do {// write path and create cry file folder size_t pos = strFilePath. find_last_of ("\"); if (pos! = Std: string: npos) {strFilePath = strFilePath. substr (0, pos + 1);} strFilePath. append ("cryfolder"); string strCreateForderCMD = "md"; strCreateForderCMD. append (strFilePath); system (strCreateForderCMD. c_str ();} while (0); // if it is a folder, all the files under the folder should be written into the allfiles.txt file. Then read the file by row to obtain the path vector <string> oVecLines; string sline of each file to be encrypted; do {// create record file txt named allFiles // and del it string strAllFilePath = strFilePath; strAllFilePath. append ("\ allFiles.txt"); string strCmd = "cmd/c dir"; strCmd. append (strWorkPath); strCmd. append ("\\*. */a-d/B/s> "); strCmd. append (strAllFilePath); cout <strCmd <endl; system (strCmd. c_str (); ifstream ifs (strAllFilePat H); while (ifs & getline (ifs, sline) {cout <sline <endl; oVecLines. push_back (sline);} ifs. close (); string strDelCMD = "del"; strDelCMD. append (strAllFilePath); system (strDelCMD. c_str ();} while (0); // traverses the file path. Encryption in turn. For (size_t I = 0; I <oVecLines. size (); I ++) {sline = oVecLines [I]; string strTmpfileName = ""; string strTmpFilePath = ""; bool bCheck = IsFolder (sline, strTmpfileName, strTmpFilePath ); if (! BCheck) {string strOutFilePath = strFilePath; strOutFilePath. append ("\"); strOutFilePath. append (strTmpfileName); GetCryTool ()-> enCryWithFileName (sline, strOutFilePath) ;}} else {string strTmpWirtePath = strFilePath; do {// write path and create cry file folder size_t pos = strFilePath. find_last_of ("\"); if (pos! = Std: string: npos) {strFilePath = strFilePath. substr (0, pos + 1);} size_t szPos = strFileName. find_last_of ("."); if (szPos! = Std: string: npos) {strTmpWirtePath. append (strFileName. substr (0, szPos); strTmpWirtePath. append ("_ cryfile");} string strCreateForderCMD = "md"; strCreateForderCMD. append (strTmpWirtePath); system (strCreateForderCMD. c_str ();} while (0); strTmpWirtePath. append ("\"); strTmpWirtePath. append (strFileName); GetCryTool ()-> enCryWithFileName (strWorkPath, strTmpWirtePath);} int _ tmain (int argc, _ TC HAR * argv []) {char buf [1000]; GetModuleFileNameA (NULL, buf, 1000); string strKeyPath (buf); string strKeyfilepath = strKeyPath; size_t szPos = strKeyfilepath. find_last_of ("\"); if (szPos! = Std: string: npos) {strKeyfilepath = strKeyfilepath. substr (0, szPos + 1);} string strKeyPrePath = strKeyfilepath; // key_prepared.txt is a file used to store plaintext passwords. In the encryption project, strKeyPrePath. append ("key_prepared.txt"); // key_result.txt is the password after the key is encrypted. Also in the encrypted project, strKeyfilepath. append ("key_result.txt"); string strKeyPre = ""; do {ifstream ifs (strKeyPrePath); while (ifs & getline (ifs, strKeyPre) {break;} ifs. close () ;}while (0); if (strKeyPre. empty () {cout <"no password is entered. Please write the password in" <strKeyPrePath <"" <endl; return 0;} string str; cout <"[current password:" <strKeyPre <"]" <endl; cout <"-enter 'key' and press enter to change the password." <endl; cout <"-select to decrypt a file for testing. Enter 1 Press ENTER "<endl; cout <"-select encryption. Enter another press ENTER "<endl; cin> str; // determine whether to change the password if (str = "KEY" | str = "key") {system ("cls"); string strNewKeyStr; cout <"Enter the new password:" <endl; cin> strNewKeyStr; GetCryTool ()-> setUpTea (strNewKeyStr, strKeyfilepath); cout <"the password has been modified. continue to "<endl; cout <"-select to decrypt a file for testing. Enter 1 and press ENTER "<endl; cout <"-select encryption, enter another carriage return" <endl; cin> str; doMethod (str) ;}else {GetCryToo L ()-> setUpTea (strKeyPre, strKeyfilepath); doMethod (str) ;}system ("PAUSE"); return 0 ;}this code is part of the main function. Then, add the core part of encryption and decryption. In TeaEd. class TeaEd {public:/* isNetByte is unuseful param in this project. */TeaEd (const byte * key, int round = 32, bool isNetByte = false); TeaEd (const TeaEd & rhs); TeaEd & operator = (const TeaEd & rsh ); void encrypt (const byte * in, byte * out); void decrypt (const byte * in, byte * out); private: void encrypt (const ulong * in, ulong * out); void decrypt (const ulong * in, ulong * out); // Method under is use for net data. // not useful. ulong ntoh (ulong netlong) {return _ isNetByte? /* Ntohl (netlong) */netlong: netlong;} ulong hton (ulong hostlong) {return _ isNetByte? /* Htonl (hostlong) */hostlong: hostlong;} protected: int _ round; // iteration round to encrypt or decrypt // Not useful in this project. bool _ isNetByte; // whether input bytes come from network. byte _ key [16]; // encrypt or decrypt key}; In TeaEd. in the cpp file, TeaEd: TeaEd (const byte * key, int round/* = 32 */, bool isNetByte/* false */): _ round (round ), _ isNetByte (false/* isNetByte */) {if (key! = 0) {memcpy (_ key, key, 16) ;}else {memset (_ key,) ;}} TeaEd: TeaEd (const TeaEd & rhs ): _ round (rhs. _ round), _ isNetByte (/* rhs. _ isNetByte */false) {memcpy (_ key, rhs. _ key, 16);} TeaEd & TeaEd: operator = (const TeaEd & rsh) {if (& rsh! = This) {_ round = rsh. _ round; _ isNetByte =/* rsh. _ isNetByte */false; memcpy (_ key, rsh. _ key, 16);} return * this;} void TeaEd: encrypt (const byte * in, byte * out) {encrypt (const ulong *) in, (ulong *) out);} void TeaEd: decrypt (const byte * in, byte * out) {decrypt (const ulong *) in, (ulong *) out);} void TeaEd: encrypt (const ulong * in, ulong * out) {ulong * k = (ulong *) _ key; register ulong y = in [0]; // nt Oh (in [0]); register ulong z = in [1]; // ntoh (in [1]); register ulong a = k [0]; // ntoh (k [0]); register ulong B = k [1]; // ntoh (k [1]); register ulong c = k [2]; // ntoh (k [2]); register ulong d = k [3]; // ntoh (k [3]); register ulong delta = 0x9E3779B9; /* (sqrt (5)-1)/2*2 ^ 32 */register int round = _ round; register ulong sum = 0; while (round --) {/* basic cycle start */sum + = delta; y + = (z <4) + a) ^ (z + s Um) ^ (z> 5) + B); z + = (y <4) + c) ^ (y + sum) ^ (y> 5) + d);}/* end cycle */out [0] = y; // ntoh (y); out [1] = z; // ntoh (z);} void TeaEd: decrypt (const ulong * in, ulong * out) {ulong * k = (ulong *) _ key; register ulong y = in [0]; // ntoh (in [0]); register ulong z = in [1]; // ntoh (in [1]); register ulong a = k [0]; // ntoh (k [0]); register ulong B = k [1]; // ntoh (k [1]); register ulong c = k [2]; // ntoh (k [2]); register ulong d = k [3]; // ntoh (k [3]); register ulong delta = 0x9E3779B9;/* (sqrt (5)-1) /2*2 ^ 32 */register int round = _ round; register ulong sum = 0; if (round = 32) sum = 0xC6EF3720; /* delta <5 */else if (round = 16) sum = 0xE3779B90; /* delta <4 */else sum = delta <static_cast <int> (logbase (2, round); while (round --) {/* basic cycle start */z-= (y <4) + c) ^ (y + sum) ^ (y> 5) + d); y-= (z <4) + a) ^ (z + sum) ^ (z> 5) + B ); sum-= delta;}/* end cycle */out [0] = y; // ntoh (y); out [1] = z; // ntoh (z);} a password must be used globally for encryption and decryption. Therefore, we use a singleton class to manage encryption and decryption functions. Both are in TeaEd. h and TeaEd. cpp. The Singleton management section of the header file. Many methods have been tried during the test. Therefore, there are many useless interfaces. Only the void enCryWithFileName (const string & strFile, const string & strOutFile); void deCryWithFileName (const string & strFileName, const string & strOutfile) interfaces are actually used. The second parameter used in the decry part is useless. At the beginning of the construction of the singleton class, the Tea object was created by using the password. Due to the busy work, I will not detail it in detail ..