private void 軟體驗證ToolStripMenuItem_Click(object sender, EventArgs e) { //驗證此軟體. //1. 檢查是否有驗證資訊. string F1 = 機碼密碼初步加密(GetCPUID() + "_" + GetMacAddress()); string F2 = 密碼轉16位密碼計算(F1); string NewKey = 密碼16位碼再加密(F2); //MessageBox.Show(NewKey); //string MKey = 機碼密碼初步加密(GetCPUID() + "_" + GetMacAddress()); string MKeyPostiion = 驗證機碼存放位置 + "\\" + "MKEY.KEY"; string getMKkey = null; string gets = ""; this._PSKEYCORRECT = false; if(!Directory.Exists(驗證機碼存放位置)) { //沒有驗證過. Form_EnterKey fkey = new Form_EnterKey(this, NewKey, "請連絡相關人員來提供您驗證密碼" + Environment.NewLine + F2); fkey.ShowDialog(); if (!_PSKEYCORRECT) { //失敗. if (_提供服務訊息) { this.發送操作指令("電腦主機'" + Dns.GetHostName() + "'(MAC IP = '" + GetMacAddress() + "', IP(IPV4) = '" + MyIP() + "')有嘗試驗證軟體之活動且驗證已失敗,員工編號為'" + _LoginInUserID + "',員工名稱為'" + _LoginInUserName + "',時間為:" + DateTime.Now.ToString("yyyy/MM/dd HH:mm")); } } else { Directory.CreateDirectory(驗證機碼存放位置); StreamWriter sw1 = new StreamWriter(MKeyPostiion); sw1.WriteLine(NewKey); sw1.Flush(); sw1.Close(); MessageBox.Show("驗證已通過", "驗證軟體", MessageBoxButtons.OK, MessageBoxIcon.Information); this._PSKEYCORRECT = false; if (_提供服務訊息) { this.發送操作指令("電腦主機'" + Dns.GetHostName() + "'(MAC IP = '" + GetMacAddress() + "', IP(IPV4) = '" + MyIP() + "')已成功完成軟體驗證(驗證密碼為'" + F2 + "'),員工編號為'" + _LoginInUserID + "',員工名稱為'" + _LoginInUserName + "',時間為:" + DateTime.Now.ToString("yyyy/MM/dd HH:mm")); } } } else { if(!File.Exists(MKeyPostiion)) { //無此檔案,視為無驗證過. Form_EnterKey fkey = new Form_EnterKey(this, NewKey, "請連絡相關人員來提供您驗證密碼" + Environment.NewLine + F2); fkey.ShowDialog(); if (!_PSKEYCORRECT) { //失敗. if (_提供服務訊息) { this.發送操作指令("電腦主機'" + Dns.GetHostName() + "'(MAC IP = '" + GetMacAddress() + "', IP(IPV4) = '" + MyIP() + "')有嘗試驗證軟體之活動且驗證已失敗,員工編號為'" + _LoginInUserID + "',員工名稱為'" + _LoginInUserName + "',時間為:" + DateTime.Now.ToString("yyyy/MM/dd HH:mm")); } } else { Directory.CreateDirectory(驗證機碼存放位置); StreamWriter sw1 = new StreamWriter(MKeyPostiion); sw1.WriteLine(NewKey); sw1.Flush(); sw1.Close(); this._PSKEYCORRECT = false; if (_提供服務訊息) { this.發送操作指令("電腦主機'" + Dns.GetHostName() + "'(MAC IP = '" + GetMacAddress() + "', IP(IPV4) = '" + MyIP() + "')已成功完成軟體驗證(驗證密碼為'" + F2 + "'),員工編號為'" + _LoginInUserID + "',員工名稱為'" + _LoginInUserName + "',時間為:" + DateTime.Now.ToString("yyyy/MM/dd HH:mm")); } } } else { //讀取檔案的第一行. StreamReader sr1 = new StreamReader(MKeyPostiion); gets = sr1.ReadLine(); getMKkey = gets;//取得轉換完成之密碼.如1234-5678 sr1.Close(); if(getMKkey!=NewKey) { //失敗. //殺掉檔案,顯示失敗. if (_提供服務訊息) { this.發送操作指令("電腦主機'" + Dns.GetHostName() + "'(MAC IP = '" + GetMacAddress() + "', IP(IPV4) = '" + MyIP() + "')有嘗試驗證軟體之活動且驗證因密碼不符合已失敗,員工編號為'" + _LoginInUserID + "',員工名稱為'" + _LoginInUserName + "',時間為:" + DateTime.Now.ToString("yyyy/MM/dd HH:mm")); } MessageBox.Show("您的驗證已過時,請聯絡相關人員提供最新之驗證", "驗證錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error); File.Delete(MKeyPostiion); } else { //沒事. if (_提供服務訊息) { this.發送操作指令("電腦主機'" + Dns.GetHostName() + "'(MAC IP = '" + GetMacAddress() + "', IP(IPV4) = '" + MyIP() + "')有嘗試驗證軟體之活動且驗證通過(驗證密碼為'" + F2 + "'),員工編號為'" + _LoginInUserID + "',員工名稱為'" + _LoginInUserName + "',時間為:" + DateTime.Now.ToString("yyyy/MM/dd HH:mm")); } MessageBox.Show("您的驗證無誤","軟體驗證",MessageBoxButtons.OK,MessageBoxIcon.Information); } } } }
private void 軟體機碼設定ToolStripMenuItem_Click(object sender, EventArgs e) { string MKey = 機碼密碼初步加密(GetCPUID()+ "_" + GetMacAddress());//獲取此機器之數據. string MKeyPostiion =驗證機碼存放位置+ "\\" + "MKEY.KEY"; //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! string MPSPosition = 驗證機碼存放位置 + "\\" + "MPS.KEY"; //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! if (!Directory.Exists(驗證機碼存放位置)) { //採取自動建立此資料夾. Directory.CreateDirectory(驗證機碼存放位置); //無驗證碼. //創建新的驗證資訊. StreamWriter sw1 = new StreamWriter(MKeyPostiion); sw1.WriteLine(MKey); sw1.Flush(); sw1.Close(); string NewPSKey; NewPSKey = 產生隨機碼(); StreamWriter sw2 = new StreamWriter(MPSPosition); sw2.WriteLine(NewPSKey); sw2.Flush(); sw2.Close(); MessageBox.Show("由於您的驗證機碼與驗證密碼尚未設定,已成功建立之" + Environment.NewLine + "您新的驗證密碼(請務必記住此密碼)為" + NewPSKey, "您的驗證機制已重新更新", MessageBoxButtons.OK, MessageBoxIcon.Information); _PSKEYCORRECT = false; return; } //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! FileInfo mk = new FileInfo(MKeyPostiion); FileInfo mps = new FileInfo(MPSPosition); string getMKey = null; string getmpsKey = null; string gets = ""; if(mk.Exists) { //讀取檔案的第一行. StreamReader sr1 = new StreamReader(MKeyPostiion); gets = sr1.ReadLine(); getMKey = gets; sr1.Close(); } if(mps.Exists) { StreamReader sr2 = new StreamReader(MPSPosition); getmpsKey = sr2.ReadLine(); sr2.Close(); } getmpsKey = getmpsKey == "" ? null : getmpsKey; //若空白則視為錯誤. //getmpsKey = 密碼解密(getmpsKey); if(object.Equals(getMKey, null)) { //無機碼. if(object.Equals(getmpsKey,null)) { //無驗證碼. //創建新的驗證資訊. StreamWriter sw1 = new StreamWriter(MKeyPostiion); sw1.WriteLine(MKey); sw1.Flush(); sw1.Close(); string NewPSKey; NewPSKey = 產生隨機碼(); StreamWriter sw2 = new StreamWriter(MPSPosition); sw2.WriteLine(NewPSKey); sw2.Flush(); sw2.Close(); MessageBox.Show("由於您的驗證機碼與驗證密碼尚未設定,已成功建立之" + Environment.NewLine + "您新的驗證密碼(請務必記住此密碼)為" + NewPSKey, "您的驗證機制已重新更新", MessageBoxButtons.OK, MessageBoxIcon.Information); _PSKEYCORRECT = false; return; } else { //有驗證密碼. _PSKEYCORRECT = false;//強制變更為密碼輸入不正確. Form_EnterKey fkeyy = new Form_EnterKey(this, getmpsKey,"您的驗證資訊需更新"); fkeyy.ShowDialog();//強制顯示. if(_PSKEYCORRECT) { //驗證通過. //更新機碼. StreamWriter sw1 = new StreamWriter(MKeyPostiion); sw1.WriteLine(MKey); sw1.Flush(); sw1.Close(); MessageBox.Show("您已成功更新驗證資訊!"); _PSKEYCORRECT = false; return; } else { //驗證取消. MessageBox.Show("您沒有成功驗證!操作取消", "驗證失敗", MessageBoxButtons.OK, MessageBoxIcon.Information); _PSKEYCORRECT = false; return; } } }//有無MKKey. else { //有機碼. if(getMKey!=MKey) { //機碼不正確. //提示機碼錯誤,需要提供驗證才能再次設定. //若驗證密碼失效,則表示整體驗證系統崩潰,則全部重新設定. if(object.Equals(getmpsKey, null)) { //驗證密碼錯誤,視為整體崩潰. string NewPSKey; NewPSKey = 產生隨機碼(); StreamWriter sw1 = new StreamWriter(MKeyPostiion); sw1.WriteLine(MKey); sw1.Flush(); sw1.Close(); StreamWriter sw2 = new StreamWriter(MPSPosition); sw2.WriteLine(NewPSKey); sw2.Flush(); sw2.Close(); MessageBox.Show("由於您的驗證機碼與驗證密碼已經完全毀損,已成功重新建立之" + Environment.NewLine + "您新的驗證密碼(請務必記住此密碼)為" + NewPSKey, "您的驗證機制已重新更新", MessageBoxButtons.OK, MessageBoxIcon.Information); _PSKEYCORRECT = false; return; } else { //有驗證密碼,但機碼不正確. _PSKEYCORRECT = false;//強制變更為密碼輸入不正確. Form_EnterKey fkeyy = new Form_EnterKey(this, getmpsKey,"您的驗證資訊需重新設定"); fkeyy.ShowDialog();//強制顯示. if(_PSKEYCORRECT) { //更新機碼與驗證密碼. string NewPSKey; NewPSKey = 產生隨機碼(); StreamWriter sw1 = new StreamWriter(MKeyPostiion); sw1.WriteLine(MKey); sw1.Flush(); sw1.Close(); StreamWriter sw2 = new StreamWriter(MPSPosition); sw2.WriteLine(NewPSKey); sw2.Flush(); sw2.Close(); MessageBox.Show("您已成功更新驗證資訊!您新的驗證密碼為" + NewPSKey +",請務必記錄下您的驗證密碼","驗證更新",MessageBoxButtons.OK ,MessageBoxIcon.Information); _PSKEYCORRECT = false; return; } else { MessageBox.Show("您沒有成功驗證!操作取消", "驗證失敗", MessageBoxButtons.OK, MessageBoxIcon.Information); _PSKEYCORRECT = false; return; } }//有無驗證密碼. }//得到的機碼不一致. else { //機碼一致,但是密碼可能受損. if(object.Equals(getmpsKey,null)) { //驗證密碼受損. string NewPSKey; NewPSKey = 產生隨機碼(); StreamWriter sw2 = new StreamWriter(MPSPosition); sw2.WriteLine(NewPSKey); sw2.Flush(); sw2.Close(); MessageBox.Show("您已成功更新驗證密碼,您新的驗證密碼是:" + NewPSKey + ",請務必紀錄起來","驗證密碼更新",MessageBoxButtons.OK,MessageBoxIcon.Information); _PSKEYCORRECT = false; return; } else { if(MessageBox.Show("您的驗證資訊已完整,您是否要繼續更新資訊(包含新的驗證密碼)?", "驗證系統", MessageBoxButtons.OKCancel, MessageBoxIcon.Information)==DialogResult.OK) { //更新機碼設定(其實不用更新)與密碼. _PSKEYCORRECT = false;//強制變更為密碼輸入不正確. Form_EnterKey fkeyy = new Form_EnterKey(this, getmpsKey, "您需要輸入舊的驗證密碼"); fkeyy.ShowDialog();//強制顯示. if(_PSKEYCORRECT) { //驗證通過. string NewPSKey; NewPSKey = 產生隨機碼(); StreamWriter sw2 = new StreamWriter(MPSPosition); sw2.WriteLine(NewPSKey); sw2.Flush(); sw2.Close(); MessageBox.Show("您已成功更新驗證密碼,您新的驗證密碼是:" + NewPSKey + ",請務必紀錄起來", "驗證密碼更新", MessageBoxButtons.OK, MessageBoxIcon.Information); _PSKEYCORRECT = false; return; } else { //取消操作. MessageBox.Show("取消操作", "驗證設定更新", MessageBoxButtons.OK, MessageBoxIcon.Information); _PSKEYCORRECT = false; return; } } else { _PSKEYCORRECT = false; return; } } } } //方法: // 1. 取得此電腦mac與CPU資訊. // 或是CPU訊息(但這是好方法嗎 ?). // 2. 隨機產生一組密碼. // 3. 將此資訊透過簡單加密填入新建立之機碼內. //若已有機碼,則需要輸入驗證密碼. //string keyPathString = "SOFTWARE\\VESD_TOOL"; //string pathstring = null; //string BuiltPASSKey = 取得內存密碼();//NULL表示沒有此值. //Microsoft.Win32.RegistryKey startnn=Microsoft.Win32.Registry.LocalMachine; //Microsoft.Win32.RegistryKey programnn=null; //= startnn.OpenSubKey(keyPathString); //try //{ // //startnn = Microsoft.Win32.Registry.LocalMachine; // programnn= startnn.OpenSubKey(keyPathString); //} //catch //{ // //沒有此機碼位置. // //直接建立新的. // string NewPSKey; // NewPSKey = 產生隨機碼(); // MessageBox.Show(NewPSKey.ToString()); // //設定新的機碼,並且設定新的PASS KEY. // Microsoft.Win32.RegistryKey start = Microsoft.Win32.Registry.LocalMachine; // Microsoft.Win32.RegistryKey programName = start.CreateSubKey(keyPathString); // programName.SetValue("PATH_S", 密碼加密(GetCPUID(), GetMacAddress()));//寫入值. // programName.SetValue("PASS_CODE", NewPSKey); // programName.Close(); // MessageBox.Show("由於您的驗證機碼與驗證密碼尚未設定,已成功建立之" + Environment.NewLine + "您新的驗證密碼(請務必記住此密碼)為" + NewPSKey, "您的驗證機制已重新更新", MessageBoxButtons.OK, MessageBoxIcon.Information); // _PSKEYCORRECT = false; // return; //} //if(programnn!=null) //{ // try // { // pathstring = (string)programnn.GetValue("PATH_S");//獲取機碼. // } // catch // { // } // //比對機碼是否正確. // if(pathstring!=MKey) // { // //機碼不一樣. // //提示機碼錯誤,需要提供驗證才能再次設定. // //若驗證密碼失效,則表示整體驗證系統崩潰,則全部重新設定. // if (object.Equals(BuiltPASSKey, null)) // { // string NewPSKey; // NewPSKey = 產生隨機碼(); // //設定新的機碼,並且設定新的PASS KEY. // Microsoft.Win32.RegistryKey start = Microsoft.Win32.Registry.LocalMachine; // Microsoft.Win32.RegistryKey programName = start.OpenSubKey(keyPathString); // programName.SetValue("PATH_S", 密碼加密(GetCPUID(), GetMacAddress()));//寫入值. // programName.SetValue("PASS_CODE",NewPSKey); // programName.Close(); // MessageBox.Show("由於您的驗證機碼與驗證密碼已經完全毀損,已成功重新建立之" + Environment.NewLine + "您新的驗證密碼(請務必記住此密碼)為" + NewPSKey, "您的驗證機制已重新更新", MessageBoxButtons.OK, MessageBoxIcon.Information); // _PSKEYCORRECT = false; // return; // } // else // { // //仍有合理之密碼,需要提供密碼來完成驗證,完成驗證後才能更新機碼,否則視為取消操作. // _PSKEYCORRECT = false;//強制變更為密碼輸入不正確. // Form_EnterKey fkeyy = new Form_EnterKey(this, BuiltPASSKey); // fkeyy.ShowDialog();//強制顯示. // if(_PSKEYCORRECT) // { // //若驗證正確,則更新驗證機碼. // Microsoft.Win32.RegistryKey start = Microsoft.Win32.Registry.LocalMachine; // Microsoft.Win32.RegistryKey programName = start.OpenSubKey(keyPathString); // programName.SetValue("PATH_S", 密碼加密(GetCPUID(), GetMacAddress())); // MessageBox.Show("您已成功更新驗證機碼!"); // _PSKEYCORRECT = false; // return; // } // else // { // //驗證失敗[使用者取消]. // MessageBox.Show("您沒有成功驗證!操作取消", "驗證失敗", MessageBoxButtons.OK, MessageBoxIcon.Information); // _PSKEYCORRECT = false; // return; // } // }//是否驗證密碼仍可以辨識. // }//機碼是否正確. // else // { // //機碼正確,但若密碼損毀,則重新提供. // if(object.Equals(BuiltPASSKey,null)) // { // string NewPSKey; // NewPSKey = 產生隨機碼(); // //設定新的機碼,並且設定新的PASS KEY. // Microsoft.Win32.RegistryKey start = Microsoft.Win32.Registry.LocalMachine; // Microsoft.Win32.RegistryKey programName = start.OpenSubKey(keyPathString); // programName.SetValue("PASS_CODE", NewPSKey); // programName.Close(); // MessageBox.Show("由於您的驗證密碼已經完全毀損,已成功重新建立之" + Environment.NewLine + "您新的驗證密碼(請務必記住此密碼)為" + NewPSKey, "您的驗證機制已重新更新", MessageBoxButtons.OK, MessageBoxIcon.Information); // return; // } // } //有機碼,但機碼是否正確之Loop. //} //else //{ // //沒有此鍵,需根據是否有密碼Key來決定是否須提供驗證: // //1. 已有密碼Key,則需輸入密碼. // //2. 沒有密碼Key,則直接創建密碼Key與驗證碼,並顯示驗證密碼給使用者. // if(object.Equals(BuiltPASSKey,null)) // { // //沒有正確的驗證密碼且機碼不存在. // //創建新的驗證密碼與機碼. // string NewPSKey; // NewPSKey = 產生隨機碼(); // //設定新的機碼,並且設定新的PASS KEY. // Microsoft.Win32.RegistryKey start = Microsoft.Win32.Registry.LocalMachine; // Microsoft.Win32.RegistryKey programName = start.CreateSubKey(keyPathString); // programName.SetValue("PATH_S", 密碼加密(GetCPUID(), GetMacAddress()));//寫入值. // programName.SetValue("PASS_CODE", NewPSKey); // programName.Close(); // MessageBox.Show("由於您的驗證機碼與驗證密碼已經完全毀損,已成功重新建立之" + Environment.NewLine + "您新的驗證密碼(請務必記住此密碼)為" + NewPSKey, "您的驗證機制已重新更新", MessageBoxButtons.OK, MessageBoxIcon.Information); // _PSKEYCORRECT = false; // return; // } // else // { // //沒有機碼,但仍有驗證密碼. // _PSKEYCORRECT = false;//強制變更為密碼輸入不正確. // Form_EnterKey fkeyy = new Form_EnterKey(this, BuiltPASSKey); // fkeyy.ShowDialog();//強制顯示. // if (_PSKEYCORRECT) // { // //若驗證正確,則更新驗證機碼. // Microsoft.Win32.RegistryKey start = Microsoft.Win32.Registry.LocalMachine; // Microsoft.Win32.RegistryKey programName = start.CreateSubKey(keyPathString); // programName.SetValue("PATH_S", 密碼加密(GetCPUID(), GetMacAddress())); // MessageBox.Show("您已成功更新驗證機碼!"); // _PSKEYCORRECT = false; // return; // } // else // { // //驗證失敗[使用者取消]. // MessageBox.Show("您沒有成功驗證!操作取消", "驗證失敗", MessageBoxButtons.OK, MessageBoxIcon.Information); // _PSKEYCORRECT = false; // return; // } // } // //Microsoft.Win32.RegistryKey start = Microsoft.Win32.Registry.LocalMachine; // //Microsoft.Win32.RegistryKey programName = start.CreateSubKey(keyPathString); // //programName.SetValue("PATH_S", 密碼加密(GetCPUID(), GetMacAddress()));//寫入值. // //programName.Close(); //} //MessageBox.Show(GetCPUID()); //MessageBox.Show(GetMacAddress()); }