private void btnCreate_Click(object sender, EventArgs e) { string fileName = txtFileName.Text.Trim(); if (string.IsNullOrEmpty(fileName)) { MessageBox.Show("请输入钥匙名称!"); return; } string directoryPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Keys", fileName); if (!Directory.Exists(directoryPath)) { Directory.CreateDirectory(directoryPath); } string publicKeyPath = Path.Combine(directoryPath, "PublicKey"); string privateKeyPath = Path.Combine(directoryPath, "PrivateKey"); RSAHelper rsaHelper = new RSAHelper(); var keys = rsaHelper.GetKey(); txtCreateLog.Clear(); File.WriteAllText(publicKeyPath, keys.PublicKey); txtCreateLog.AppendText("公钥路径\r\n"); txtCreateLog.AppendText(publicKeyPath + "\r\n"); File.WriteAllText(privateKeyPath, keys.PrivateKey); txtCreateLog.AppendText("私钥路径\r\n"); txtCreateLog.AppendText(privateKeyPath + "\r\n"); }
private void btnCreateKey_Click(object sender, EventArgs e) { string stationNo = txtStationNo.Text; string computerNo = txtComputerNo.Text; string maxDate = dtMaxDate.Value.ToString("yyyy/MM/dd"); string ming = $"{stationNo}-{computerNo}-{maxDate}"; if (string.IsNullOrEmpty(computerNo) || string.IsNullOrEmpty(stationNo)) { MessageBox.Show("请完善注册信息!"); return; } string keyPath = txtKeyFile.Text.Trim(); if (!File.Exists(keyPath)) { MessageBox.Show("选择私钥路径"); return; } string privateKey = File.ReadAllText(keyPath); RSAHelper rsaHelper = new RSAHelper(); string val = rsaHelper.EncryptByPrivateKey(ming, privateKey); txtKeyVal.Text = val; File.WriteAllText(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Keys", computerNo + ".txt"), val); }
public bool Validate(string publicKey, string val, bool valifyDate, out string msg) { msg = "注册码不正确"; bool succ = false; bool validateComputerNo = false; bool validateDate = !valifyDate; try { RSAHelper rsaHelper = new RSAHelper(); string ming = rsaHelper.DecryptByPublicKey(val, publicKey); string[] vals = ming.Split('-'); if (vals.Length == 3) { StationNo = vals[0]; ComputerNo = vals[1]; MaxDate = vals[2]; SoftReg softReg = new SoftReg(); string valifyNo = softReg.GetMNum(); validateComputerNo = valifyNo.Equals(ComputerNo); if (!validateComputerNo) { msg = "机器码不匹配"; } if (valifyDate) { DateTime d; if (DateTime.TryParse(MaxDate, out d)) { validateDate = d < DateTime.Now; EndDate = d; if (validateDate) { msg = "注册码已过期"; } } } if (!valifyDate) { succ = validateComputerNo; } else { succ = validateComputerNo && !validateDate; } } } catch (Exception ex) { succ = false; } return(succ); }