|
[code]bool XorEncrypt(void* bufPtr, unsigned int bufSize, const char* key, unsigned int keySize)
{
if (!bufPtr || !key || keySize == 0)
{
return false;
}
char* ptr = (char*)bufPtr;
unsigned int index;
for (unsigned int i = 0; i < bufSize; i++)
{
index = i%keySize;
ptr[i] ^= key[index];
}
return true;
}
bufPtr为需要加密的数据指针
bufSize为需要加密的数据长度
key为密钥数据指针
keySize为密钥数据长度
void main()
{
const char* szKey = "Garbageman is grabage";
unsigned int keySize = strlen(szKey);
int test_int[10] =
{0, 0xff356992, 323, 23582, 0x90abcd,
332335, 69895, 456812, 548, 7646};
float test_float[10] =
{0.0f, 1.000001f, 953214.12f, 3658.01f, 5245.045f,
1.000001f, 953214.12f, 3658.0f, 9545.0f, 65323.0f};
double test_double[10] =
{0.0, 1.000001, 953214.12, 3658.01, 5245.045,
1.000001, 953214.12, 3658.0, 9545.0, 65323.0};
// 加密
XorEncrypt(test_int, sizeof(test_int), szKey, keySize);
XorEncrypt(test_float, sizeof(test_float), szKey, keySize);
XorEncrypt(test_double, sizeof(test_double), szKey, keySize);
// 解密
XorEncrypt(test_int, sizeof(test_int), szKey, keySize);
XorEncrypt(test_float, sizeof(test_float), szKey, keySize);
XorEncrypt(test_double, sizeof(test_double), szKey, keySize);
int m = 0;
}[/code] |
|