为什么我的价值为0。我调试了它,发现我的值是0。此外,当我存储到数据库时,它也存储0

时间:2017-03-24 06:54:54

标签: c# asp.net random rngcryptoserviceprovider

我正在尝试使用RNGCryptoServiceProvider生成随机数,但有些我如何得到o(零)作为回报

private int generatePassword()
{

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

    }

}

1 个答案:

答案 0 :(得分:0)

也许你应该看看这个link here

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();
}