简单的字符串加密算法
本文介绍的一种简单的字符串加密算法,主要有两部分组成:
1.加密钥匙字符串混淆;2.加密字符串与钥匙之间的异或加密;
先附上加密和解密的实现代码,后面有机会再详细分析一下。
加密常量(可以定义任意常量)
#define C1 1
#define C2 2
加密
CString EnCrypt(CString S, WORD key)
{
CString Result, str;
int i, j;
Result = S;
for (i = 0; i < S.GetLength(); i++)
{
Result.SetAt(i, S.GetAt(i) ^ (key >> 8));
key = ((BYTE)Result.GetAt(i) + key)*C1 + C2;
}
S = Result;
Result.Empty();
for (i = 0; i < S.GetLength(); i++)
{
j = (BYTE)S.GetAt(i);
str = "12";
str.SetAt(0, 65 + j / 26);
str.SetAt(1, 65 + j % 26);
Result += str;
}
return Result;
}
解密
CString DeCrypt(CString S, WORD key)
{
int i, j;
CString Result, str;
Result.Empty();
for (i = 0; i < S.GetLength() / 2; i++)
{
j = ((BYTE)S.GetAt(2 * i) - 65) * 26;
j += ((BYTE)S.GetAt(2 * i + 1) - 65);
str = "1";
str.SetAt(0, j);
Result += str;
}
S = Result;
for (i = 0; i < S.GetLength(); i++)
{
Result.SetAt(i, (BYTE)S.GetAt(i) ^ (key >> 8));
key = ((BYTE)S.GetAt(i) + key)*C1 + C2;
}
return Result;
}