给软件增加注册功能 c#

1.软件注册类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Management;
using System.IO;
using System.Diagnostics;
using System.Configuration;

namespace Text2Excel.Helper
{
    public static class SoftRegister
    {
        private static string SoftVersion = "V02";//v0.1
        private static string SoftMD5 = string.Empty;
        private static string DiskVolume = string.Empty;
        private static string CPUSerialNum = string.Empty;
        private static string PublicKey = "<RSAKeyValue><Modulus>kkrIPGLqt2CUSeSLC3l4lgJzLuBt0GGwTI7Jqt9SbHmj8Kaz5ffPbHY3aibx4jF8bmWlyNiO8LTc2qL6CV1mCL599mStYhIdePElE9+WxniSc3YCCzKxpTnWjRL7ROA25fQPJghAWUiWva7VamwjF0rLHDgX+rcuGF7LScY49B0=</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>";
        private static string getDiskVolume()
        {
            //ManagementClass mc = new ManagementClass("win32_NetworkAdapterConfiguration");
            ManagementObject disk = new ManagementObject("win32_logicaldisk.deviceid=\"c:\"");
            disk.Get();
            return disk.GetPropertyValue("VolumeSerialNumber").ToString();
        }
        private static string getCPUserialNum()
        {
            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;
        }
        private static string getSoftMD5()
        {
            string fileName = System.Windows.Forms.Application.ExecutablePath;
            try
            {
                FileStream file = new FileStream(fileName, FileMode.Open, FileAccess.Read);
                System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
                byte[] retVal = md5.ComputeHash(file);
                file.Close();

                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < retVal.Length; i++)
                {
                    sb.Append(retVal[i].ToString("x2"));
                }
                return sb.ToString().ToUpper();
            }
            catch (Exception ex)
            {
                throw new Exception("GetMD5HashFromFile() fail,error:" + ex.Message);
            }
        }
        public static void killMyself()
        {
            string bat = string.Format("@echo off \n ping -n 3 127.0.0.1 \n del {0}", System.Windows.Forms.Application.ExecutablePath);
            File.WriteAllText("killme.bat", bat);
            ProcessStartInfo psi = new ProcessStartInfo();
            psi.FileName = "killme.bat";
            psi.Arguments = "\"" + Environment.GetCommandLineArgs()[0] + "\"";
            psi.WindowStyle = ProcessWindowStyle.Hidden;            
            Process.Start(psi);
            System.Environment.Exit(0);
            //Application.Exit();
        }
        public static bool meIsMe(){
            if (getSoftMD5().Substring(0,1) == SoftMD5)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        public static string getMNum()
        {
            if (SoftMD5 == string.Empty)
            {
                SoftMD5 = getSoftMD5();
            }
            if (DiskVolume == string.Empty)
            {
                DiskVolume = getDiskVolume();
            }
            if (string.IsNullOrEmpty(CPUSerialNum))
            {
                CPUSerialNum = getCPUserialNum();
            }
            string strNum = SoftVersion + SoftMD5.Substring(0, 5) + CPUSerialNum.Substring(0, 5) + DiskVolume.Substring(0, 5);
            return strNum;//18个注册字符
        }
        public static bool toRegist(string cipher)
        {
            try
            {
                if (CryptionHelper.VerifySigned(getMNum(), cipher, PublicKey))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception e)
            {
                LogHelper.WriteLog(e);
                return false;
            }
            
        }
        public static void updateConfig(string name, string value)
        {
            Configuration cfa = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            cfa.AppSettings.Settings[name].Value = value;
            cfa.Save();
            ConfigurationManager.RefreshSection("appSettings");
        }
    }
}
View Code

 

posted @ 2016-08-16 15:12  lwn6  阅读(670)  评论(0编辑  收藏  举报
什么是幸福?天天在做自己想做的事情,家人、同事、朋友、客户、网友都和和睦睦,身体健康、钱包鼓鼓、女朋友天天开心、生活无忧无虑就是最大的幸福