■ 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

    {

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

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

 

        #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'

        };

 

        #endregion

 

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

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

 

        #region 생성자 - MainForm()

 

        /// <summary>

        /// 생성자

        /// </summary>

        public MainForm()

        {

            InitializeComponent();

 

            #region 이벤트를 설정한다.

 

            Load                    += Form_Load;

            this.createButton.Click += createButton_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)

        {

            int currentYear = DateTime.Now.Year;

 

            for(int i = currentYear - 3; i < currentYear + 4; i++)

            {

                this.yearComboBox.Items.Add(i.ToString());

            }

 

            this.yearComboBox.Text = currentYear.ToString();

 

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

 

            ManagementObjectSearcher managementObjectSearcher = new ManagementObjectSearcher(objectQuery);

 

            foreach(ManagementObject managementObject in managementObjectSearcher.Get())

            {

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

                {

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

                }

            }

        }

 

        #endregion

        #region 생성하기 버튼 클릭시 처리하기 - createButton_Click(sender, e)

 

        /// <summary>

        /// 생성하기 버튼 클릭시 처리하기

        /// </summary>

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

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

        private void createButton_Click(object sender, EventArgs e)

        {

            string year       = this.yearComboBox.Text;

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

            string maxAddress = this.macAddressTextBox.Text.Trim();

 

            if(string.IsNullOrEmpty(userName))

            {

                MessageBox.Show

                (

                    this,

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

                    "INFORMATION",

                    MessageBoxButtons.OK,

                    MessageBoxIcon.Error

                );

 

                this.userNameTextBox.Focus();

 

                return;

            }

 

            StringBuilder stringBuilder1 = new StringBuilder();

 

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

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

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

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

            stringBuilder1.Append(maxAddress);

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

            stringBuilder1.Append(maxAddress.Substring(1, 1));

            stringBuilder1.Append(GetMD5Hash(userName).Substring(0, 1));

            stringBuilder1.Append(GetMD5Hash(userName).Substring(29, 1));

            

            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;

 

            StringBuilder stringBuilder2 = new StringBuilder();

 

            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.cdKeyTextBox.Text = stringBuilder2.ToString();

        }

 

        #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[] hashArray = md5.ComputeHash(Encoding.Default.GetBytes(text));

 

            StringBuilder stringBuilder = new StringBuilder();

 

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

            {

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

            }

 

            return stringBuilder.ToString();

        }

 

        #endregion

    }

}

 

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

Posted by 사용자 icodebroker
TAG