.NETヘルプ RandomNumberGenerator C# Curtis Chau 更新日:9月 4, 2025 Download IronPDF NuGet Download テキストの検索と置換 テキストと画像のスタンプ Start Free Trial Copy for LLMs Copy for LLMs Copy page as Markdown for LLMs Open in ChatGPT Ask ChatGPT about this page Open in Gemini Ask Gemini about this page Open in Grok Ask Grok about this page Open in Perplexity Ask Perplexity about this page Share Share on Facebook Share on X (Twitter) Share on LinkedIn Copy URL Email article 時々、PDF文書を扱うときに、ランダムな数字やランダムな文字列を生成する必要がある場合があります。 PDF暗号化のためにランダムな数字やパスワードを生成したり、ユニークなファイル名を作成したり、予測可能なシーケンスを避けたり、その他の目的で数字を生成する必要がある場合、RandomNumberGenerator C#クラスを使用すると、PDFの生成および編集プロジェクトを次のレベルに引き上げることができます。 Randomクラスから作成された基本的なランダムオブジェクトとは異なり、この暗号ランダム数生成器は、セキュアなアプリケーションでの暗号操作に適した暗号的に強力なランダム値を生成します。 この記事では次のことを探ります: RandomNumberGeneratorが何であり、なぜ重要なのか。 暗号目的で暗号ランダム数生成器を使用してランダムな数値、ランダムな文字列、その他のランダムデータを生成する方法。 生成された数字や文字列を使用して安全でユニークなPDFを作成するためのIronPDFとRandomNumberGeneratorの統合の実践的な例。 PDFアプリケーションのセキュリティとプロフェッショナリズムを向上させるためのヒントとベストプラクティス。 RandomNumberGeneratorクラスとは何ですか IronPDF統合に入る前に、RandomNumberGeneratorが特別である理由を簡単に振り返ります。 System.Security.Cryptography名前空間の一部です。 ランダムバイトとして暗号的に強力なランダム値を生成し、通常のRandomクラスよりも遥かに安全です。 セキュアなトークン、キー、ソルト、ユニークな識別子の生成など、予測不能性を要するシナリオに理想的です。 AES-CTR DRBG、Fortuna、またはOSが提供するCSPRNGなどの暗号アルゴリズムを使用。 * 予測に対する耐性があります。 暗号キーの作成、パスワード生成、セキュアトークン、ユニークなドキュメントIDを作成するタスクに最適です。 これは、オペレーティングシステムの基礎にある安全なランダム数生成器に依存することから来る強さであり、予測やリバースエンジニアリング攻撃に耐性があります。 同じシード値で同じシーケンスを生成する可能性がある疑似ランダム数生成器とは異なり、このクラスは真のランダム性および暗号目的のために設計されています。 C#のRandomクラスではなくRandomNumberGeneratorを使用する理由 多くの開発者はランダムな整数を生成するためにC#のRandomクラスを最初に使用しますが、高セキュリティシナリオ向けに設計されていません。 同じシード値またはシステム時間を使用する場合、出力のパターンは予測可能であり、生成された数字が推測される可能性があります。 これは、メソッドが同じ入力で繰り返す可能性のある単純な計算式の操作を使用して値を生成するためです。 対照的に、RandomNumberGeneratorは、.NET Frameworkまたは基礎OSの暗号ランダム数生成器から派生した真のランダム数を生成します。 このことによって、低い値の偏りがなく、しばしば下限値(例:int minValue)と排他的な上限値(例:int maxValue)の間で一様分布を維持するために破棄および再試行戦略を使用します。 以下の図は、弱いRNGと安全なRNGの違いを示しています。 IronPDFでのRandomNumberGeneratorの使用理由 IronPDFは強力な.NET PDFライブラリであり、開発者がPDF文書を作成、編集、保護することを可能にします。 ランダム性が重要な一般的な使用例には次のものがあります: ユニークなドキュメント識別子:PDFに暗号的に安全なIDを付けて追跡や検証を行います。 セキュアウォーターマーク:偽造を防ぐランダムなウォーターマークやコードを埋め込む。 暗号化キーまたはパスワード:PDF暗号化のためのセキュアなキーやパスワードを生成。 ランダムコンテンツ:整合性の確認のためにPDF文書にランダムなユニークトークンやソルトを加えます。 C#での安全なランダムデータ生成方法 ここに、128ビット(16バイト)の安全なランダムトークンを生成する単純な例があります。 using System; using System.Security.Cryptography; public static string GenerateSecureToken(int size = 16) { byte[] randomBytes = new byte[size]; RandomNumberGenerator.Fill(randomBytes); return Convert.ToBase64String(randomBytes); } using System; using System.Security.Cryptography; public static string GenerateSecureToken(int size = 16) { byte[] randomBytes = new byte[size]; RandomNumberGenerator.Fill(randomBytes); return Convert.ToBase64String(randomBytes); } Imports System Imports System.Security.Cryptography Public Shared Function GenerateSecureToken(Optional ByVal size As Integer = 16) As String Dim randomBytes(size - 1) As Byte RandomNumberGenerator.Fill(randomBytes) Return Convert.ToBase64String(randomBytes) End Function $vbLabelText $csharpLabel このメソッドは安全なバイト配列を作成し、それをBase64文字列として返します。ユニットテスト、ファイル名、または安全なIDに埋め込むのに最適です。 実用例:PDF文書にユニークなドキュメントIDを追加 RandomNumberGeneratorとIronPDFの威力を組み合わせて、各ページにユニークでセキュアなドキュメントIDが刻印されたPDFを生成しましょう。 ステップ1: セキュアなドキュメントIDを生成 string GenerateDocumentId() { byte[] idBytes = new byte[12]; // 96-bit ID RandomNumberGenerator.Fill(idBytes); return BitConverter.ToString(idBytes).Replace("-", ""); } string GenerateDocumentId() { byte[] idBytes = new byte[12]; // 96-bit ID RandomNumberGenerator.Fill(idBytes); return BitConverter.ToString(idBytes).Replace("-", ""); } Private Function GenerateDocumentId() As String Dim idBytes(11) As Byte ' 96-bit ID RandomNumberGenerator.Fill(idBytes) Return BitConverter.ToString(idBytes).Replace("-", "") End Function $vbLabelText $csharpLabel これは24文字の16進ランダム文字列を生成します(例:「4F3A2C9B7D1E8F0A5B6C7D8E」)。 ステップ2: PDFを作成してIDを刻印 using IronPdf; void CreatePdfWithSecureId() { var documentId = GenerateDocumentId(); var renderer = new ChromePdfRenderer(); // Add a custom footer with the document ID renderer.RenderingOptions.HtmlFooter = new HtmlHeaderFooter { HtmlFragment = $"<div style='text-align: center; font-size: 10px;'>Document ID: {documentId} - Generated on {DateTime.UtcNow:yyyy-MM-dd HH:mm:ss} UTC</div>", DrawDividerLine = true, }; var pdf = renderer.RenderHtmlAsPdf($"<h1>Secure Document</h1><p>Document ID: {documentId}</p>"); string outputPath = $"SecurePdf_{documentId}.pdf"; pdf.SaveAs(outputPath); Console.WriteLine($"PDF saved as {outputPath}"); }What this Code Does: using IronPdf; void CreatePdfWithSecureId() { var documentId = GenerateDocumentId(); var renderer = new ChromePdfRenderer(); // Add a custom footer with the document ID renderer.RenderingOptions.HtmlFooter = new HtmlHeaderFooter { HtmlFragment = $"<div style='text-align: center; font-size: 10px;'>Document ID: {documentId} - Generated on {DateTime.UtcNow:yyyy-MM-dd HH:mm:ss} UTC</div>", DrawDividerLine = true, }; var pdf = renderer.RenderHtmlAsPdf($"<h1>Secure Document</h1><p>Document ID: {documentId}</p>"); string outputPath = $"SecurePdf_{documentId}.pdf"; pdf.SaveAs(outputPath); Console.WriteLine($"PDF saved as {outputPath}"); }What this Code Does: Imports IronPdf Private Sub CreatePdfWithSecureId() Dim documentId = GenerateDocumentId() Dim renderer = New ChromePdfRenderer() ' Add a custom footer with the document ID renderer.RenderingOptions.HtmlFooter = New HtmlHeaderFooter With { .HtmlFragment = $"<div style='text-align: center; font-size: 10px;'>Document ID: {documentId} - Generated on {DateTime.UtcNow:yyyy-MM-dd HH:mm:ss} UTC</div>", .DrawDividerLine = True } Dim pdf = renderer.RenderHtmlAsPdf($"<h1>Secure Document</h1><p>Document ID: {documentId}</p>") Dim outputPath As String = $"SecurePdf_{documentId}.pdf" pdf.SaveAs(outputPath) Console.WriteLine($"PDF saved as {outputPath}") End Sub 'INSTANT VB TODO TASK: The following line uses invalid syntax: 'What Me Code Does: $vbLabelText $csharpLabel 暗号的に安全でユニークなドキュメントID用のランダムな数字を生成。 そのIDを埋め込んだHTMLをPDFにレンダリングします。 ドキュメントIDとタイムスタンプを含むフッターを各ページに追加。 追跡を容易にするためにIDをファイル名として保存します。 完全な動作コード例 using IronPdf; using IronPdf.Editing; using System; using System.Security.Cryptography; class Program { public static void Main(string[] args) { // Create an instance of Program to run non-static methods var program = new Program(); program.CreatePdfWithSecureId() } string GenerateDocumentId() { byte[] idBytes = new byte[12]; // 96-bit ID RandomNumberGenerator.Fill(idBytes); return BitConverter.ToString(idBytes).Replace("-", ""); } void CreatePdfWithSecureId() { var documentId = GenerateDocumentId(); var renderer = new ChromePdfRenderer(); // Add a custom footer with the document ID renderer.RenderingOptions.HtmlFooter = new HtmlHeaderFooter { HtmlFragment = $"<div style='text-align: center; font-size: 10px;'>Document ID: {documentId} - Generated on {DateTime.UtcNow:yyyy-MM-dd HH:mm:ss} UTC</div>", DrawDividerLine = true, }; var pdf = renderer.RenderHtmlAsPdf($"<h1>Secure Document</h1><p>Document ID: {documentId}</p>"); string outputPath = $"SecurePdf_{documentId}.pdf"; pdf.SaveAs(outputPath); Console.WriteLine($"PDF saved as {outputPath}"); } } using IronPdf; using IronPdf.Editing; using System; using System.Security.Cryptography; class Program { public static void Main(string[] args) { // Create an instance of Program to run non-static methods var program = new Program(); program.CreatePdfWithSecureId() } string GenerateDocumentId() { byte[] idBytes = new byte[12]; // 96-bit ID RandomNumberGenerator.Fill(idBytes); return BitConverter.ToString(idBytes).Replace("-", ""); } void CreatePdfWithSecureId() { var documentId = GenerateDocumentId(); var renderer = new ChromePdfRenderer(); // Add a custom footer with the document ID renderer.RenderingOptions.HtmlFooter = new HtmlHeaderFooter { HtmlFragment = $"<div style='text-align: center; font-size: 10px;'>Document ID: {documentId} - Generated on {DateTime.UtcNow:yyyy-MM-dd HH:mm:ss} UTC</div>", DrawDividerLine = true, }; var pdf = renderer.RenderHtmlAsPdf($"<h1>Secure Document</h1><p>Document ID: {documentId}</p>"); string outputPath = $"SecurePdf_{documentId}.pdf"; pdf.SaveAs(outputPath); Console.WriteLine($"PDF saved as {outputPath}"); } } Imports IronPdf Imports IronPdf.Editing Imports System Imports System.Security.Cryptography Friend Class Program Public Shared Sub Main(ByVal args() As String) ' Create an instance of Program to run non-static methods 'INSTANT VB NOTE: The variable program was renamed since it may cause conflicts with calls to static members of the user-defined type with this name: Dim program_Conflict As New Program() program_Conflict.CreatePdfWithSecureId() End Sub Private Function GenerateDocumentId() As String Dim idBytes(11) As Byte ' 96-bit ID RandomNumberGenerator.Fill(idBytes) Return BitConverter.ToString(idBytes).Replace("-", "") End Function Private Sub CreatePdfWithSecureId() Dim documentId = GenerateDocumentId() Dim renderer = New ChromePdfRenderer() ' Add a custom footer with the document ID renderer.RenderingOptions.HtmlFooter = New HtmlHeaderFooter With { .HtmlFragment = $"<div style='text-align: center; font-size: 10px;'>Document ID: {documentId} - Generated on {DateTime.UtcNow:yyyy-MM-dd HH:mm:ss} UTC</div>", .DrawDividerLine = True } Dim pdf = renderer.RenderHtmlAsPdf($"<h1>Secure Document</h1><p>Document ID: {documentId}</p>") Dim outputPath As String = $"SecurePdf_{documentId}.pdf" pdf.SaveAs(outputPath) Console.WriteLine($"PDF saved as {outputPath}") End Sub End Class $vbLabelText $csharpLabel 出力 高度な使用例:ランダムキーによる安全なPDF暗号化 IronPDFはPDF暗号化をサポートし、セキュアなPDF作成に強力なサポートを提供します。 暗号化のために強力なパスワードやキーを作成するにはRandomNumberGeneratorを使用できます: using IronPdf; using IronPdf.Editing; using System; using System.Security.Cryptography; class Program { public static void Main(string[] args) { // Create an instance of Program to run non-static methods var program = new Program(); program.CreateEncryptedPdf(); } string GenerateSecurePassword(int length = 12) { const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()"; byte[] data = new byte[length]; RandomNumberGenerator.Fill(data); char[] result = new char[length]; for (int i = 0; i < length; i++) { result[i] = chars[data[i] % chars.Length]; } return new string(result); } void CreateEncryptedPdf() { string password = GenerateSecurePassword(); var renderer = new ChromePdfRenderer(); var pdf = renderer.RenderHtmlAsPdf("<h1>Confidential PDF</h1><p>Access is restricted.</p>"); // Set security settings pdf.SecuritySettings.UserPassword = password; pdf.SecuritySettings.AllowUserAnnotations = false; pdf.SecuritySettings.AllowUserCopyPasteContent = false; pdf.SecuritySettings.AllowUserPrinting = IronPdf.Security.PdfPrintSecurity.NoPrint; string filePath = "EncryptedSecurePdf.pdf"; pdf.SaveAs(filePath); Console.WriteLine($"Encrypted PDF saved to {filePath} with password: {password}"); } } using IronPdf; using IronPdf.Editing; using System; using System.Security.Cryptography; class Program { public static void Main(string[] args) { // Create an instance of Program to run non-static methods var program = new Program(); program.CreateEncryptedPdf(); } string GenerateSecurePassword(int length = 12) { const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()"; byte[] data = new byte[length]; RandomNumberGenerator.Fill(data); char[] result = new char[length]; for (int i = 0; i < length; i++) { result[i] = chars[data[i] % chars.Length]; } return new string(result); } void CreateEncryptedPdf() { string password = GenerateSecurePassword(); var renderer = new ChromePdfRenderer(); var pdf = renderer.RenderHtmlAsPdf("<h1>Confidential PDF</h1><p>Access is restricted.</p>"); // Set security settings pdf.SecuritySettings.UserPassword = password; pdf.SecuritySettings.AllowUserAnnotations = false; pdf.SecuritySettings.AllowUserCopyPasteContent = false; pdf.SecuritySettings.AllowUserPrinting = IronPdf.Security.PdfPrintSecurity.NoPrint; string filePath = "EncryptedSecurePdf.pdf"; pdf.SaveAs(filePath); Console.WriteLine($"Encrypted PDF saved to {filePath} with password: {password}"); } } Imports IronPdf Imports IronPdf.Editing Imports System Imports System.Security.Cryptography Friend Class Program Public Shared Sub Main(ByVal args() As String) ' Create an instance of Program to run non-static methods 'INSTANT VB NOTE: The variable program was renamed since it may cause conflicts with calls to static members of the user-defined type with this name: Dim program_Conflict As New Program() program_Conflict.CreateEncryptedPdf() End Sub Private Function GenerateSecurePassword(Optional ByVal length As Integer = 12) As String Const chars As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()" Dim data(length - 1) As Byte RandomNumberGenerator.Fill(data) Dim result(length - 1) As Char For i As Integer = 0 To length - 1 result(i) = chars.Chars(data(i) Mod chars.Length) Next i Return New String(result) End Function Private Sub CreateEncryptedPdf() Dim password As String = GenerateSecurePassword() Dim renderer = New ChromePdfRenderer() Dim pdf = renderer.RenderHtmlAsPdf("<h1>Confidential PDF</h1><p>Access is restricted.</p>") ' Set security settings pdf.SecuritySettings.UserPassword = password pdf.SecuritySettings.AllowUserAnnotations = False pdf.SecuritySettings.AllowUserCopyPasteContent = False pdf.SecuritySettings.AllowUserPrinting = IronPdf.Security.PdfPrintSecurity.NoPrint Dim filePath As String = "EncryptedSecurePdf.pdf" pdf.SaveAs(filePath) Console.WriteLine($"Encrypted PDF saved to {filePath} with password: {password}") End Sub End Class $vbLabelText $csharpLabel このメソッドはOSのRNGからのデフォルト実装を使用し、暗号操作に適したパスワードを生成します。 生成された安全なパスワードが適用されるため、パスワードへのアクセス権を持つユーザーだけが文書を閲覧できます。 さらに、設定した許可と共に暗号化され安全な文書が残ります: ベストプラクティスとヒント セキュリティ関連のあらゆることにはRandomNumberGeneratorを常に使用。 ID、トークン、またはパスワードにRandomを避ける。 センシティブデータをログやユーザー向けメッセージから外し、十分な追跡と監査が可能になるようにログを記録。 ランダムなIDと組み合わせてタイムスタンプや他のメタデータを使用してトレース性を向上。 IronPDFの組み込みセキュリティ機能をランダムキーと組み合わせて文書を保護します。 ファイル名、URL、バーコードなどの状況での使用可能性を確保するためにランダムデータの長さとエンコーディングを検証する。 まとめ C#のRandomNumberGeneratorクラスをIronPDFと統合することで、現代のセキュリティ基準を満たす安全でユニークなPDFを生成できます。 ユニークなIDのスタンプ、暗号化キーの生成、セキュアトークンの埋め込みなどを行うことで、このアプローチは次のことを支援します: 文書の偽造を防ぐ トレース性を向上 PDF内のセンシティブデータのセキュリティを高める このクラスの暗号的強度とIronPDFの多用途なPDFツールを組み合わせることで、PDFソリューションをより安全かつプロフェッショナルにします。 自分で試してみよう! PDFセキュリティを向上させる準備はできましたか? IronPDFの無料トライアルを試して、今日セキュアなランダムデータを使って実験を始めましょう! よくある質問 C# における RandomNumberGenerator クラスとは? C# の RandomNumberGenerator クラスは、暗号的に安全なランダム数を生成する方法を提供する System.Security.Cryptography 名前空間の一部です。PDF の暗号化やユニークな識別子の生成などのシナリオで特に有用です。 RandomNumberGenerator は PDF プロジェクトでどのように使用できますか? PDF プロジェクトでは、RandomNumberGenerator を使用して PDF 暗号化のための安全なパスワードを作成し、ユニークなファイル名を生成し、予測可能な連続性を排除することができます。これにより、ドキュメントのセキュリティと独自性が向上します。 ランダム数を生成するために RandomNumberGenerator を選ぶ理由は何ですか? RandomNumberGenerator は、暗号的に安全な数を生成するため推奨されます。これにより、PDF ドキュメントの暗号化など、セキュリティが重要なアプリケーションに適しています。 RandomNumberGenerator は予測可能な連番の回避に役立ちますか? はい、RandomNumberGenerator は、暗号的に安全な数を生成することで予測可能な連番を回避し、シーケンスがランダムかつ簡単に予測できないようにします。 RandomNumberGenerator はランダム文字列の生成に適していますか? はい、RandomNumberGenerator は、まずランダムバイトを生成し、それを文字列に変換することでランダム文字列を生成するために使用できます。これは、PDF プロジェクト内で安全なパスワードやユニークな識別子を作成するのに役立ちます。 C# での RandomNumberGenerator の利用例は何ですか? C# での RandomNumberGenerator の利用例には、PDF 暗号化、ユニークなファイル名の生成、ランダムパスワードの作成、および安全なランダム数が必要な任意のシナリオが含まれます。 RandomNumberGenerator はどのようにして PDF ドキュメントのセキュリティを強化しますか? RandomNumberGenerator は、暗号的に安全な乱数を提供することによって、PDF ドキュメントのセキュリティを強化します。これにより、暗号化キーやパスワード、その他のセキュリティ対策としてドキュメントコンテンツを保護します。 RandomNumberGenerator を他の乱数生成器よりも使う利点は何ですか? RandomNumberGenerator を他のランダム数生成器よりも使用する利点は、暗号的に安全な数を生成できることであり、高いセキュリティが求められるアプリケーション、例えば PDF ファイルの暗号化などに適しています。 Curtis Chau 今すぐエンジニアリングチームとチャット テクニカルライター Curtis Chauは、カールトン大学でコンピュータサイエンスの学士号を取得し、Node.js、TypeScript、JavaScript、およびReactに精通したフロントエンド開発を専門としています。直感的で美しいユーザーインターフェースを作成することに情熱を持ち、Curtisは現代のフレームワークを用いた開発や、構造の良い視覚的に魅力的なマニュアルの作成を楽しんでいます。開発以外にも、CurtisはIoT(Internet of Things)への強い関心を持ち、ハードウェアとソフトウェアの統合方法を模索しています。余暇には、ゲームをしたりDiscordボットを作成したりして、技術に対する愛情と創造性を組み合わせています。 関連する記事 更新日 9月 4, 2025 C# String Equals(開発者向けの仕組み) 強力なPDFライブラリであるIronPDFと組み合わせることで、switchパターンマッチングは、ドキュメント処理のためのよりスマートでクリーンなロジックを構築できます 詳しく読む 更新日 8月 5, 2025 C# Switch Pattern Matching(開発者向けの仕組み) 強力なPDFライブラリであるIronPDFと組み合わせることで、switchパターンマッチングは、ドキュメント処理のためのよりスマートでクリーンなロジックを構築できます 詳しく読む 更新日 8月 5, 2025 C# Integer Division(開発者向けの仕組み) 二つの整数(int型)を/演算子で割る場合、C#は整数演算を行い、分数部分を切り捨てて整数を返します 詳しく読む C# String Equals(開発者向け...
更新日 9月 4, 2025 C# String Equals(開発者向けの仕組み) 強力なPDFライブラリであるIronPDFと組み合わせることで、switchパターンマッチングは、ドキュメント処理のためのよりスマートでクリーンなロジックを構築できます 詳しく読む
更新日 8月 5, 2025 C# Switch Pattern Matching(開発者向けの仕組み) 強力なPDFライブラリであるIronPDFと組み合わせることで、switchパターンマッチングは、ドキュメント処理のためのよりスマートでクリーンなロジックを構築できます 詳しく読む
更新日 8月 5, 2025 C# Integer Division(開発者向けの仕組み) 二つの整数(int型)を/演算子で割る場合、C#は整数演算を行い、分数部分を切り捨てて整数を返します 詳しく読む