public static string GetNewPassword()
{
Random rd = new Random();
int pwdLength = ApplicationConfiguration.RandomPasswordLength;
byte[] pwd = new byte[pwdLength];

int i;
for( i=0;i<pwdLength;i++ )
{
int a = 0;
//while 后的条件用来限定密码的字符集
while( !((a>=48 && a<=57) || (a>=65 && a<=90) || (a>=97 && a<=122)) )
{
a = rd.Next(48,125);
}
pwd[i] = (byte)a;
}

return new string(UnicodeEncoding.ASCII.GetChars(pwd));
}这个我的一个动态产生密码的方法,你把限定字符集改一改就可以用了

解决方案 »

  1.   

    int pwdLength = ApplicationConfiguration.RandomPasswordLength;
    byte[] pwd = new byte[pwdLength];改一改:byte[] pwd = new byte["你需要的长度"];
      

  2.   

    string CreateRandomString(int length)
    {
          Encoding encoding = Encoding.Unicode;
          byte[] buf = new Byte[encoding.GetByteCount(new string(' ', length))];
          for (;;)
          {
            Random r = new Random();
            r.NextBytes(buf);
            try
            {
              return new string(encoding.GetChars(buf));
            }
            catch(Exception)
            {
            }
          }
     }