关于加密程序
开发加密程序时,有很多种加密算法可以选择,blowfish,AES,还有简单的异或运算啦等等
其实这些都不是关键,关键点是如何隐藏加密算法的key
如果将key作为明文保存在程序中,很容易在数据段中查看到相应的值
从而失去了加密的意义
由此可见,一个关键点在于隐藏key字符
使用boost库的序列化宏,可以简单的实现隐藏key的功能
代码如下:
1 #define CRYPT_MACRO(r, d, i, elem) ( elem ^ ( d - i ) ) 2 3 #include <boost/preprocessor/cat.hpp> 4 #include <boost/preprocessor/seq/for_each_i.hpp> 5 #include <boost/preprocessor/seq/enum.hpp> 6 7 #define DEFINE_HIDDEN_STRING(NAME, SEED, SEQ)\ 8 static const char* BOOST_PP_CAT(Get, NAME)()\ 9 {\ 10 static char data[] = {\ 11 BOOST_PP_SEQ_ENUM(BOOST_PP_SEQ_FOR_EACH_I(CRYPT_MACRO, SEED, SEQ)),\ 12 '\0'\ 13 };\ 14 \ 15 static bool isEncrypted = true;\ 16 if ( isEncrypted )\ 17 {\ 18 for (unsigned i = 0; i < ( sizeof(data) / sizeof(data[0]) ) - 1; ++i)\ 19 {\ 20 data[i] = CRYPT_MACRO(_, SEED, i, data[i]);\ 21 }\ 22 \ 23 isEncrypted = false;\ 24 }\ 25 \ 26 return data;\ 27 } 28 29 //32116D937A114CF68FBA11E55F4B7150 30 //258795D85B004823B5546DB33E055F0D 31 32 DEFINE_HIDDEN_STRING(EncryptionKey1, 0x7f, ('3')('2')('1')('1')('6')('D')('9')('3')('7')('A')('1')('1')('4')('C')('F')('6')('8')('F')('B')('A')('1')('1')('E')('5')('5')('F')('4')('B')('7')('1')('5')('0')) 33 DEFINE_HIDDEN_STRING(EncryptionKey2, 0x5d, ('2')('5')('8')('7')('9')('5')('D')('8')('5')('B')('0')('0')('4')('8')('2')('3')('B')('5')('5')('4')('6')('D')('B')('3')('3')('E')('0')('5')('5')('F')('0')('D')) 34 35 const char* GetEncodeKey1(void) 36 { 37 return GetEncryptionKey1(); 38 } 39 40 const char* GetEncodeKey2(void) 41 { 42 return GetEncryptionKey2(); 43 }
原始的key是由guid生成器生成,将key序列化打乱之后,在程序的二进制数据,代码段,数据段都不会找到key的原始数据了