#include
#include
#include
#include
int encode(const void* raw_in, void* raw_out, uint32_t password, size_t len)
{
const uint8_t* in=(const uint8_t*)raw_in;
uint8_t* out=(uint8_t*)raw_out;
uint32_t seed = password ^ 0x164ba504u;
for (size_t i = 0; i < len; ++i)
{
uint8_t a = (in[i] ^ seed) >> 4; //将in[i]的高四位异或后的结果存放在a的低四位中
uint8_t b = ((((uint32_t)in[i]) << 12)^seed)>>(12-4); //将in[i]的低四位异或后放
在b的高四位中
a &= 15; //将未存储有效数据位清零
b &= 240; //将未存储有效数据位清零
a = 15 & (a ^(b <<3)); //这句约等于没有,不起任何作用起误导作用
out[i] = a | b; //把经过转换的数据相或组成新的数据
seed = ((seed ^ out[i]) * 48475829 + out[i]); //通过通式更新seed
}
}
int decode(const void* raw_in, void* raw_out, uint32_t password, size_t len)
{
const uint8_t* in = (const uint8_t*)raw_in;
uint8_t* out = (uint8_t*)raw_out;
uint32_t seed = password ^ 0x164ba504u;
for(size_t i = 0 ; i < len; ++i)
{
// 请在此处补全代码
uint8_t a = in[i] & 15;//取出in[i]的低四位放入a中
uint8_t b = in[i] & 240;//取出in[i]的高四位放入b中
uint8_t c = ((a<<4)^seed)&240;//运用c=a^b;a=c^b;的原理,将原来的数据还原,并
清空无效的数据位
unit8_t d = (((((uint32_t)b)<<8)^seed)>>12)&15;//运用c=a^b;a=c^b;的原理,
将原来的数据还原,并清空无效的 数据位
out[i] = c | d;//将还原后的高四位和低四位相或
seed = ((seed ^ in[i]) * 48475829 + in[i]);//保证编码和解码用的一个密码;(编码 中的out[i]就是解码中的in[i])
}
}
int main()
{
const uint8_t buf1[] = {0xf2, 0x60, 0xab, 0xa6, 0xa5, 0xbe, 0xc9, 0x39, 0x15, 0xdf, 0xe6, 0x06, 0x47, 0x02, 0xa6, 0xfb, 0xcb, 0x60, 0x76, 0xf0, 0x08, 0xfb, 0x92, 0xe4, 0xaf, 0x4d, 0x22, 0x7a, 0x6a, 0xfe, 0xcf, 0x85, 0x25, 0x25, 0xfc, 0x31, 0xe4, 0xa2, 0x10, 0x2b, 0xd8, 0x50, 0x8f, 0x7f, 0x3f, 0xf0, 0xde, 0xee, 0x30, 0x96, 0x24, 0x7f, 0x9f, 0x56, 0x06, 0x21, 0xd3, };
uint8_t buf2[100] = {};
const uint32_t password = 0xee90efe9u;
const size_t len = sizeof(buf1);
decode(buf1, buf2, password, len);
printf("%s\n", buf2);
}