Base64 learning, base64
Originally, we wanted to create a simple string encryption function. However, a normal XOR operation may result in some result bits being 0 or invisible characters, such as when the current character is the same as the current key. The aes algorithm is much more difficult than Base64 (it seems that the content length is not flexible), and I don't need strong encryption. The modified Base64 is enough.
By the way, let's take a look at the Base64 principle.
/// When Base64 encoding is performed, every 6 bits are regarded as a value (with less than 6 bits complement 0). This value is used as an index and a visible character is obtained from the encoding table as a result, (if there are less than 6 bits, two bits are added with a '=' after the result '). /// During Base64 decoding, each byte is used as an index (visible Characters During encoding) and the value (6-bit value during encoding) is obtained from the decoding table ), then it is assembled into 8-bit bytes, that is, the original bytes. /*** The encoding table decoding table can be customized as long as the values on both sides correspond to each other. The following encoding and decoding tables are used A lot. For details, refer to ** encoding table: * 'A ', 'B', 'C', 'D', 'E', 'F', 'G', 'h', 'I', 'J ', // 0-9 * 'k', 'l', 'M', 'n', 'O', 'P', 'Q', 'R ', 'S ', 't', // 10-19 * 'U', 'V', 'w', 'x', 'y', 'z ', 'A', 'B', 'C', 'D', // 20-29 * 'E', 'F', 'G', 'h ', 'I', 'J', 'k', 'l', 'M', 'n', // 30-39 * 'O', 'P ', 'Q', 'R', 's', 't', 'U', 'V', 'w', 'x ', // 40-49 * 'y', 'z', '0', '1', '2', '3', '4' ', '5', '6', '7', // 50-59 * '8', '9',' + ', '/' // 60-63 * decoding table: * 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0-9*0, 0, 0, 0, 0, 0, 0, 0, 0, // 10-19*0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 20-29*0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 30-39*0, 0, 0, 62, 0, 0, 0, 63, 52, 53, // 40-49*54, 55, 56, 57, 58, 59, 60, 61, 0, 0, // 50-59*0, 61, 0, 0, 0, 1, 2, 3, 4, // 60-69*5, 6, 7, 8, 9, 10, 11, 12, 13, 14, // 70-79*15, 16, 17, 18, 19, 20, 21, 22, 23, 24, // 80-89*25, 0, 0, 0, 0, 0, 0, 26, 27, 28, // 90-99*29, 30, 31, 32, 33, 34, 35, 36, 37, 38, // 100-109*39, 40, 41, 42, 43, 44, 45, 46, 47, 48, // 110-119*49, 50, 51, 0, 0, 0, 0, 0 // 120-127 * /// For example: ABCDE binary corresponds to 01000001 01000010 01000011 01000100 01000101 // each 6 bits is converted: 010000 010100 001001 000011 010001 000100 010100 (No 6bit, low position 0) // query the encoding table to obtain: q u j d r e u = // query and decoding table: 00010000 00010100 00001001 00000011 00010001 00000100 00010100 // after removing the two highest bits, combine each 8 bits into a byte: // get the original value: 01000001 01000010 01000011 01000100 01000101 00 // because all encoded characters are converted to visible characters, you can use the key for ^ encryption Before encoding, after decoding, use the same key ^ to decrypt the file. /// // header file // /// // # ifndef _ PWD_H __# define _ PWD_H __# include <string> using std:: string; // Base64-Based Encryption class PWD {public: static string encode (const string & text, const string & key); static string decode (const string & pass, const string & key); private: static const char _ encode_table [64]; static const char _ decode_table [128];}; # endif // source file ///////// /// // # include "PWD. h "const char PWD: _ encode_table [64] = {'A', 'B', 'C', 'D', 'E', 'F ', 'G', 'h', 'I', 'J', // 0-9 'K', 'L', 'M', 'n', 'O', 'P', 'Q', 'R', 's','t ', // 10-19 'U', 'V', 'w', 'x', 'y', 'z', 'A', 'B ', 'C', 'D', // 20-29 'E', 'F', 'G', 'h', 'I', 'J', 'k ', 'l', 'M', 'n', // 30-39 'O', 'P', 'Q', 'R','s ', 'T', 'U', 'V', 'w', 'x', // 40-49 'y', 'z', '0 ', '1', '2', '3', '4', '5', '6', '7', // 50-59 '8 ', '9', '+', '/' // 60-63}; const char PWD: _ decode_table [128] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0 -9 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 10-19 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 20-29 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 30-39 0, 0, 0, 62, 0, 0, 0, 63, 52, 53, // 40-49 54, 55, 56, 57, 58, 59, 60, 61, 0, 0, // 50-59 0, 61, 0, 0, 0, 0, 1, 2, 3, 4, // 60-69 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, // 70-79 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, // 80-89 25, 0, 0, 0, 0, 0, 0, 26, 27, 28, // 90-99 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, // 100-109 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, // 110-119 49, 50, 51, 0, 0, 0, 0 // 120-127}; string PWD :: encode (const string & text, const string & key) {string pwd; unsigned char temp [4] = {0}; size_t text_len = text. length (); size_t key_len = key. length (); int key_index = 0; if (key_len = 0) {return pwd;} for (int index = 0; index <text_len/3; ind Ex ++) {temp [1] = text [index * 3 + 0] ^ key [(++ key_index) % key_len]; temp [2] = text [index * 3 + 1] ^ key [(++ key_index) % key_len]; temp [3] = text [index * 3 + 2] ^ key [(++ key_index) % key_len]; pwd + = _ encode_table [temp [1]> 2]; pwd + = _ encode_table [(temp [1] <4) | (temp [2]> 4) & 0x3F]; pwd + = _ encode_table [(temp [2] <2) | (temp [3]> 6) & 0x3F]; pwd + = _ encode_table [temp [3] & 0x3F];} int Mod = text_len % 3; if (mod = 1) {temp [1] = text [text_len-1] ^ key [(++ key_index) % key_len]; pwd + = _ encode_table [(temp [1] & 0xFC)> 2]; pwd + = _ encode_table [(temp [1] & 0x03) <4]; pwd + = "=";} else if (mod = 2) {temp [1] = text [text_len-2] ^ key [(++ key_index) % key_len]; temp [2] = text [text_len-1] ^ key [(++ key_index) % key_len]; pwd + = _ encode_table [(temp [1] & 0xFC)> 2]; pwd + = _ encode_ta Ble [(temp [1] & 0x03) <4) | (temp [2] & 0xF0)> 4)]; pwd + = _ encode_table [(temp [2] & 0x0F) <2]; pwd + = "=" ;}return pwd ;}string PWD :: decode (const string & pass, const string & key) {string text; int temp; size_t pass_len = pass. length (); size_t key_len = key. length (); int index = 0; int key_index = 0; if (key_len = 0) {return text;} while (index <pass_len) {temp = _ decode_table [pass [index ++] <18; Temp + = _ decode_table [pass [index ++] <12; text. push_back (temp & 0xFF0000)> 16) ^ key [(++ key_index) % key_len]); if (pass [index]! = ') {Temp + = _ decode_table [pass [index ++] <6; text. push_back (temp & 0xFF00)> 8) ^ key [(++ key_index) % key_len]); if (pass [index]! = ') {Temp + = _ decode_table [pass [index ++]; text. push_back (temp & 0xFF) ^ key [(++ key_index) % key_len]);} else {break ;}} return text ;}
The above description and code are for reference only. If any error is found, please correct it.