-1

我正在尝试使用 RNGCryptoServiceProvider 生成随机数,但是我得到了 o(Zero) 作为回报

private int generatePassword()
{

    using (RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider())
    {
        string value = string.Empty;
        byte[] randomnumber = new byte[10000];
        rng.GetBytes(randomnumber);
       return randomnumber;

    }

}
4

1 回答 1

0

也许您应该在这里查看此链接

dotNetFiddle

static string RandomString(int length)
{
    const string valid = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
    StringBuilder res = new StringBuilder();

    using (RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider())
    {
        byte[] uintBuffer = new byte[sizeof(uint)];

        while (length-- > 0)
        {
            rng.GetBytes(uintBuffer);
            uint num = BitConverter.ToUInt32(uintBuffer, 0);
            res.Append(valid[(int)(num % (uint)valid.Length)]);
        }
    }

    return res.ToString();
}
于 2017-03-24T07:17:57.787 回答