• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
后园
博客园    首页    新随笔    联系   管理    订阅  订阅
随机生成N位密码(C#)
Code
 1    /**//// <summary>
 2    /// 生成随机数的种子
 3    /// </summary>
 4    /// <returns></returns>

 5    private static int getNewSeed()
 6    {
 7        byte[] rndBytes = new byte[4];
 8        System.Security.Cryptography.RNGCryptoServiceProvider rng = new System.Security.Cryptography.RNGCryptoServiceProvider();
 9        rng.GetBytes(rndBytes);
10        return BitConverter.ToInt32(rndBytes, 0);
11    }
     /**//// <summary>
12    /// 生成8位随机数
13    /// </summary>
14    /// <param name="length"></param>
15    /// <returns></returns>

16    static public string GetRandomString(int len)
17    {
18        string s = "123456789abcdefghijklmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ";
19        string reValue = string.Empty;
20        Random rnd = new Random(getNewSeed());
21        while (reValue.Length < len)
22        {
23            string s1 = s[rnd.Next(0, s.Length)].ToString();
24            if (reValue.IndexOf(s1) == -1) reValue += s1;
25        }

26        return reValue;
27    }

 

以上是随机产生len长度的密码,因为数字0和大小写的o不容易分辨,所以去掉。

这完全是我个人的建议,要与不要,全在个人了啦。。。

posted on 2008-10-24 11:02  ning ning  阅读(643)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3