欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-12-03 08:36:58
复制代码 代码如下:string t = datetime.now.ticks.tostring();      ...

复制代码 代码如下:

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

 

    }