我們日常工作中經常要對一些東西進行加密,可選的加密方法當然很多了,Windows都自帶了加密庫,但密碼學方面的東西實在令我頭大,可能因為我從小學開始數學一直沒學好的緣故,而我仔細考慮之后發覺:我們對信息加密,不一定是出自于什么“top security”的原因,大多可能僅僅是不想讓用戶看到具體的文件的結構或保護一些個人隱私信息,也就是說對安全的要求一般,沒必要引入太過重量級的東西,最好是非常快捷、高效和輕量的。那本文提供了一種方法,能讓你的信息看起來被很好地加過密了,并且你根本不需要去研究什么密碼學,也不需要引入什么龐大的Lib,因為代碼就那么幾行。
寫這個程序的時候其實我是想要這么一種加密方法:假如明文只有一個字節,那加密出來的密文應該也只有一個字節。而實際上別人實現好的AES算法加密出來的密文通常都要帶上一串隨機數,不太符合我的要求。
沒有什么include,沒有什么LIB,更沒有什么DLL,代碼再簡單不過,但我保證它很有效,OK,不多說了,看代碼:
#include "String.h"
//加密和解密,其實這兩個函數完全相同的,簡單起見嘛,另外我居然允許密碼為空,也是簡單起見
void JiangEncode(unsigned char *pBuff, int iBuffLen, char *pKey=NULL, int iKeyLen=0);
void JiangDecode(unsigned char *pBuff, int iBuffLen, char *pKey=NULL, int iKeyLen=0);
//這個main函數是用來測試的
#define TEST_LEN 100
int main(int argc, char* argv[])
{
unsigned char totest[TEST_LEN];
memcpy(totest, "012345678901234567890123456789012345678901234567890123456789\
0123456789012345678901234567890123456789", 100);
JiangEncode(totest, TEST_LEN, "J~xye", 6);
int i;
for(i=0; i<TEST_LEN; i++)
{
if((i%10)==0)
printf("\n");
printf("%d ", totest[i]);
}
printf("\n");
JiangDecode(totest, TEST_LEN, "J~xye", 6);
for(i=0; i<TEST_LEN; i++)
{
if((i%10)==0)
printf("\n");
printf("%d ", totest[i]);
}
return 0;
}
//交換兩個BYTE
void Swap2Byte(unsigned char* v1, unsigned char* v2)
{
*v1 ^= *v2;
*v2 ^= *v1;
*v1 ^= *v2;
}
void GetMyCypher(const char* pKey, int iKeyLen, unsigned char* pMyCypher)
{
//原始加密索引
const unsigned char cypherOrg[256] =
{
1, 3, 5, 15, 17, 51, 85, 255, 26, 46, 114, 150, 161, 248, 19, 53,
95, 225, 56, 72, 216, 115, 149, 164, 247, 2, 6, 10, 30, 34, 102, 170,
229, 52, 92, 228, 55, 89, 235, 38, 106, 190, 217, 112, 144, 171, 230, 49,
83, 245, 4, 12, 20, 60, 68, 204, 79, 209, 104, 184, 211, 110, 178, 205,
76, 212, 103, 169, 224, 59, 77, 215, 98, 166, 241, 8, 24, 40, 120, 136,
131, 158, 185, 208, 107, 189, 220, 127, 129, 152, 179, 206, 73, 219, 118, 154,
181, 196, 87, 249, 16, 48, 80, 240, 11, 29, 39, 105, 187, 214, 97, 163,
254, 25, 43, 125, 135, 146, 173, 236, 47, 113, 147, 174, 233, 32, 96, 160,
251, 22, 58, 78, 210, 109, 183, 194, 93, 231, 50, 86, 250, 21, 63, 65,
195, 94, 226, 61, 71, 201, 64, 192, 91, 237, 44, 116, 156, 191, 218, 117,
159, 186, 213, 100, 172, 239, 42, 126, 130, 157, 188, 223, 122, 142, 137, 128,
155, 182, 193, 88, 232, 35, 101, 175, 234, 37, 111, 177, 200, 67, 197, 84,
252, 31, 33, 99, 165, 244, 7, 9, 27, 45, 119, 153, 176, 203, 70, 202,
69, 207, 74, 222, 121, 139, 134, 145, 168, 227, 62, 66, 198, 81, 243, 14,
18, 54, 90, 238, 41, 123, 141, 140, 143, 138, 133, 148, 167, 242, 13, 23,
57, 75, 221, 124, 132, 151, 162, 253, 28, 36, 108, 180, 199, 82, 246, 1
};
memcpy(pMyCypher, cypherOrg, 256);
//根據密碼調整加密索引
int i, j;
for(i=0; i<iKeyLen; i++)
{
for(j=0; j<256; j++)
{
int iSwitchIndex = (pMyCypher[j] * pKey[i])%255;
if(iSwitchIndex!=j)
Swap2Byte(&(pMyCypher[j]), &(pMyCypher[iSwitchIndex]));
}
}
}
void JiangCode(unsigned char* pBuff, int iBuffLen, char* pKey, int iKeyLen)
{
unsigned char cypher[256];
GetMyCypher(pKey, iKeyLen, cypher);
int iIndex=0;
while (iIndex<iBuffLen)
{
//其實……也就一個異或操作,所以加密和解密的過程完全一樣
pBuff[iIndex] ^= cypher[iIndex%256];
++iIndex;
}
}
void JiangEncode(unsigned char* pBuff, int iBuffLen, char* pKey, int iKeyLen)
{
JiangCode(pBuff, iBuffLen, pKey, iKeyLen);
}
void JiangDecode(unsigned char* pBuff, int iBuffLen, char* pKey, int iKeyLen)
{
JiangCode(pBuff, iBuffLen, pKey, iKeyLen);
}
寫完了,運行結果是如下:
241 206 235 249 4 160 190 73 253 196
160 238 105 243 18 190 10 97 26 66
182 218 99 73 40 204 208 123 196 46
76 208 117 159 38 66 234 117 142 52
66 63 135 193 60 49 228 15 12 58
131 252 1 171 96 72 254 198 171 201
110 242 27 189 199 108 136 12 177 215
88 66 156 183 221 142 27 45 61 221
122 158 39 174 123 112 156 241 77 227
112 148 57 67 225 30 47 49 83 94
48 49 50 51 52 53 54 55 56 57
48 49 50 51 52 53 54 55 56 57
48 49 50 51 52 53 54 55 56 57
48 49 50 51 52 53 54 55 56 57
48 49 50 51 52 53 54 55 56 57
48 49 50 51 52 53 54 55 56 57
48 49 50 51 52 53 54 55 56 57
48 49 50 51 52 53 54 55 56 57
48 49 50 51 52 53 54 55 56 57
48 49 50 51 52 53 54 55 56 57 Press any key to continue
上面這段是密文,看起來是不是完全沒有原文的樣子了?你試試看把TEST_LEN改為1,只加密一個字節看,這也是允許的,代碼純C,移植性很好,拿去別處用也方便。但對安全性要求高的應用可能不能使用這種方法了,至于怎么樣才算安全,我也不知道,密碼學方面的東西我最害怕了。