陈宝刚---享受生活,追逐梦想!
理想是心中的火焰,有追求的人才是幸福的人!

这是一个自己做的关于C#软件注册码小程序的代码(略去了窗体代码)

 

下图是小程序截图

 

 

using System;

using System.Drawing;

using System.Collections;

using System.ComponentModel;

using System.Windows.Forms;

using System.Data;

using System.Configuration;

using System.IO;

using System.Security.Cryptography;

using System.Management;

 

 

 

 

 

 

 

 

 

namespace Messagebox

{

    /// <summary>

    /// Form1 的摘要说明。

    /// </summary>

    public class Form1 : System.Windows.Forms.Form

    {

        private System.Windows.Forms.Button button1;

        private System.Windows.Forms.TextBox textBox1;

        private Button button2;

        private Button button3;

        private Button button4;

        /// <summary>

        /// 必需的设计器变量。

        /// </summary>

        private System.ComponentModel.Container components = null;

 

        public Form1()

        {

            //

            // Windows 窗体设计器支持所必需的

            //

            InitializeComponent();

 

            //

            // TODO: 在InitializeComponent 调用后添加任何构造函数代码

            //

        }

 

        /// <summary>

        /// 清理所有正在使用的资源。

        /// </summary>

        protected override void Dispose(bool disposing)

        {

            if (disposing)

            {

                if (components != null)

                {

                    components.Dispose();

                }

            }

            base.Dispose(disposing);

        }

 

 

        /// <summary>

        /// 应用程序的主入口点。

        /// </summary>

        [STAThread]

        static void Main()

        {

            Application.Run(new Form1());

        }

 

        private void button1_Click(object sender, System.EventArgs e)

        {

            Hasher hash = new Hasher();

 

            textBox1 .Text = hash.hashGetDriveID().ToString();

 

        }

 

        private void button2_Click(object sender, EventArgs e)

        {

            Hasher shit = new Hasher ();

            textBox1.Text = shit.Sern();

        }

 

        private void button3_Click(object sender, EventArgs e)

        {

           Hasher shit = new Hasher ();

           textBox1.Text = shit.GetDriveID();

        }

 

        private void button4_Click(object sender, EventArgs e)

        {

             Hasher shit = new Hasher ();

             textBox1.Text = shit.GetCpuID();

        }

 

 

 

 

    }

 

 

    public class Hasher

    {

 

 

 

        public string _HashText; //待加密的字符串

 

        // public Hasher()

 

        // {

 

        // }

 

 

 

 

 

        // 需要产生加密哈希的字符串

 

        public string HashText

        {

 

            set

            {

 

                _HashText = value;

 

            }

 

            get

            {

 

                return _HashText;

 

            }

 

        }

 

 

 

        /// 使用MD5CryptoServiceProvider类产生哈希值。不需要提供密钥。

 

        /// </summary>

 

        /// <returns></returns>

 

        public string MD5Hasher()

        {

 

            byte[] MD5Data = System.Text.Encoding.UTF8.GetBytes(HashText);

 

 

 

            MD5 Md5 = new MD5CryptoServiceProvider();

 

 

 

            byte[] Result = Md5.ComputeHash(MD5Data);

 

 

 

            return Convert.ToBase64String(Result); //返回长度为字节字符串

 

        }

 

 

 

 

 

        //获取cpu序列号

 

        public String GetCpuID()

        {

 

            ManagementClass mc = new ManagementClass("Win32_Processor");

 

            ManagementObjectCollection moc = mc.GetInstances();

 

 

 

            String strCpuID = null;

 

            foreach (ManagementObject mo in moc)

            {

 

                strCpuID = mo.Properties["ProcessorId"].Value.ToString();

 

                break;

 

            }

 

            return strCpuID;

 

        }//end method

 

        //获取硬盘序列号

 

        public String GetDriveID()

        {

 ManagementObject mo = new ManagementObject("win32_LogicalDisk.deviceid=""C:""");

            mo.Get();

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

 

 

 

        }

 

 

 

        public String strJia()

       {

 

            string strJiami = "";

 

            strJiami = GetCpuID() + GetDriveID();

 

 

 

            return strJiami;

 

        }

 

        public string jiqixuelie;

 

        public string duijiqima = null;

 

        ///得到机器码

 

        public string hashGetDriveID()

        {

 

            Hasher hs = new Hasher();

 

 

 

            hs.HashText = hs.strJia();

 

            string jiqi = hs.MD5Hasher();

 

            duijiqima = jiqi.Substring(8, 5);

 

            return duijiqima;

 

        }

 

 

 

        public string Sern()

 

        //最终的序列号//m为配置文件值

 

        //x为序列号值

 

        //css进行比较该注册码是否正确

        {

 

 

 

 

 

            //获取机器码

 

            Hasher hs = new Hasher();

 

 

 

            //获取序列号

 

            Hasher hash = new Hasher();

 

            hash.HashText = hs.hashGetDriveID().ToString();

 

            jiqixuelie = hash.MD5Hasher();

 

            return jiqixuelie;

 

 

        }

 

 

 

    }

}

posted on 2009-07-17 23:43  追梦人RUBY  阅读(1362)  评论(0编辑  收藏  举报