平台服务端注册码生成主界面如下所示:
图2 注册文件管理器
通过“注册文件管理器”,我们就可以根据用户提供的信息来生成软件的注册文件。
客户端的注册主要就是根据我们提供的注册文件与公钥,来验证注册文件是否为当前客户的有效注册文件,如果有效,注册成功,无效则注册失败!客户端注册功能设计参考如下所示:
图3 平台注册
用户单击“注册”按钮,成功注册提示:
图4注册成功
服务端注册码生成核心代码:
生成公/私钥文件:
1 private void btnGenerateKey_Click(object sender, EventArgs e) 2 { 3 if (MessageBox.Show("确定生成生成公/私钥对吗(是/否)?", "询问信息", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) 4 == System.Windows.Forms.DialogResult.Cancel) 5 { 6 return; 7 } 8 9 RSACryptoServiceProvider crypt = new RSACryptoServiceProvider(); 10 11 string publicKey = crypt.ToXmlString(true); 12 string privateKey = crypt.ToXmlString(false); 13 crypt.Clear(); 14 15 //生成公钥 16 using (StreamWriter sw = new StreamWriter(KeyPath + "RDIFrameworkkey.key", false, UTF8Encoding.UTF8)) 17 { 18 sw.Write(SecretHelper.AESEncrypt(publicKey)); 19 sw.Flush(); 20 } 21 22 //生成私钥 23 using (StreamWriter sw = new StreamWriter(KeyPath + "RDIFrameworkPrivateKey.key", false, UTF8Encoding.UTF8)) 24 { 25 sw.Write(SecretHelper.AESEncrypt(privateKey)); 26 sw.Flush(); 27 } 28 29 MessageBox.Show("成功生成公/私钥对!","提示信息",MessageBoxButtons.OK,MessageBoxIcon.Information); 30 }
二、 生成注册文件:
1 private void btnGenerateRegisterFile_Click(object sender, EventArgs e) 2 { 3 if (string.IsNullOrEmpty(txtUserEmail.Text.Trim())) 4 { 5 MessageBox.Show("用户邮箱不能为空!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning); 6 txtUserEmail.Focus(); 7 return; 8 } 9 else 10 { 11 if (!RegexValidatorHelper.IsMatch(txtUserEmail.Text.Trim(), Pattern.EMAIL)) 12 { 13 MessageBox.Show("邮箱格式不正确!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning); 14 txtUserEmail.SelectAll(); 15 return; 16 } 17 } 18 19 if (string.IsNullOrEmpty(txtCPUSerialNo.Text.Trim())) 20 { 21 MessageBox.Show("CPU序列号不能为空!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning); 22 return; 23 } 24 25 if (!string.IsNullOrEmpty(txtUseLimited.Text.Trim())) 26 { 27 if (!RegexValidatorHelper.IsMatch(txtUseLimited.Text.Trim(), Pattern.INTEGER)) 28 { 29 MessageBox.Show("使用次数应该为数值型!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning); 30 txtUseLimited.SelectAll(); 31 return; 32 } 33 } 34 35 //读取私钥 36 StreamReader sr = new StreamReader(KeyPath + "RDIFrameworkPrivateKey.key", UTF8Encoding.UTF8); 37 string keypair = sr.ReadToEnd(); 38 sr.Close(); 39 40 //用私钥参数初始化RSACryptoServiceProvider类的实例crypt。 41 RSACryptoServiceProvider crypt = new RSACryptoServiceProvider(); 42 43 crypt.FromXmlString(SecretHelper.AESDecrypt(keypair)); 44 45 UTF8Encoding enc = new UTF8Encoding(); 46 47 string trialTime = "30";//试用次数(默认:30数,0:表示永久) 48 if (!string.IsNullOrEmpty(txtUseLimited.Text.Trim())) 49 { 50 trialTime = txtUseLimited.Text.Trim(); 51 } 52 string regInfo = txtUserEmail.Text.Trim() + ";" + txtMAC.Text.Trim() + ";" + txtCPUSerialNo.Text.Trim() + ";" + trialTime; 53 54 byte[] bytes = enc.GetBytes(regInfo);//格式:邮箱地址;MAC;CPU序列号;试用时间 55 //对用户信息加密 56 bytes = crypt.Encrypt(bytes, false); 57 58 //生成注册数据,对二进制字节进行Base64编码,但采用注册文件的形式的进修也可以不做此转化。 59 string encrytText = System.Convert.ToBase64String(bytes, 0, bytes.Length); 60 61 //将注册码写入文件 62 using (StreamWriter sw = new StreamWriter(KeyPath + "RDIFramework_reg_file.lic", false, UTF8Encoding.UTF8)) 63 { 64 sw.Write(encrytText); 65 sw.Flush(); 66 } 67 68 MessageBox.Show("注册文件:RDIFramework_reg_file.lic生成成功!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information); 69 }
三、 验证注册文件:
1 private void btnCheckRegistr_Click(object sender, EventArgs e) 2 { 3 //读取注册数据文件 4 StreamReader sr = new StreamReader(KeyPath + "RDIFramework_reg_file.lic", UTF8Encoding.UTF8); 5 string encrytText = sr.ReadToEnd(); 6 sr.Close(); 7 8 9 //读取公钥 10 StreamReader srPublickey = new StreamReader(KeyPath + "RDIFrameworkkey.key", UTF8Encoding.UTF8); 11 string publicKey = srPublickey.ReadToEnd(); 12 srPublickey.Close(); 13 14 //用公钥初化始RSACryptoServiceProvider类实例crypt。 15 RSACryptoServiceProvider crypt = new RSACryptoServiceProvider(); 16 crypt.FromXmlString(SecretHelper.AESDecrypt(publicKey)); 17 UTF8Encoding enc = new UTF8Encoding(); 18 byte[] decryptByte; 19 try 20 { 21 byte[] newBytes; 22 newBytes = System.Convert.FromBase64CharArray(encrytText.ToCharArray(), 0, encrytText.Length); 23 decryptByte = crypt.Decrypt(newBytes, false); 24 string decrypttext = enc.GetString(decryptByte); 25 // 26 //TODO:在此处添加验证逻辑 27 // 28 MessageBox.Show(decrypttext); 29 } 30 catch(Exception ex) 31 { 32 MessageBox.Show(ex.Message); 33 } 34 }