C#生成注册码的实例代码

时间:2022-05-16 23:35:58 


string t = DateTime.Now.Ticks.ToString();

            t = DESKey.DESEncrypt(t, DESKey.DesKeyStr);
            string[] strid = new string[t.Length];//
            for (int i = 0; i < t.Length; i++)//把字符赋给数组
            {
                strid[i] = t.Substring(i, 1);
            }
            string s = "";
            Random rdid = new Random();
            for (int i = 0; i < 9; i++)//从数组随机抽取字符组成新的字符生成机器三
            {
                s += strid[rdid.Next(0, strid.Length)];
            }


class DESKey
    {
        public const string DesKeyStr = "BLUE2013";

        #region DES加密
        /// <summary>
        /// DES加密
        /// </summary>
        /// <param name="pToEncrypt">需要加密的字符串</param>
        /// <returns>加密后的字符串</returns>
        public static string DESEncrypt(string pToEncrypt, string DesKeyStr)
        {
            try
            {
                DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt);
                des.Key = ASCIIEncoding.ASCII.GetBytes(DesKeyStr);
                des.IV = ASCIIEncoding.ASCII.GetBytes(DesKeyStr);
                MemoryStream ms = new MemoryStream();
                CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
                cs.Write(inputByteArray, 0, inputByteArray.Length);
                cs.FlushFinalBlock();
                StringBuilder ret = new StringBuilder();
                foreach (byte b in ms.ToArray())
                {
                    ret.AppendFormat("{0:X2}", b);
                }
                ret.ToString();
                return ret.ToString();
            }
            catch
            {

                return "";
            }


        }
        #endregion

        #region DES解密
        /// <summary>
        /// DES解密
        /// </summary>
        /// <param name="pToDecrypt">加密后的字符串</param>
        /// <returns>解密后的字符串</returns>
        public static string DESDecrypt(string pToDecrypt, string DesKeyStr)
        {
            try
            {
                DESCryptoServiceProvider des = new DESCryptoServiceProvider();

                byte[] inputByteArray = new byte[pToDecrypt.Length / 2];
                for (int x = 0; x < pToDecrypt.Length / 2; x++)
                {
                    int i = (Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16));
                    inputByteArray[x] = (byte)i;
                }

                des.Key = ASCIIEncoding.ASCII.GetBytes(DesKeyStr);
                des.IV = ASCIIEncoding.ASCII.GetBytes(DesKeyStr);
                MemoryStream ms = new MemoryStream();
                CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
                cs.Write(inputByteArray, 0, inputByteArray.Length);
                cs.FlushFinalBlock();

                StringBuilder ret = new StringBuilder();

                return System.Text.Encoding.Default.GetString(ms.ToArray());
            }
            catch
            {

                return "";
            }
        }
        #endregion

 

    }

标签:C#,注册码
0
投稿

猜你喜欢

  • spring cloud Feign使用@RequestLine遇到的坑

    2023-12-15 09:18:16
  • Spring Boot整合Mybatis并完成CRUD操作的实现示例

    2023-11-09 04:36:46
  • Java使用application.property读取文件里面的值

    2023-11-01 07:24:56
  • Spring Boot实现JWT token自动续期的实现

    2022-02-12 03:48:40
  • spring boot 2整合swagger-ui过程解析

    2021-08-08 22:57:35
  • Android jni调试打印char阵列的实例详解

    2022-06-18 14:55:40
  • 一键移除ButterKnife并替换为ViewBinding的旧项目拯救

    2023-08-19 12:30:28
  • Android开发使用自定义View将圆角矩形绘制在Canvas上的方法

    2021-06-08 01:03:17
  • C#简单快速的json组件fastJSON使用介绍

    2022-08-13 01:47:50
  • mybatis-plus配置控制台打印完整带参数SQL语句的实现

    2023-11-24 22:43:58
  • 浅谈JSON的数据交换、缓存问题和同步问题

    2021-08-08 09:31:25
  • C#实现航班预订系统

    2021-12-26 02:19:37
  • c语言定时器示例分享

    2023-11-04 09:44:52
  • C# 汉字与拼音互转的实现示例

    2022-03-06 06:50:20
  • spring cloud alibaba Nacos 注册中心搭建过程详解

    2022-07-08 17:38:01
  • mybatis foreach标签的使用详解

    2023-05-07 21:12:09
  • Android自定义LinearLayout布局显示不完整的解决方法

    2022-09-14 17:44:16
  • 使用C语言编写基于TCP协议的Socket通讯程序实例分享

    2023-07-05 03:30:03
  • Java超详细分析泛型与通配符

    2023-07-28 08:34:26
  • C# 使用相同权限调用 cmd 传入命令的方法

    2023-06-01 23:36:45
  • asp之家 软件编程 m.aspxhome.com