forked from metin2/client
102 lines
2.7 KiB
C++
102 lines
2.7 KiB
C++
|
/*
|
|||
|
* Filename: tea.c
|
|||
|
* Description: TEA <EFBFBD><EFBFBD>ȣȭ <EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
*
|
|||
|
* Author: <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> (aka. <EFBFBD><EFBFBD><EFBFBD><EFBFBD>, Cronan), <EFBFBD>ۿ<EFBFBD><EFBFBD><EFBFBD> (aka. myevan, <EFBFBD><EFBFBD><EFBFBD>ڷ<EFBFBD>)
|
|||
|
*/
|
|||
|
#include "StdAfx.h"
|
|||
|
#include "tea.h"
|
|||
|
#include <memory.h>
|
|||
|
|
|||
|
/*
|
|||
|
* TEA Encryption Module Instruction
|
|||
|
* Edited by <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> aka. <EFBFBD><EFBFBD><EFBFBD><EFBFBD>, Cronan
|
|||
|
*
|
|||
|
* void tea_code(const unsigned long sz, const unsigned long sy, const unsigned long *key, unsigned long *dest)
|
|||
|
* void tea_decode(const unsigned long sz, const unsigned long sy, const unsigned long *key, unsigned long *dest)
|
|||
|
* 8<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ʈ<EFBFBD><EFBFBD> <EFBFBD><EFBFBD>ȣ/<EFBFBD><EFBFBD>ȣȭ <EFBFBD>Ҷ<EFBFBD> <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȴ<EFBFBD>. key <EFBFBD><EFBFBD> 16 <EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ʈ<EFBFBD><EFBFBD><EFBFBD><EFBFBD> <EFBFBD>Ѵ<EFBFBD>.
|
|||
|
* sz, sy <EFBFBD><EFBFBD> 8<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ʈ<EFBFBD><EFBFBD> <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ѵ<EFBFBD>.
|
|||
|
*
|
|||
|
* int tea_decrypt(unsigned long *dest, const unsigned long *src, const unsigned long *key, int size);
|
|||
|
* int tea_encrypt(unsigned long *dest, const unsigned long *src, const unsigned long *key, int size);
|
|||
|
* <EFBFBD>Ѳ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 8 <EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ʈ <EFBFBD>̻<EFBFBD><EFBFBD><EFBFBD> <EFBFBD><EFBFBD>ȣ/<EFBFBD><EFBFBD>ȣȭ <EFBFBD>Ҷ<EFBFBD> <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ѵ<EFBFBD>. <EFBFBD><EFBFBD><EFBFBD><EFBFBD> size <EFBFBD><EFBFBD>
|
|||
|
* 8<EFBFBD><EFBFBD> <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <EFBFBD>ƴϸ<EFBFBD> 8<EFBFBD><EFBFBD> <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> ũ<EFBFBD>⸦ "<EFBFBD>÷<EFBFBD><EFBFBD><EFBFBD>" <EFBFBD><EFBFBD>ȣȭ <EFBFBD>Ѵ<EFBFBD>.
|
|||
|
*
|
|||
|
* ex. tea_code(pdwSrc[1], pdwSrc[0], pdwKey, pdwDest);
|
|||
|
* tea_decrypt(pdwDest, pdwSrc, pdwKey, nSize);
|
|||
|
*/
|
|||
|
|
|||
|
#define TEA_ROUND 32 // 32 <20><> <20><><EFBFBD><EFBFBD><EFBFBD>ϸ<EFBFBD>, <20><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD>.
|
|||
|
#define DELTA 0x9E3779B9 // DELTA <20><> <20>ٲ<EFBFBD><D9B2><EFBFBD> <20><><EFBFBD><EFBFBD>.
|
|||
|
|
|||
|
void tea_code(const unsigned long sz, const unsigned long sy, const unsigned long *key, unsigned long *dest)
|
|||
|
{
|
|||
|
register unsigned long y = sy, z = sz, sum = 0;
|
|||
|
unsigned long n = TEA_ROUND;
|
|||
|
|
|||
|
while (n-- > 0)
|
|||
|
{
|
|||
|
y += ((z << 4 ^ z >> 5) + z) ^ (sum + key[sum & 3]);
|
|||
|
sum += DELTA;
|
|||
|
z += ((y << 4 ^ y >> 5) + y) ^ (sum + key[sum >> 11 & 3]);
|
|||
|
}
|
|||
|
|
|||
|
*(dest++) = y;
|
|||
|
*dest = z;
|
|||
|
}
|
|||
|
|
|||
|
void tea_decode(const unsigned long sz, const unsigned long sy, const unsigned long *key, unsigned long *dest)
|
|||
|
{
|
|||
|
#pragma warning(disable:4307)
|
|||
|
register unsigned long y = sy, z = sz, sum = DELTA * TEA_ROUND;
|
|||
|
#pragma warning(default:4307)
|
|||
|
|
|||
|
unsigned long n = TEA_ROUND;
|
|||
|
|
|||
|
while (n-- > 0)
|
|||
|
{
|
|||
|
z -= ((y << 4 ^ y >> 5) + y) ^ (sum + key[sum >> 11 & 3]);
|
|||
|
sum -= DELTA;
|
|||
|
y -= ((z << 4 ^ z >> 5) + z) ^ (sum + key[sum & 3]);
|
|||
|
}
|
|||
|
|
|||
|
*(dest++) = y;
|
|||
|
*dest = z;
|
|||
|
}
|
|||
|
|
|||
|
int tea_encrypt(unsigned long *dest, const unsigned long *src, const unsigned long * key, int size)
|
|||
|
{
|
|||
|
int i;
|
|||
|
int resize;
|
|||
|
|
|||
|
if (size % 8 != 0)
|
|||
|
{
|
|||
|
resize = size + 8 - (size % 8);
|
|||
|
memset((char *) src + size, 0, resize - size);
|
|||
|
}
|
|||
|
else
|
|||
|
resize = size;
|
|||
|
|
|||
|
for (i = 0; i < resize >> 3; i++, dest += 2, src += 2)
|
|||
|
tea_code(*(src + 1), *src, key, dest);
|
|||
|
|
|||
|
return (resize);
|
|||
|
}
|
|||
|
|
|||
|
int tea_decrypt(unsigned long *dest, const unsigned long *src, const unsigned long * key, int size)
|
|||
|
{
|
|||
|
int i;
|
|||
|
int resize;
|
|||
|
|
|||
|
if (size % 8 != 0)
|
|||
|
resize = size + 8 - (size % 8);
|
|||
|
else
|
|||
|
resize = size;
|
|||
|
|
|||
|
for (i = 0; i < resize >> 3; i++, dest += 2, src += 2)
|
|||
|
tea_decode(*(src + 1), *src, key, dest);
|
|||
|
|
|||
|
return (resize);
|
|||
|
}
|
|||
|
|