本来想弄个简单的字符串加密函数,但普通的异或运算可能会导致某些结果位为0或不可见字符,比如当前字符与当前key相同的时候。 aes算法之类的感觉相对Base64要麻烦许多(貌似对内容长度不灵活),而且我不需要很强大的加密,变形的Base64已经足够了。顺便学习一下Base64原理。
/// Base64编码时,每6bit当成一个值(不足6位的低位补0),将这个值当作索引,从编码表中获取一个可见字符作为结果,(不足6bit的每差2位bit在结果后面补一个‘=‘)。 /// Base64解码时,将每个字节当作索引(编码时的可见字符),从解码表中获取值(编码时6bit的值),然后组装成8bit的字节即为原文字节。 /* * * 编码表解码表可以自定义,只要两边的值相互对应即可,以下编解码表使用得比较多,可以参考 * * 编码表: * ‘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 * 解码表: * 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, // 20 - 29 * 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, 0 // 120 - 127 */ /// 例如:ABCDE 二进制对应 01000001 01000010 01000011 01000100 01000101 /// 每6bit为一字节变成: 010000 010100 001001 000011 010001 000100 010100(不足6bit,低位补0) /// 查编码表得到: Q U J D R E U = /// 查解码表得到: 00010000 00010100 00001001 00000011 00010001 00000100 00010100 /// 去掉两个最高位后,再每8bit组合成一个字节: /// 得到原值: 01000001 01000010 01000011 01000100 01000101 00 /// 因为编码后都转换成了可见字符,所以编码前可以使用key进行^加密,解码后使用同样的key^解密即可 //////////////////////////// 头文件 //////////////////////////// #ifndef __PWD_H__ #define __PWD_H__ #include <string> using std::string; /// 基于Base64的加密 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 //////////////////////////// 源文件 //////////////////////////// #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, // 20 - 29 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, 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; index++) { 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_table[ ((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; } } else { break; } } return text; }
以上描述与代码仅供参考,如果发现错误,欢迎指正。
时间: 2024-10-10 14:36:20