ソフトウェア登録

29059 ワード

以下、ソフトウェアの登録の実現過程を簡単に紹介します.
SoftRegクラス:
using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Management;



namespace CommonClass

{

    public class SoftReg

    {

        /// <summary>

        ///  

        /// </summary>

        /// <returns></returns>

        public string GetDiskVolumeSerialNumber()

        {

            ManagementClass mc = new ManagementClass("win32_NetworkAdapterConfiguration");

            ManagementObject disk = new ManagementObject("win32_logicaldisk.deviceid=\"c:\"");

            disk.Get();

            return disk.GetPropertyValue("VolumeSerialNumber").ToString();

        }



        /// <summary>

        ///  

        /// </summary>

        /// <returns></returns>

        public static string GetMacAddress()

        {

            string mac = string.Empty;

            using (ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration"))

            {

                ManagementObjectCollection moc = mc.GetInstances();

                foreach (ManagementObject mo in moc)

                {

                    if ((bool)mo["IPEnabled"])

                    {

                        mac = mo["MacAddress"].ToString();

                        break;

                    }

                }

            }

            return mac.Replace(":", "");

        }



        /// <summary>

        ///  CPU 

        /// </summary>

        /// <returns></returns>

        public string GetCpu()

        {

            string strCpu = null;

            ManagementClass myCpu = new ManagementClass("win32_Processor");

            ManagementObjectCollection myCpuCollection = myCpu.GetInstances();

            foreach (ManagementObject myObject in myCpuCollection)

            {

                strCpu = myObject.Properties["Processorid"].Value.ToString();

            }

            return strCpu;

        }



        /// <summary>

        ///  

        /// </summary>

        /// <returns></returns>

        public string GetMNum()

        {

            //string strNum = GetCpu() + GetDiskVolumeSerialNumber();   

            string strNum = GetMacAddress() + GetDiskVolumeSerialNumber();

            string strMNum = strNum.Substring(0, 20); // 20 

            return strMNum;

        }



        public int[] intCode = new int[127]; // 

        public char[] charCode = new char[21]; // ASCII 

        public int[] intNumber = new int[21]; // ASCII 



        // 

        public void SetIntCode()

        {

            for (int i = 1; i < intCode.Length; i++)

            {

                intCode[i] = i % 9;

            }

        }



        /// <summary>

        ///  

        /// </summary>

        /// <returns></returns>

        public string GetRNum(string xStr)

        {

            SetIntCode();

            string strMNum = xStr;

            for (int i = 1; i < charCode.Length; i++) // 

            {

                charCode[i] = Convert.ToChar(strMNum.Substring(i - 1, 1));

            }

            for (int j = 1; j < intNumber.Length; j++) // ASCII 

            {

                intNumber[j] = Convert.ToInt32(charCode[j]) + intCode[Convert.ToInt32(charCode[j])];

            }

            string strAsciiName = ""; // 

            for (int k = 1; k < intNumber.Length; k++) // 

            {



                if ((intNumber[k] >= 48 && intNumber[k] <= 57) || (intNumber[k] >= 65 && intNumber[k]

                <= 90) || (intNumber[k] >= 97 && intNumber[k] <= 122)) // 0-9、A-Z、a-z 

                {

                    strAsciiName += Convert.ToChar(intNumber[k]).ToString();

                }

                else if (intNumber[k] > 122) // z

                {

                    strAsciiName += Convert.ToChar(intNumber[k] - 10).ToString();

                }

                else

                {

                    strAsciiName += Convert.ToChar(intNumber[k] - 9).ToString();

                }

            }

            return strAsciiName;

        }

    }

}

 
 
登録フォーム:
using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

using ImClient.Comm.UI;

using ImClient.Comm;

using CommonClass;

using Microsoft.Win32;

using ImCommon.Util;

using ImCommon.FrameWork;



namespace ImClient.UI

{

    public partial class SoftRegForm : BaseForm

    {

        public SoftRegForm()

        {

            InitializeComponent();

        }





        SoftReg softreg = new SoftReg();

        #region * FormLoad

        private void SoftRegForm_Load(object sender, EventArgs e)

        {

            this.txtMNum.Text = softreg.GetMNum();// 

            string RNum = softreg.GetRNum(softreg.GetMNum());// 

            bool xFlag = false;

            RegistryKey retkey = Registry.CurrentUser.OpenSubKey("SOFTWARE", true).OpenSubKey(Encrypt.EncryptDES("jara", Const.EncryptKey), true).OpenSubKey(Encrypt.EncryptDES("jara.ini",Const.EncryptKey),true);// 

            foreach (string keyName in retkey.GetSubKeyNames())

            {

                if (Encrypt.DecryptDES(keyName, Const.EncryptKey) == RNum)// 

                {

                    xFlag = true;

                    break;

                }

            }

            if (xFlag == true)

            {

                this.txtRNum.Text = RNum;

                this.txtRNum.Properties.ReadOnly = true;

                this.btnReg.Enabled = false;

            }

            else

            {

                this.txtRNum.Text = string.Empty;

                this.txtRNum.Properties.ReadOnly = false;

                this.btnReg.Enabled = true;

            }

        }



        private void SoftRegForm_Shown(object sender, EventArgs e)

        {

            this.txtRNum.Focus();

        }

        #endregion



        #region *  

        private void btnReg_Click(object sender, EventArgs e)

        {

            if (this.txtRNum.Text.ToString() == string.Empty)

            {

                ImClient.Comm.MessageBox.Info(" !");

                return;

            }

            try

            {

                if (this.txtRNum.Text == softreg.GetRNum(this.txtMNum.Text.ToString().Trim()))

                {

                    RegistryKey retkey = Registry.CurrentUser.OpenSubKey("Software", true).CreateSubKey(Encrypt.EncryptDES("jara",Const.EncryptKey)).CreateSubKey(Encrypt.EncryptDES("jara.ini",Const.EncryptKey)).CreateSubKey(Encrypt.EncryptDES(this.txtRNum.Text.ToString().Trim(), Const.EncryptKey));

                    ImClient.Comm.MessageBox.Info(" ! !");

                    this.Close();

                }

                else

                {

                    ImClient.Comm.MessageBox.Warn(" !");

                }

            }

            catch (Exception ex)

            {

                throw new Exception(ex.Message);

            }

        }

        #endregion



        #region *  

        private void btnClose_Click(object sender, EventArgs e)

        {

            this.Close();

        }

        #endregion

    }

}

 
メインフォーム:
public static void RegMetod()

        {

            SoftReg softreg = new SoftReg();

            // 

            RegistryKey retkey = Registry.CurrentUser.OpenSubKey("SOFTWARE", true).CreateSubKey(Encrypt.EncryptDES("jara", Const.EncryptKey)).CreateSubKey(Encrypt.EncryptDES("jara.ini", Const.EncryptKey));

            foreach (string strRNum in retkey.GetSubKeyNames())

            {

                if (Encrypt.DecryptDES(strRNum, Const.EncryptKey) == softreg.GetRNum(softreg.GetMNum()))

                {

                    Application.Run(new LoginForm(" ",""));// 

                    return;

                }

            }

            //System.Windows.Forms.MessageBox.Show(" , 30 !", " ", MessageBoxButtons.OK, MessageBoxIcon.Information);

            Int32 tLong;

            try

            {

                tLong = (Int32)Registry.GetValue("HKEY_LOCAL_MACHINE\\SOFTWARE\\Angel", "UseTimes", 0);

                //System.Windows.Forms.MessageBox.Show(" " + tLong + " !", " ", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }

            catch

            {

                //System.Windows.Forms.MessageBox.Show(" !", " ", MessageBoxButtons.OK, MessageBoxIcon.Information);

                Registry.SetValue("HKEY_LOCAL_MACHINE\\SOFTWARE\\Angel", "UseTimes", 0, RegistryValueKind.DWord);

            }

            tLong = (Int32)Registry.GetValue("HKEY_LOCAL_MACHINE\\SOFTWARE\\Angel", "UseTimes", 0);

            if (tLong < 30)

            {

                int tTimes = tLong + 1;

                Registry.SetValue("HKEY_LOCAL_MACHINE\\SOFTWARE\\Angel", "UseTimes", tTimes);

                int xint = 30 - tTimes;

                Application.Run(new LoginForm(" ",xint.ToString()));// 

            }

            else

            {

                DialogResult result = System.Windows.Forms.MessageBox.Show(" ! ?", " ", MessageBoxButtons.YesNo, MessageBoxIcon.Information);

                if (result == DialogResult.Yes)

                {

                    //frmRegisterForm.state = false;

                    //btnReg_Click(sender, e);

                }

                else

                {

                    Application.Exit();

                }

            }

        }