RNGCryptoServiceProvider.GetBytes メソッドとは? わかりやすく解説

Weblio 辞書 > コンピュータ > .NET Framework クラス ライブラリ リファレンス > RNGCryptoServiceProvider.GetBytes メソッドの意味・解説 

RNGCryptoServiceProvider.GetBytes メソッド

バイト配列に、暗号化使用するランダムな値の厳密なシーケンス設定します

名前空間: System.Security.Cryptography
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

例外例外
例外種類条件

CryptographicException

暗号サービス プロバイダ (CSP) を取得できません。

解説解説
使用例使用例

RNGCryptoServiceProvider クラス使用して乱数作成する方法次のコード例示します

'The following sample uses the Cryptography class to simulate the roll
 of a dice.
Imports System
Imports System.IO
Imports System.Text
Imports System.Security.Cryptography



Class RNGCSP
    
    ' Main method.
    Public Shared Sub Main()
        ' Roll the dice 30 times and display 
        ' the results to the console.
        Dim x As Integer
        For x = 0 To 30
            Console.WriteLine(RollDice(6))
        Next x
    End Sub 'Main
     
    ' This method simulates a roll of the dice. The input parameter
 is the 
    ' number of sides of the dice.
    Public Shared Function
 RollDice(NumSides As Integer) As
 Integer
        ' Create a byte array to hold the random value.
        Dim randomNumber(0) As Byte
        
        ' Create a new instance of the RNGCryptoServiceProvider. 
        Dim Gen As New RNGCryptoServiceProvider()
        
        ' Fill the array with a random value.
        Gen.GetBytes(randomNumber)
        
        ' Convert the byte to an integer value to make the modulus operation
 easier.
        Dim rand As Integer
 = Convert.ToInt32(randomNumber(0))
        
        ' Return the random number mod the number
        ' of sides.  The possible values are zero-
        ' based, so we add one.
        Return rand Mod NumSides + 1
    End Function 'RollDice
End Class 'CryptoMemoryStream

//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
{
    // Main method.
    public static void Main()
    {
        // Roll the dice 30 times and display 
        // the results to the console.
        for(int x = 0; x <= 30; x++)
            Console.WriteLine(RollDice(6));
    }
    
    // This method simulates a roll of the dice. The input parameter
 is the 
    // number of sides of the dice.
    public static int RollDice(int
 NumSides)
    {
        // Create a byte array to hold the random value.
        byte[] randomNumber = new byte[1];

        // Create a new instance of the RNGCryptoServiceProvider. 
        RNGCryptoServiceProvider Gen = new RNGCryptoServiceProvider();

        // Fill the array with a random value.
        Gen.GetBytes(randomNumber);

        // Convert the byte to an integer value to make the modulus
 operation easier.
        int rand = Convert.ToInt32(randomNumber[0]);

        // Return the random number mod the number
        // of sides.  The possible values are zero-
        // based, so we add one.
        return rand % NumSides + 1;
    }
}

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

using namespace System;
using namespace System::IO;
using namespace System::Text;
using namespace System::Security::Cryptography;

int RollDice(int numberSides)
{
    // Create a byte array to hold the random value.
    array<Byte>^ randomNumber = gcnew array<Byte>(1);

    // Create a new instance of the RNGCryptoServiceProvider.
    RNGCryptoServiceProvider^ cryptoProvider =
        gcnew RNGCryptoServiceProvider();

    // Fill the array with a random value.
    cryptoProvider->GetBytes(randomNumber);

    // Convert the byte to an integer value to make the modulus operation
    // easier.
    int rand = Convert::ToInt32(randomNumber[0]);

    // Return the random number mod the number of sides.  The possible
    // values are zero-based, so we add one.
    return (rand % numberSides) + 1;
}
    
int main()
{
    // Roll the dice 30 times and display
    // the results to the console.
    for (int i = 0; i <= 30; i++)
    {
        Console::WriteLine(RollDice(6));
    }
}

プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
RNGCryptoServiceProvider クラス
RNGCryptoServiceProvider メンバ
System.Security.Cryptography 名前空間
その他の技術情報
暗号サービス


このページでは「.NET Framework クラス ライブラリ リファレンス」からRNGCryptoServiceProvider.GetBytes メソッドを検索した結果を表示しています。
Weblioに収録されているすべての辞書からRNGCryptoServiceProvider.GetBytes メソッドを検索する場合は、下記のリンクをクリックしてください。
 全ての辞書からRNGCryptoServiceProvider.GetBytes メソッド を検索

英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

「RNGCryptoServiceProvider.GetBytes メソッド」の関連用語

RNGCryptoServiceProvider.GetBytes メソッドのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



RNGCryptoServiceProvider.GetBytes メソッドのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2025 Microsoft.All rights reserved.

©2025 GRAS Group, Inc.RSS