License控制解决方案
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
License控制解决⽅案
当我们写完⼀个软件以后⼀般都会牵扯到软件控制,那么控制版本的原理是什么呢?其实就是在程序中添加了⼀段经过⾃⼰编写算法(这个算法可以是简单的公式运算,也可以是复杂的结合硬件的绑定⽅式),将形成的序列号注册到我们的注册表中.每当程序运⾏的时候都去读取注册表信息并验证⼀把.
下⾯是⼀个简单的License⽣成器代码:
public partial class LicenseCreator : Form
{
public LicenseCreator()
{
InitializeComponent();
}
private void btnLincenseCreator_Click(object sender, EventArgs e)
{
string licenseKey = GetLicenseKey(txtUsername.Text);
txtLincense.Text = licenseKey;
}
//通过⾃定义算法返回序列号
private string GetLicenseKey(string username)
{
int breakCount = 8;
//定制算法可根据个⼈修改
username =
System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(
Math.Abs(~System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(username, "md5").GetHashCode() ^ 0x2010 << 3)
.ToString(), "sha1");
StringBuilder result = new StringBuilder();
for (int i = 0; i < username.Length; i++)
{
result.Append(username.Substring(i, 1));
if ((i + 1) % breakCount == 0)
{
result.Append("-");
}
}
return result.ToString().TrimEnd('-');
}
//注册信息
private void btnRegedit_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(txtUsername.Text) || string.IsNullOrEmpty(txtLincense.Text))
{
MessageBox.Show("请⽣成Lincese后重新注册");
return;
}
string regeditKey = @"Licenses\DFF31A6A-A5C2-48A7-B4D9-3C1151676FC8\";
RegistryKey classesRootKey = Registry.ClassesRoot;
RegistryKey licenseKey = classesRootKey.CreateSubKey(regeditKey);
licenseKey.SetValue("UserName", txtUsername.Text);
licenseKey.SetValue("Key", txtLincense.Text);
MessageBox.Show("注册成功!");
}
private void btnClear_Click(object sender, EventArgs e)
{
txtUsername.Text = string.Empty;
txtLincense.Text = string.Empty;
txtUsername.Focus();
}
LicenseCreator。