C# 生成软件注册码
今天早上,花了一个早上弄了个生成机器码和注册码的Demo,通过生成的注册码里面包含时间信息,保证了注册码在使用后的指定时间后失效
由于数学不行所以写的比较简单,ok 上代码吧
- 产生机器码的原理很简单,基本上都是取设备信息之后加密
/// <summary>
/// 取本机机器码
/// </summary>
public static string GetMachineCode()
{
//CPU信息
string cpuId = DeviceHelper.GetCpuID();
//磁盘信息
string diskId = DeviceHelper.GetDiskID();
//网卡信息
string MacAddress = DeviceHelper.GetMacAddress();
string m1 = GetMD5(cpuId + typeof(string).ToString());
string m2 = GetMD5(diskId + typeof(int).ToString());
string m3 = GetMD5(MacAddress+typeof(double).ToString());
string code1 = GetNum(m1, 8);
string code2 = GetNum(m2, 8);
string code3 = GetNum(m3, 8);
return code1 + code2 + code3;
}
- 产生注册码
/// <summary>
/// 根据机器码产生注册码
/// </summary>
/// <param name="machineCode">机器码</param>
/// <param name="overTime">到期时间</param>
/// <returns></returns>
public static string CreateRegisterCode(string machineCode,DateTime overTime)
{
int year = int.Parse(overTime.Year.ToString().Substring(2))+33;
int month = overTime.Month+21;
int day = overTime.Day+54;
int section = machineCode.Length / 4;
string reg = "";
int n = 1597;
for (int i = 0; i < section; i++)
{
int sec = int.Parse(machineCode.Substring(i*4,4));
int resu = sec + n;
if (resu >= 10000)
{
resu = sec - 1597;
}
reg += resu ;
n = n + 1597;
}
//插入年月日信息
reg = InsertNum(reg, year, 0, 8, 4, 6, 7, 1, 3, 2, 5, 9);
reg = InsertNum(reg, month, 0, 6, 9, 7, 3, 8, 4, 1, 2, 5);
reg = InsertNum(reg, day, 0,1, 2, 5, 6,7, 3, 8, 9, 4);
return reg.ToString();
}
/// <summary>
/// 在指定数字后面插入内容
/// </summary>
/// <param name="str"></param>
/// <param name="num"></param>
/// <param name="index"></param>
/// <param name="pmc"></param>
/// <returns></returns>
static string InsertNum(string str,int num,int index,params int[] pmc)
{
int posi = str.IndexOf(pmc[index].ToString());
if (posi <= -1)
return InsertNum(str, num, index + 1, pmc);
return str.Insert(posi, num.ToString());
}
- 验证注册码
/// <summary>
/// 检查注册码
/// </summary>
/// <param name="registerCode"></param>
/// <param name="overTime"></param>
/// <returns></returns>
public static bool CheckRegister( string registerCode,ref DateTime overTime)
{
try
{
string machineCode = GetMachineCode();
//提取年月日
int day = int.Parse(ExtractNum(ref registerCode, 0, 1, 2, 5, 6, 7, 3, 8, 9, 4));
int month = int.Parse(ExtractNum(ref registerCode, 0, 6, 9, 7, 3, 8, 4, 1, 2, 5));
int year = int.Parse(ExtractNum(ref registerCode, 0, 8, 4, 6, 7, 1, 3, 2, 5, 9));
day -= 54;
month -= 21;
year -= 33;
overTime = new DateTime(year, month, day);
//核对注册码
int section = machineCode.Length / 4;
int n = 1597;
string reg = "";
for (int i = 0; i < section; i++)
{
int sec = int.Parse(machineCode.Substring(i * 4, 4));
int resu = sec + n;
if (resu >= 10000)
{
resu = sec - 1597;
}
reg += resu;
n = n + 1597;
}
return registerCode == reg;
}
catch {
return false;
}
}
/// <summary>
/// 提取数字
/// </summary>
/// <param name="str"></param>
/// <param name="index"></param>
/// <param name="pmc"></param>
/// <returns></returns>
static string ExtractNum(ref string str, int index, params int[] pmc)
{
int posi = str.IndexOf(pmc[index].ToString());
if (posi <= -1)
return ExtractNum(ref str, index + 1, pmc);
string resu = str.Substring(posi - 2, 2);
str = str.Remove(posi - 2, 2);
return resu;
}
- 调用实例
//取机器码
string mCode = RegInfo.GetMachineCode();
//产生注册码
string regCode = RegInfo.CreateRegisterCode(mCode, DateTime.Now);
DateTime time = DateTime.Now;
//验证注册码
bool resu = RegInfo.CheckRegister(regCode+"1", ref time);
代码下载
链接:http://pan.baidu.com/s/1bpfFu3d 密码:lth3