C#软件注册码的实现代码
第一步。根据卷标,cpu序列号,生成机器码
// 取得设备硬盘的卷标号
public static string getdiskvolumeserialnumber()
{
managementclass mc = new managementclass(“win32_networkadapterconfiguration”);
managementobject disk = new managementobject(“win32_logicaldisk.deviceid=”d:”");
disk.get();
return disk.getpropertyvalue(“volumeserialnumber”).tostring();
}
//获得cpu的序列号
public static string getcpu()
{
string strcpu = null;
managementclass mycpu = new managementclass(“win32_processor”);
managementobjectcollection mycpuconnection = mycpu.getinstances();
foreach (managementobject myobject in mycpuconnection)
{
strcpu = myobject.properties["processorid"].value.tostring();
break;
}
return strcpu;
}
//生成机器码
public static string getmnum()
{
string strnum = getcpu() + getdiskvolumeserialnumber();//获得24位cpu和硬盘序列号
string strmnum = strnum.substring(0, 24);//从生成的字符串中取出前24个字符做为机器码
return strmnum;
}
public static int[] intcode = new int[127];//存储密钥
public static int[] intnumber = new int[25];//存机器码的ascii值
public static char[] charcode = new char[25];//存储机器码字
public static void setintcode()//给数组赋值小于10的数
{
for (int i = 1; i < intcode.length; i++)
{
intcode[i] = i % 9;
}
}
第二步。根据机器码 生成注册码
//生成注册码
public static string getrnum()
{
setintcode();//初始化127位数组
for (int i = 1; i < charcode.length; i++)//把机器码存入数组中
{
charcode[i] = convert.tochar(getmnum().substring(i – 1, 1));
}
for (int j = 1; j < intnumber.length; j++)//把字符的ascii值存入一个整数组中。
{
intnumber[j] = intcode[convert.toint32(charcode[j])] + convert.toint32(charcode[j]);
}
string strasciiname = “”;//用于存储注册码
for (int j = 1; j < intnumber.length; j++)
{
if (intnumber[j] >= 48 && intnumber[j] <= 57)//判断字符ascii值是否0-9之间
{
strasciiname += convert.tochar(intnumber[j]).tostring();
}
else if (intnumber[j] >= 65 && intnumber[j] <= 90)//判断字符ascii值是否a-z之间
{
strasciiname += convert.tochar(intnumber[j]).tostring();
}
else if (intnumber[j] >= 97 && intnumber[j] <= 122)//判断字符ascii值是否a-z之间
{
strasciiname += convert.tochar(intnumber[j]).tostring();
}
else//判断字符ascii值不在以上范围内
{
if (intnumber[j] > 122)//判断字符ascii值是否大于z
{
strasciiname += convert.tochar(intnumber[j] – 10).tostring();
}
else
{
strasciiname += convert.tochar(intnumber[j] – 9).tostring();
}
}
}
return strasciiname;
}
第三步。检查注册状况,若没有注册,可自定义试用
/// <summary>
/// 检查注册
/// </summary>
private void checkregist()
{
this.btn_reg.enabled = true;
registrykey retkey = microsoft.win32.registry.currentuser.opensubkey(“software”, true).createsubkey(“wxk”).createsubkey(“wxk.ini”);
foreach (string strrnum in retkey.getsubkeynames())//判断是否注册
{
if (strrnum == clstools.getrnum())
{
thcontrol(true);
return;
}
}
thcontrol(false);
thread th2 = new thread(new threadstart(thcheckregist2));
th2.start();
}
}
/// <summary>
/// 验证试用次数
/// </summary>
private static void thcheckregist2()
{
messagebox.show(“您现在使用的是试用版,该软件可以免费试用3000000次!”, “提示”, messageboxbuttons.ok, messageboxicon.information);
int32 tlong;
try
{
tlong = (int32)registry.getvalue(“hkey_local_machine\software\angel”, “usetimes”, 0);
messagebox.show(“感谢您已使用了” + tlong + “次”, “提示”, messageboxbuttons.ok, messageboxicon.information);
}
catch
{
registry.setvalue(“hkey_local_machine\software\angel”, “usetimes”, 0, registryvaluekind.dword);
messagebox.show(“欢迎新用户使用本软件”, “提示”, messageboxbuttons.ok, messageboxicon.information);
}
tlong = (int32)registry.getvalue(“hkey_local_machine\software\angel”, “usetimes”, 0);
if (tlong < 3000000)
{
int times = tlong + 1;
registry.setvalue(“hkey_local_machine\software\angel”, “usetimes”, times);
}
else
{
messagebox.show(“试用次数已到”, “警告”, messageboxbuttons.ok, messageboxicon.warning);
application.exit();
}
}