RNGCryptoServiceProvider.GetBytes 方法

定义

重载

GetBytes(Byte[])

用经过加密的强随机值序列填充字节数组。

GetBytes(Span<Byte>)

使用加密型强随机字节填充范围。

GetBytes(Byte[], Int32, Int32)

使用加密型强随机值序列填充指定字节数组(从指定索引处开始,填充指定字节数)。

GetBytes(Byte[])

Source:
RNGCryptoServiceProvider.cs
Source:
RNGCryptoServiceProvider.cs
Source:
RNGCryptoServiceProvider.cs

用经过加密的强随机值序列填充字节数组。

public override void GetBytes (byte[] data);

参数

data
Byte[]

用经过加密的强随机值序列填充的数组。

例外

无法获取加密服务提供程序 (CSP)。

datanull

示例

下面的代码示例演示如何使用 RNGCryptoServiceProvider 类创建随机数。

//The following sample uses the Cryptography class to simulate the roll of a dice.

using System;
using System.IO;
using System.Text;
using System.Security.Cryptography;

class RNGCSP
{
    private static RNGCryptoServiceProvider rngCsp = new RNGCryptoServiceProvider();
    // Main method.
    public static void Main()
    {
        const int totalRolls = 25000;
        int[] results = new int[6];

        // Roll the dice 25000 times and display
        // the results to the console.
        for (int x = 0; x < totalRolls; x++)
        {
            byte roll = RollDice((byte)results.Length);
            results[roll - 1]++;
        }
        for (int i = 0; i < results.Length; ++i)
        {
            Console.WriteLine("{0}: {1} ({2:p1})", i + 1, results[i], (double)results[i] / (double)totalRolls);
        }
        rngCsp.Dispose();
    }

    // This method simulates a roll of the dice. The input parameter is the
    // number of sides of the dice.

    public static byte RollDice(byte numberSides)
    {
        if (numberSides <= 0)
            throw new ArgumentOutOfRangeException("numberSides");

        // Create a byte array to hold the random value.
        byte[] randomNumber = new byte[1];
        do
        {
            // Fill the array with a random value.
            rngCsp.GetBytes(randomNumber);
        }
        while (!IsFairRoll(randomNumber[0], numberSides));
        // Return the random number mod the number
        // of sides.  The possible values are zero-
        // based, so we add one.
        return (byte)((randomNumber[0] % numberSides) + 1);
    }

    private static bool IsFairRoll(byte roll, byte numSides)
    {
        // There are MaxValue / numSides full sets of numbers that can come up
        // in a single byte.  For instance, if we have a 6 sided die, there are
        // 42 full sets of 1-6 that come up.  The 43rd set is incomplete.
        int fullSetsOfValues = Byte.MaxValue / numSides;

        // If the roll is within this range of fair values, then we let it continue.
        // In the 6 sided die case, a roll between 0 and 251 is allowed.  (We use
        // < rather than <= since the = portion allows through an extra 0 value).
        // 252 through 255 would provide an extra 0, 1, 2, 3 so they are not fair
        // to use.
        return roll < numSides * fullSetsOfValues;
    }
}

注解

字节数组的长度决定了生成的加密强度强随机字节数。

此方法是线程安全的。

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

GetBytes(Span<Byte>)

Source:
RNGCryptoServiceProvider.cs
Source:
RNGCryptoServiceProvider.cs
Source:
RNGCryptoServiceProvider.cs

使用加密型强随机字节填充范围。

public override void GetBytes (Span<byte> data);

参数

data
Span<Byte>

要用加密型强随机字节填充的范围。

适用于

.NET 9 和其他版本
产品 版本
.NET Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Standard 2.1

GetBytes(Byte[], Int32, Int32)

Source:
RNGCryptoServiceProvider.cs
Source:
RNGCryptoServiceProvider.cs
Source:
RNGCryptoServiceProvider.cs

使用加密型强随机值序列填充指定字节数组(从指定索引处开始,填充指定字节数)。

public override void GetBytes (byte[] data, int offset, int count);

参数

data
Byte[]

要用加密型强随机字节填充的数组。

offset
Int32

开始填充操作的数组的索引。

count
Int32

要填充的字节数。

例外

datanull

offsetcount 小于 0。

offset 加上 count 超过 data 的长度。

适用于

.NET 9 和其他版本
产品 版本
.NET Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Standard 2.1