//source.h
#pragma once
//_string : 加密之后的偏移量
//ip : 服务器ip
//port : 服务器端口
//random_base : 保存加密基数
//return : 返回0代表成功,-1代表失败
int offset_encrypt(char *_string, const char *ip, const int port, int &random_base);
//_string : 待解密的偏移量
//ip : 解密后获取的服务器ip
//port : 解密后获取的服务器端口
//random_base : 随机生成的加密基数
//return : 返回0代表成功,-1代表失败
int offset_decrypt(const char * _string, char *ip, int &port, int &random_base);
//_string : 加密之后的注册码id
//random_base : offset_encrypt中生成,offset random_base license_id 三者为一组数据
//source : 待加密的注册码源数据
//return : 返回0代表成功,-1代表失败
int license_id_encrypt(char *_string,const int random_base,const char *source);
//_string : 待解密的注册码id
//license_id : 保存解密后的注册码id
//random_base : 加密基数
//return : 返回0代表成功,-1代表失败
int license_id_decrypt(char *_string, char *license_id,int random_base);
//_string : 待解密的注册码字符串包含id和offset两部分,前32位为id
//license_id : 存储注册码id原始值
//ip : 服务器ip
//port : 服务器端口
//return : 返回0代表成功,-1代表失败
int get_all_register_info(char *_string, char *license_id, char *ip, int &port);
//end source.h
###################################################################################
//source.cpp
#include "source.h"
#include <time.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <math.h>
//ip:xxx.xxx.xxx.xxx port xxxxx
int offset_encrypt(char *_string, const char *ip, const int port,int &random_base)
{
//此处使用的是伪随机生成函数,若需随机性更高的随机数 需使用CryptGenRandom 或/dev/random生成
if (!ip||!_string||strlen(ip) > 18 || port > 65535 || port < 0)return -1;
static int seed = 0;
seed++;
srand(time(0)+seed*seed);
random_base = rand() % 15;
//ip处理
int temp = random_base % 3;
char ip_str[18] = { 0 };
switch (temp)
{
case 0:
for (size_t i = 0; i < strlen(ip); i++)
{
if (i % 3 == 1 && i + 1 < strlen(ip))
{
ip_str[i] = ip[i + 1] + 1+random_base;
ip_str[i + 1] = ip[i] - 1;
i++;
}
else
{
ip_str[i] = ip[i];
}
}
ip_str[strlen(ip)] = '\0';
break;
case 1:
for (size_t i = 0; i < strlen(ip); i++)
{
if (i % 3 == 0 && i + 2 < strlen(ip))
{
ip_str[i] = ip[i] + ip[i + 2];
ip_str[i + 2] = ip[i + 2] + ip[i + 1];
continue;
}
if (i % 3 == 2)continue;
ip_str[i] = ip[i] + random_base;
}
ip_str[strlen(ip)] = '\0';
break;
case 2:
for (size_t i = 0; i < strlen(ip); i++)
{
if (i % 3 == 0 && i+2 < strlen(ip))
{
ip_str[i] = ip[i];
ip_str[i+1] = ip[i+1]+i+1;
ip_str[i + 2] = ip[i + 2] + i+ random_base;
i = i + 2;
continue;
}
ip_str[i] = ip[i];
}
ip_str[strlen(ip)] = '\0';
default:
break;
}
//端口处理
int num = port % 256;
int den = port / 256;
char port_str[8] = { 0 };
sprintf(port_str,"%d%d", num, den);
//printf("port_str:%s %d %d \n", port_str,num,den);
//字符串拼接7-8bit存储余数部分5-6bit存储整数商部分1-4bit存储加密基数
unsigned char num_temp = 0;
if (num>=100)
{
num_temp = 3;
}
else if (num >=10)
{
num_temp = 2;
}
else
{
num_temp = 1;
}
unsigned char den_temp = strlen(port_str)-num_temp;
unsigned char base_temp = random_base | (den_temp << 4)|(num_temp <<6);
sprintf(_string, "%s%s%c", ip_str, port_str, base_temp);
//printf("base_temp:%c %x\n", base_temp,base_temp);
//printf("%s:%d %x %d\n", _string, random_base, _string[strlen(_string) - 1], _string[strlen(_string) - 1]);
return 0;
}
int offset_decrypt(const char * _string, char *ip, int &port, int &random_base)
{
if (!_string || !ip)return -1;
//计算加密基数
int string_len = strlen(_string);
unsigned char base_temp = _string[strlen(_string) - 1];
unsigned char num_temp = (_string[strlen(_string) - 1] & 0xC0)>>6;
unsigned char den_temp= (_string[strlen(_string) - 1] & 0x30)>>4;
random_base = (_string[strlen(_string) - 1] & 0xf);
//端口解密
int den = 0;
int num = 0;
for (size_t i = 0; i < den_temp; i++)
{
den += (_string[string_len - 2 - i]-'0') * pow(10,i);
//printf("%c\n", _string[string_len - 2 - i]);
}
for (size_t i = 0; i < num_temp; i++)
{
num += (_string[string_len - 2-den_temp - i] - '0') * pow(10, i);
//printf("%c\n", _string[string_len - 2 - den_temp - i]);
}
port = den * 256 + num;
//ip解密
int temp = random_base % 3;
size_t ip_len = string_len - 1 - num_temp - den_temp;
switch (temp)
{
case 0:
for (size_t i = 0; i < ip_len; ++i)
{
if (i % 3 == 1 && i + 1 < ip_len)
{
ip[i+1] = _string[i]-1- random_base;
ip[i] = _string[i+1] + 1;
i++;
}
else
{
ip[i] = _string[i];
}
}
ip[ip_len] = '\0';
break;
case 1:
for (size_t i = 0; i < ip_len; i++)
{
if (i % 3 == 0 && i + 2 < ip_len)
{
ip[i + 1] = _string[i + 1] - random_base;
ip[i + 2] = _string[i + 2] - ip[i + 1];
ip[i] = _string[i] - ip[i + 2];
i += 2;
continue;
}
ip[i]=_string[ip_len]-random_base;
}
ip[ip_len] = '\0';
break;
case 2:
for (size_t i = 0; i < ip_len; i++)
{
if (i % 3 == 0 && i + 2 <ip_len)
{
ip[i] = _string[i];
ip[i + 1] = _string[i + 1] - i - 1;
ip[i + 2] = _string[i + 2] - i - random_base;
i = i + 2;
continue;
}
ip[i] = _string[i];
}
ip[ip_len] = '\0';
default:
break;
}
printf("ip:%s port:%d base:%d %d %d\n", ip, port, random_base,den,num);
return 0;
}
int license_id_encrypt(char *_string, const int random_base, const char *source)
{
//注册码加密
if (!_string || !source || strlen(source) > 32 || random_base > 15 || random_base < 0)return -1;
size_t temp = random_base % 8;
int length = strlen(source);
unsigned char low_4bit = 0;
unsigned char high_4bit = 0;
switch (temp)
{
case 0:
for (size_t i = 0; i < length; i++)
{
if (i%8==0&&i%3!=0)
{
low_4bit = source[i] & 0xf;
high_4bit = source[i] & 0xf0;
high_4bit += (i % 5)<<4;
_string[i] = low_4bit<<4 | high_4bit>>4;
}
//将第三位反转
else if (i%3==0)
{
_string[i] = source[i] ^ (1 << 3);
}
//第四位反转
else
{
_string[i] = source[i] ^ (1 << 4);
}
}
_string[length] = '\0';
break;
case 1:
for (size_t i = 0; i < length; i++)
{
if (i % 8 == 1 && i%3!=0 )
{
low_4bit = source[i] & 0xf;
high_4bit = source[i] & 0xf0;
high_4bit += (i % 5) << 4;
_string[i] = low_4bit<<4 | high_4bit>>4;
}
//第三位取反
else if (i % 3 == 0)
{
_string[i] = source[i] ^ (1 << 3);
}
//第五位取反
else
{
_string[i] = source[i] ^ (1 << 5);
}
}
_string[length - 1] = _string[length - 1] + 1;
_string[length] = '\0';
break;
case 2:
for (size_t i = 0; i < length; i++)
{
if (i % 8 == 2 && i % 3 != 0)
{
low_4bit = source[i] & 0xf;
high_4bit = (source[i] & 0xf0);
high_4bit += (i % 5) << 4;
_string[i] = low_4bit << 4 | high_4bit >> 4;
}
//第三位取反
else if (i % 3 == 0)
{
_string[i] = (source[i] ^ (1 << 3))+2;
}
//
else
{
_string[i] = source[i] ^ (1 << temp);
}
}
_string[length - 2] = _string[length - 2] + 2;
_string[length] = '\0';
break;
case 3:
for (size_t i = 0; i < length; i++)
{
if (i % 8 == 3 && i % 3 != 0)
{
low_4bit = source[i] & 0xf;
high_4bit = (source[i] & 0xf0);
high_4bit += (i % 5) << 4;
_string[i] = low_4bit << 4 | high_4bit >> 4;
}
//第三位取反
else if (i % 3 == 0)
{
_string[i] = (source[i] ^ (1 << 3)) + 3;
}
else
{
_string[i] = source[i] ;
}
}
_string[length - temp] = _string[length - temp] + temp;
_string[length] = '\0';
break;
case 4:
for (size_t i = 0; i < length; i++)
{
if (i % 8 == 4 && i % 3 != 0)
{
low_4bit = source[i] & 0xf;
high_4bit = (source[i] & 0xf0);
high_4bit += (i % 5) << 4;
_string[i] = low_4bit << 4 | high_4bit >> 4;
}
//第三位取反
else if (i % 3 == 0)
{
_string[i] = (source[i] ^ (1 << 3)) + 4;
}
else
{
_string[i] = source[i] ^ (1 << temp);
}
}
_string[length - temp] = _string[length - temp] + temp;
_string[length] = '\0';
break;
case 5:
for (size_t i = 0; i < length; i++)
{
if (i % 8 == 5 && i % 3 != 0)
{
low_4bit = source[i] & 0xf;
high_4bit = (source[i] & 0xf0);
high_4bit += (i % 5) << 4;
_string[i] = low_4bit << 4 | high_4bit >> 4;
}
//第三位取反
else if (i % 3 == 0)
{
_string[i] = (source[i] ^ (1 << 3)) + 5;
}
else
{
_string[i] = source[i] ^ (1 << temp);
}
}
_string[length - temp] = _string[length - temp] + temp;
_string[length] = '\0';
break;
case 6:
for (size_t i = 0; i < length; i++)
{
if (i % 8 == 6 && i % 3 != 0)
{
low_4bit = source[i] & 0xf;
high_4bit = (source[i] & 0xf0);
high_4bit += (i % 5) << 4;
_string[i] = low_4bit << 4 | high_4bit >> 4;
}
//第三位取反
else if (i % 3 == 0)
{
_string[i] = (source[i] ^ (1 << 3)) + 6;
}
else
{
_string[i] = source[i] ^ (1 << temp);
}
}
_string[length - temp] = _string[length - temp] + temp;
_string[length] = '\0';
break;
case 7:
for (size_t i = 0; i < length; i++)
{
if (i % 8 == 7 && i % 3 != 0)
{
low_4bit = source[i] & 0xf;
high_4bit = (source[i] & 0xf0);
high_4bit += (i % 5) << 4;
_string[i] = low_4bit << 4 | high_4bit >> 4;
}
//第三位取反
else if (i % 3 == 0)
{
_string[i] = (source[i] ^ (1 << 3)) + 7;
}
else
{
_string[i] = source[i] ^ (1 << temp);
}
}
_string[length - temp] = _string[length - temp] + temp;
_string[length] = '\0';
break;
default:
break;
}
printf("license_id:%s\n", _string);
return 0;
}
int license_id_decrypt(char *_string, char *license_id, int random_base)
{
if (!_string || !license_id || random_base < 0 || random_base>15)return -1;
size_t temp = random_base % 8;
int length = strlen(_string);
unsigned char low_4bit = 0;
unsigned char high_4bit = 0;
switch (temp)
{
case 0:
for (size_t i = 0; i < length; i++)
{
if (i % 8 == 0 && i % 3 != 0)
{
low_4bit = _string[i] & 0xf-i%5;
high_4bit = _string[i] & 0xf0;
license_id[i] = low_4bit << 4 | high_4bit >> 4;
}
else if (i % 3 == 0)
{
license_id[i] = _string[i] ^ (1 << 3);
}
else
{
license_id[i] = _string[i] ^ (1 << 4);
}
}
break;
case 1:
_string[length - 1] = _string[length - 1] - 1;
for (size_t i = 0; i < length; i++)
{
if (i % 8 == 1 && i % 3 != 0)
{
low_4bit = _string[i] & 0xf;
low_4bit -= i % 5;
high_4bit = _string[i] & 0xf0 ;
license_id[i] = low_4bit << 4 | high_4bit >> 4;
}
else if (i % 3 == 0)
{
license_id[i] = _string[i] ^ (1 << 3);
}
else
{
license_id[i] = _string[i] ^ (1 << 5);
}
}
break;
case 2:
_string[length - temp] = _string[length - temp] - temp;
for (size_t i = 0; i < length; i++)
{
if (i % 8 == 2 && i % 3 != 0)
{
low_4bit = (_string[i] & 0xf) - i % 5;
high_4bit = _string[i] & 0xf0;
license_id[i] = low_4bit << 4 | high_4bit >> 4;
}
else if (i % 3 == 0)
{
license_id[i] = (_string[i]-2) ^ (1 << 3);
}
else
{
license_id[i] = _string[i] ^ (1 << temp);
}
}
break;
case 3:
_string[length - temp] = _string[length - temp] - temp;
for (size_t i = 0; i < length; i++)
{
if (i % 8 == 3 && i % 3 != 0)
{
low_4bit = (_string[i] & 0xf) - i % 5;
high_4bit = _string[i] & 0xf0;
license_id[i] = low_4bit << 4 | high_4bit >> 4;
}
else if (i % 3 == 0)
{
license_id[i] = (_string[i] - 3) ^ (1 << 3);
}
else
{
license_id[i] = _string[i] ;
}
}
break;
case 4:
_string[length - temp] = _string[length - temp] - temp;
for (size_t i = 0; i < length; i++)
{
if (i % 8 == 4 && i % 3 != 0)
{
low_4bit = (_string[i] & 0xf) - i % 5;
high_4bit = _string[i] & 0xf0;
license_id[i] = low_4bit << 4 | high_4bit >> 4;
}
else if (i % 3 == 0)
{
license_id[i] = (_string[i] - 4) ^ (1 << 3);
}
else
{
license_id[i] = _string[i] ^ (1 << temp);
}
}
break;
case 5:
_string[length - temp] = _string[length - temp] - temp;
for (size_t i = 0; i < length; i++)
{
if (i % 8 == 5 && i % 3 != 0)
{
low_4bit = (_string[i] & 0xf) - i % 5;
high_4bit = _string[i] & 0xf0;
license_id[i] = low_4bit << 4 | high_4bit >> 4;
}
else if (i % 3 == 0)
{
license_id[i] = (_string[i] - 5) ^ (1 << 3);
}
else
{
license_id[i] = _string[i] ^ (1 << temp);
}
}
break;
case 6:
_string[length - temp] = _string[length - temp] - temp;
for (size_t i = 0; i < length; i++)
{
if (i % 8 == 6 && i % 3 != 0)
{
low_4bit = (_string[i] & 0xf) - i % 5;
high_4bit = _string[i] & 0xf0;
license_id[i] = low_4bit << 4 | high_4bit >> 4;
}
else if (i % 3 == 0)
{
license_id[i] = (_string[i] - 6) ^ (1 << 3);
}
else
{
license_id[i] = _string[i] ^ (1 << temp);
}
}
break;
case 7:
_string[length - temp] = _string[length - temp] - temp;
for (size_t i = 0; i < length; i++)
{
if (i % 8 == 7 && i % 3 != 0)
{
low_4bit = (_string[i] & 0xf) - i % 5;
high_4bit = _string[i] & 0xf0;
license_id[i] = low_4bit << 4 | high_4bit >> 4;
}
else if (i % 3 == 0)
{
license_id[i] = (_string[i] - 7) ^ (1 << 3);
}
else
{
license_id[i] = _string[i] ^ (1 << temp);
}
}
break;
default:
break;
}
license_id[length] = '\0';
printf("license_id:%s\n", license_id);
return 0;
}
int get_all_register_info(char *_string, char *license_id, char *ip, int &port)
{
if (!_string || strlen(_string) < 32||!license_id||!ip)return -1;
char offset[64] = { 0 };
char license_tmp[33] = { 0 };
strncpy(license_tmp, _string, 32);
license_tmp[32] = '\0';
strncpy(offset, _string + 32, 64);
int random_base = 0;
if (offset_decrypt(offset, ip, port, random_base) < 0)return -1;
if (license_id_decrypt(license_tmp, license_id, random_base))return -1;
return 0;
}
//end source.cpp
#####################################################################################
加密解密学习。