c# 产生随机字符串,包括大小写字母和数字

本文介绍了一个简单的随机密码生成器实现方法,通过组合数字与大小写字母来生成具有一定复杂度的密码,有效提升账户安全性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 #region MyRegion
        //產生密碼
        protected static string GetPwd()
        {
           return CreateRandomNum123(2) + CreateRandomNumABC(3) + CreateRandomNumabc(3); 
        }
        protected static string CreateRandomNum123(int NumCount)
        {
            string allChar = "0,1,2,3,4,5,6,7,8,9"; // 
            string[] allCharArray = allChar.Split(',');//拆分成数组
            string randomNum = "";
            int temp = -1;//记录上次随机数的数值,尽量避免产生几个相同的随机数

            Random rand = new Random();
            for (int i = 0; i < NumCount; i++)
            {
                if (temp != -1)
                {
                    rand = new Random(i * temp * ((int)DateTime.Now.Ticks));
                }
                int t = rand.Next(10);
                if (temp == t)
                {
                    return CreateRandomNum123(NumCount);
                }
                temp = t;
                randomNum += allCharArray[t];
            }
            return randomNum;
        }
        protected static string CreateRandomNumABC(int NumCount)
        {
            string allChar = "A,B,D,E,F,G,H,I,J,K,L,M,N,Q,R,T,W,Y"; // 
            string[] allCharArray = allChar.Split(',');//拆分成数组
            string randomNum = "";
            int temp = -1;//记录上次随机数的数值,尽量避免产生几个相同的随机数

            Random rand = new Random();
            for (int i = 0; i < NumCount; i++)
            {
                if (temp != -1)
                {
                    rand = new Random(i * temp * ((int)DateTime.Now.Ticks));
                }
                int t = rand.Next(18);
                if (temp == t)
                {
                    return CreateRandomNumABC(NumCount);
                }
                temp = t;
                randomNum += allCharArray[t];
            }
            return randomNum;
        }
        protected static string CreateRandomNumabc(int NumCount)
        {
            string allChar = "a,b,d,e,f,g,h,i,j,k,l,m,n,q,r,t,w,y"; // 
            string[] allCharArray = allChar.Split(',');//拆分成数组
            string randomNum = "";
            int temp = -1;//记录上次随机数的数值,尽量避免产生几个相同的随机数

            Random rand = new Random();
            for (int i = 0; i < NumCount; i++)
            {
                if (temp != -1)
                {
                    rand = new Random(i * temp * ((int)DateTime.Now.Ticks));
                }
                int t = rand.Next(18);
                if (temp == t)
                {
                    return CreateRandomNumabc(NumCount);
                }
                temp = t;
                randomNum += allCharArray[t];
            }
            return randomNum;
        }
        #endregion

 

转载于:https://www.cnblogs.com/siri/p/3520722.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值