您的当前位置:首页正文

License控制解决方案

2024-06-14 来源:客趣旅游网
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   

因篇幅问题不能全部显示,请点此查看更多更全内容