SyncWebServer/Tool.hpp

74 lines
1.6 KiB
C++

#pragma once
#include <string>
int ByteLittleToInt(unsigned char* Count)
{
int int1 = Count[0] & 0xff;
int int2 = (Count[1] & 0xff) << 8;
int int3 = (Count[2] & 0xff) << 16;
int int4 = (Count[3] & 0xff) << 24;
return int1 | int2 | int3 | int4;
}
unsigned char* IntToByteLittle(int Count)
{
unsigned char* b = new unsigned char[4];
b[0] = (Count & 0xff);
b[1] = (Count & 0xff00) >> 8;
b[2] = (Count & 0xff0000) >> 16;
b[3] = (Count & 0xff000000) >> 24;
return b;
}
# define skey {8,1,2,5,4}
//单个字符异或运算
char MakecodeChar(char c, int key, int key2) {
c = (((c + key) ^ key2) ^ key) + 1;
//std::cout << c << std::endl;
return c;
}
//单个字符解密
char CutcodeChar(char c, int key, int key2) {
return (((c - 1) ^ key) ^ key2) - key;
}
//加密
void Makecode(char* pstr, int* pkey) {
int len = strlen(pstr);//获取长度
for (int i = 0; i < len; i++)
{
*(pstr + i) = MakecodeChar(*(pstr + i), pkey[i % 5], pkey[(i + 18) % 5]);
}
}
//解密
void Cutecode(char* pstr, int* pkey) {
int len = strlen(pstr);
for (int i = 0; i < len; i++)
*(pstr + i) = CutcodeChar(*(pstr + i), pkey[i % 5], pkey[(i + 18) % 5]);
}
unsigned char* StringToByte(std::string str)
{
char* cstr = (char*)str.c_str();
int Count = strlen(cstr);
unsigned char* temp = IntToByteLittle(Count);
unsigned char* buffer = new unsigned char[Count + 4];
for (size_t i = 0; i < 4; i++)
{
buffer[i] = temp[i];
}
delete[] temp;
int key[] = skey;
Makecode(cstr, key);
for (size_t z = 4; z < Count + 4; z++)
{
buffer[z] = cstr[z - 4];
}
return buffer;
}