• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C#简单通用的软件注册类

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

using System;
using System.Collections.Generic;
using System.Text;
using System.Management;//硬件获取
using System.IO;//
using System.Security.Cryptography;//

namespace PurviewSoft
{
    public class PurviewSoftClass
    {
        /// <summary>
        /// 获取CPU编号
        /// </summary>
        /// <returns>返回CPU序列号</returns>
        private static string GetCPUSerialNumber()
        {
            string cpuSerialNumber = string.Empty;
            ManagementClass mc = new ManagementClass("Win32_processor");
            ManagementObjectCollection moc = mc.GetInstances();
            foreach (ManagementObject mo in moc)
            {
                cpuSerialNumber = mo["ProcessorId"].ToString();
                break;
            }
            mc.Dispose();
            moc.Dispose();
            return cpuSerialNumber;
        }

        /// <summary>
        /// DES加密字符串
        /// </summary>
        /// <param name="encryptString">待加密的字符串</param>
        /// <returns>加密成功返回加密后的字符串,失败返回源串</returns>
        private static string EncryptString(string encryptString)
        {
            try
            {
                byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
                byte[] rgbKey = Encoding.UTF8.GetBytes("jiao1990");
                byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);
                DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider();

                MemoryStream mStream = new MemoryStream();
                using (CryptoStream cStream = new CryptoStream(mStream, dCSP.CreateEncryptor(rgbKey, IV), CryptoStreamMode.Write))
                {
                    cStream.Write(inputByteArray, 0, inputByteArray.Length);
                    cStream.FlushFinalBlock();
                }
                return Convert.ToBase64String(mStream.ToArray());
            }
            catch
            {
                return encryptString;
            }
        }

        //-------------------------------------------------------------------------------------------------------------------------

        /// <summary>
        /// 生成的用户ID属性
        /// </summary>
        public static string UserId
        {
            get { return GetCPUSerialNumber(); }
        }

        /// <summary>
        /// 获取由程序生成的正确密匙(管理员使用)
        /// </summary>
        /// <param name="userId">用户反馈给管理员的产品编号</param>
        /// <returns>正确的授权号</returns>
        public static string GetUserPassWord(string userId)
        {
            //分析密码是否正确
            string userPassWord = EncryptString(userId);
            return userPassWord;
        }

        /// <summary>
        /// 获取用户是否通过验证
        /// </summary>
        /// <param name="passWord">用户输入的密码</param>
        /// <returns>true:是会员,false:不是会员</returns>
        public static bool GetUserRule(string passWord)
        {
            bool isUser = false;
            try
            {
                //分析用户输入的密码是否正确
                string userId = GetCPUSerialNumber();//获取CPU编号
                string userPassWord = EncryptString(userId);//加密CPU编号
                if (passWord == userPassWord)
                {
                    isUser = true;
                }
            }
            catch
            { }
            return isUser;
        }

        /// <summary>
        /// 把密码写入配置文档
        /// </summary>
        /// <param name="passWord">用户输入的密码</param>
        /// <param name="userInfoFilePath">保存密码的配置文件路径(保存为TXT或INI格式二进制文件)</param>
        /// <returns>trur: 写入成功,false:写入失败</returns>
        public static bool WriteUserInfo(string passWord, string userInfoFilePath)
        {
            bool isSucess = false;
            try
            {
                bool isUser = GetUserRule(passWord);//获取是否通过会员验证
                if (isUser)
                {
                    //创建保存密码的配置文档
                    using (StreamWriter sw = File.CreateText(userInfoFilePath))
                    {
                        sw.WriteLine(passWord);
                    }
                }
            }
            catch
            { }
            return isSucess;
        }

        /// <summary>
        /// 获取配置文件中记载的用户密码
        /// </summary>
        /// <param name="userInfoFilePath">配置文件路径</param>
        /// <returns>记载的用户密码</returns>
        public static string readUserInfo(string userInfoFilePath)
        {
            if (File.Exists(userInfoFilePath))//如果存在注册配置文件
            {
                using (StreamReader sr = File.OpenText(userInfoFilePath))
                {
                    String input;
                    input = sr.ReadLine();
                    return input;
                }
            }
            else
            {
                return null;
            }
        }
    }
}


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
encodeURI()decodeURI()C#发布时间:2022-07-10
下一篇:
在C#7.3中不可用。请使用8.0或更高的语言版本。发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap