CMUtil/src/crypt/Overflow.cpp

94 lines
2.2 KiB
C++
Raw Normal View History

2023-07-21 19:33:38 +08:00
#include<hgl/type/DataType.h>
namespace hgl
{
namespace crypt
{
/**
*
* @param target
* @param source
* @param size
* @param key
* @param key_size
*/
void OverflowEncrypt(void *target, void *source, int size, void *key, int key_size)
{
int n;
uint8 *key_p;
uint8 *tp, *sp;
n = key_size;
key_p = (uint8 *)key;
tp = (uint8 *)target;
sp = (uint8 *)source;
while (size--)
{
uint tmp = (*sp) + (*key_p);
if (tmp > 0xFF)
*tp = tmp - 0x100;
else
*tp = tmp;
*key_p ^= *tp;
tp++;
sp++;
if (--n == 0)
{
n = key_size;
key_p = (uint8 *)key;
}
else
key_p++;
}
}
/**
*
* @param target
* @param source
* @param size
* @param key
* @param keysize
*/
void OverflowDecrypt(void *target, void *source, int size, void *key, int key_size)
{
int n;
uint8 *key_p;
uint8 *tp, *sp;
n = key_size;
key_p = (uint8 *)key;
tp = (uint8 *)target;
sp = (uint8 *)source;
while (size--)
{
if (*key_p > *sp)
*tp = *sp + 0x100 - (*key_p);
else
*tp = *sp - (*key_p);
*key_p ^= *sp;
tp++;
sp++;
if (--n == 0)
{
n = key_size;
key_p = (uint8 *)key;
}
else
key_p++;
}
}
}//namespace crypt
}//namespace hgl