■ CD 키 확인하기

------------------------------------------------------------------------------------------------------------------------


TestProject.zip


MainForm.cs

 

 

using System;

using System.Management;

using System.Security.Cryptography;

using System.Text;

using System.Windows.Forms;

 

namespace TestProject

{

    /// <summary>

    /// 메인 폼

    /// </summary>

    public partial class MainForm : Form

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Field

        ////////////////////////////////////////////////////////////////////////////////////////// Private

 

        #region Field

 

        /// <summary>

        /// 문자 배열

        /// </summary>

        private char[] characterArray = new char[]

        {

            '0', '1', '2', '3', '4',

            '5', '6', '7', '8', '9',

            'A', 'B', 'C', 'D', 'E',

            'F', 'G', 'H', 'I', 'J',

            'K', 'L', 'M', 'N', 'O',

            'P', 'Q', 'R', 'S', 'T',

            'U', 'V', 'W', 'X', 'Y',

            'Z'

        };

 

        /// <summary>

        /// MAC 주소

        /// </summary>

        private string macAddress;

        

        /// <summary>

        /// CD 키

        /// </summary>

        private string cdKey;

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor

        ////////////////////////////////////////////////////////////////////////////////////////// Public

 

        #region 생성자 - MainForm()

 

        /// <summary>

        /// 생성자

        /// </summary>

        public MainForm()

        {

            InitializeComponent();

 

            #region 이벤트를 설정한다.

 

            Load                         += Form_Load;

            this.key1TextBox.TextChanged += key1TextBox_TextChanged;

            this.key2TextBox.TextChanged += key2TextBox_TextChanged;

            this.key3TextBox.TextChanged += key3TextBox_TextChanged;

            this.key4TextBox.TextChanged += key4TextBox_TextChanged;

            this.key5TextBox.TextChanged += key5TextBox_TextChanged;

            this.checkButton.Click       += checkButton_Click;

 

            #endregion

        }

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method

        ////////////////////////////////////////////////////////////////////////////////////////// Private

        //////////////////////////////////////////////////////////////////////////////// Event

 

        #region 폼 로드시 처리하기 - Form_Load(sender, e)

 

        /// <summary>

        /// 폼 로드시 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void Form_Load(object sender, EventArgs e)

        {

            ObjectQuery objectQuery = new ObjectQuery("SELECT * FROM Win32_NetworkAdapter");

 

            ManagementObjectSearcher managementObjectSearcher = new ManagementObjectSearcher(objectQuery);

 

            foreach(ManagementObject managementObject in managementObjectSearcher.Get())

            {

                if(managementObject["MACAddress"] != null)

                {

                    this.macAddress = managementObject["MACAddress"].ToString().Replace(":", "");

                }

            }

        }

 

        #endregion

        #region 키 1 텍스트 박스 텍스트 변경시 처리하기 - key1TextBox_TextChanged(sender, e)

 

        /// <summary>

        /// 키 1 텍스트 박스 텍스트 변경시 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void key1TextBox_TextChanged(object sender, EventArgs e)

        {

            if(this.key1TextBox.TextLength == 5)

            {

                this.key2TextBox.Focus();

            }

        }

 

        #endregion

        #region 키 2 텍스트 박스 텍스트 변경시 처리하기 - key2TextBox_TextChanged(sender, e)

 

        /// <summary>

        /// 키 2 텍스트 박스 텍스트 변경시 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void key2TextBox_TextChanged(object sender, EventArgs e)

        {

            if(this.key2TextBox.TextLength == 5)

            {

                this.key3TextBox.Focus();

            }

        }

 

        #endregion

        #region 키 3 텍스트 박스 텍스트 변경시 처리하기 - key3TextBox_TextChanged(sender, e)

 

        /// <summary>

        /// 키 3 텍스트 박스 텍스트 변경시 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void key3TextBox_TextChanged(object sender, EventArgs e)

        {

            if(this.key3TextBox.TextLength == 5)

            {

                this.key4TextBox.Focus();

            }

        }

 

        #endregion

        #region 키 4 텍스트 박스 텍스트 변경시 처리하기 - key4TextBox_TextChanged(sender, e)

 

        /// <summary>

        /// 키 4 텍스트 박스 텍스트 변경시 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void key4TextBox_TextChanged(object sender, EventArgs e)

        {

            if(this.key4TextBox.TextLength == 5)

            {

                this.key5TextBox.Focus();

            }

        }

 

        #endregion

        #region 키 5 텍스트 박스 텍스트 변경시 처리하기 - key5TextBox_TextChanged(sender, e)

 

        /// <summary>

        /// 키 5 텍스트 박스 텍스트 변경시 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void key5TextBox_TextChanged(object sender, EventArgs e)

        {

            if(this.key5TextBox.TextLength == 5)

            {

                this.checkButton.Focus();

            }

        }

 

        #endregion

        #region 확인하기 버튼 클릭시 처리하기 - checkButton_Click(sender, e)

 

        /// <summary>

        /// 확인하기 버튼 클릭시 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void checkButton_Click(object sender, EventArgs e)

        {

            string userName = this.userNameTextBox.Text.Trim();

 

            if(string.IsNullOrEmpty(userName))

            {

                MessageBox.Show

                (

                    this,

                    "사용자명을 입력해 주시기 바랍니다.",

                    "INFORMATION",

                    MessageBoxButtons.OK,

                    MessageBoxIcon.Information

                );

 

                this.userNameTextBox.Focus();

 

                return;

            }

 

            string key1 = this.key1TextBox.Text.Trim();

            string key2 = this.key2TextBox.Text.Trim();

            string key3 = this.key3TextBox.Text.Trim();

            string key4 = this.key4TextBox.Text.Trim();

            string key5 = this.key5TextBox.Text.Trim();

 

            if(string.IsNullOrEmpty(key1) || string.IsNullOrEmpty(key2) || string.IsNullOrEmpty(key3) ||

                string.IsNullOrEmpty(key4) || string.IsNullOrEmpty(key5))

            {

                MessageBox.Show

                (

                    this,

                    "CD 키를 입력해 주시기 바랍니다.",

                    "INFORMATION",

                    MessageBoxButtons.OK,

                    MessageBoxIcon.Information

                );

 

                return;

            }

 

            CalculateCDKey();

 

            string cdKey = string.Format($"{key1}-{key2}-{key3}-{key4}-{key5}");

 

            if(this.cdKey == cdKey)

            {

                this.resultLabel.Text = "CD 키가 일치합니다.";

            }

            else

            {

                this.resultLabel.Text = "CD 키가 일치하지 않습니다.";

            }

        }

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////// Function

 

        #region MD5 해시 구하기 - GetMD5Hash(text)

 

        /// <summary>

        /// MD5 해시 구하기

        /// </summary>

        /// <param name="text">텍스트</param>

        /// <returns>MD5 해시</returns>

        private string GetMD5Hash(string text)

        {

            MD5 md5 = MD5CryptoServiceProvider.Create();

 

            Byte[] hashAray = md5.ComputeHash(Encoding.Default.GetBytes(text));

 

            StringBuilder stringBuilder = new StringBuilder();

 

            for(int i = 0; i < hashAray.Length - 1; i++)

            {

                stringBuilder.AppendFormat("{0:x2}", hashAray[i]);

            }

 

            return stringBuilder.ToString();

        }

 

        #endregion

        #region CD 키 계산하기 - CalculateCDKey()

 

        /// <summary>

        /// CD 키 계산하기

        /// </summary>

        private void CalculateCDKey()

        {

            string yearString      = DateTime.Now.Year.ToString();

            string userName        = this.userNameTextBox.Text.Trim();

            string userNameMD5Hash = GetMD5Hash(userName);

 

            StringBuilder stringBuilder1 = new StringBuilder();

 

            stringBuilder1.Append(this.characterArray[Convert.ToInt32(yearString) % 36]);

            stringBuilder1.Append(this.characterArray[Convert.ToInt32(yearString.Substring(0, 1))]);

            stringBuilder1.Append(this.characterArray[Convert.ToInt32(yearString.Substring(2, 1))]);

            stringBuilder1.Append(this.characterArray[Convert.ToInt32(yearString.Substring(3, 1))]);

            stringBuilder1.Append(this.macAddress);

            stringBuilder1.Append(this.macAddress.Substring(0, 1));

            stringBuilder1.Append(this.macAddress.Substring(1, 1));

            stringBuilder1.Append(userNameMD5Hash.Substring(0, 1));

            stringBuilder1.Append(userNameMD5Hash.Substring(29, 1));

 

            StringBuilder stringBuilder2 = new StringBuilder();

 

            string keyList = stringBuilder1.ToString().ToUpper();

 

            char[] keyArray      = keyList.ToCharArray();

            int[]  keyValueArray = new int[keyArray.Length];

 

            int add = DateTime.Now.Year;

            int z;

 

            int value0 = 0;

            int value1 = 0;

            int value2 = 0;

            int value3 = 0;

            int value4 = 0;

 

            for(int i = 0; i < keyArray.Length; i++)

            {

                z = (i + 10) * add;

 

                keyValueArray[i] = (int)keyArray[i];

 

                stringBuilder2.Append(this.characterArray[(z ^ keyValueArray[i]) % 36]);

 

                if(((i + 1) % 5 == 0))

                {

                    stringBuilder2.Append("-");

                }

 

                switch(i % 5)

                {

                    case 0 : value0 += ((z ^ keyValueArray[i]) % 36); break;

                    case 1 : value1 += ((z ^ keyValueArray[i]) % 36); break;

                    case 2 : value2 += ((z ^ keyValueArray[i]) % 36); break;

                    case 3 : value3 += ((z ^ keyValueArray[i]) % 36); break;

                    case 4 : value4 += ((z ^ keyValueArray[i]) % 36); break;

                }

            }

 

            stringBuilder2.Append(this.characterArray[value0 % 36]);

            stringBuilder2.Append(this.characterArray[value1 % 36]);

            stringBuilder2.Append(this.characterArray[value2 % 36]);

            stringBuilder2.Append(this.characterArray[value3 % 36]);

            stringBuilder2.Append(this.characterArray[value4 % 36]);

 

            this.cdKey = stringBuilder2.ToString();

        }

 

        #endregion

    }

}

 

------------------------------------------------------------------------------------------------------------------------

Posted by 사용자 icodebroker
TAG