.NETヘルプ RandomNumberGenerator C# Jacob Mellor 更新日:9月 4, 2025 IronPDF をダウンロード NuGet ダウンロード DLL ダウンロード Windows 版 無料トライアル LLM向けのコピー LLM向けのコピー LLM 用の Markdown としてページをコピーする ChatGPTで開く このページについてChatGPTに質問する ジェミニで開く このページについてGeminiに問い合わせる ジェミニで開く このページについてGeminiに問い合わせる 困惑の中で開く このページについてPerplexityに問い合わせる 共有する Facebook で共有 Xでシェア(Twitter) LinkedIn で共有 URLをコピー 記事をメールで送る 時々、PDF文書を扱うときに、ランダムな数字やランダムな文字列を生成する必要がある場合があります。 PDF暗号化のためにランダムな数字やパスワードを生成したり、ユニークなファイル名を作成したり、予測可能なシーケンスを避けたり、その他の目的で数字を生成する必要がある場合、RandomNumberGenerator C#クラスを使用すると、PDFの生成および編集プロジェクトを次のレベルに引き上げることができます。 Randomクラスから作成された基本的なランダムオブジェクトとは異なり、この暗号ランダム数生成器は、セキュアなアプリケーションでの暗号操作に適した暗号的に強力なランダム値を生成します。 この記事では次のことを探ります: RandomNumberGeneratorが何であり、なぜ重要なのか。 暗号目的で暗号ランダム数生成器を使用してランダムな数値、ランダムな文字列、その他のランダムデータを生成する方法。 応答性のある一意のPDFを生成するために生成された番号や文字列を使用して、IronPDFとのランダム数生成器統合の実用的な例。 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 ファイルの暗号化などに適しています。 Jacob Mellor 今すぐエンジニアリングチームとチャット 最高技術責任者(CTO) Jacob Mellorは、Iron Softwareの最高技術責任者であり、C# PDF技術の開拓者としてその先進的な役割を担っています。Iron Softwareのコアコードベースのオリジナルデベロッパーである彼は、創業時から製品のアーキテクチャを形作り、CEOのCameron Rimingtonと協力してNASA、Tesla、全世界の政府機関を含む50人以上の会社に成長させました。Jacobは、1998年から2001年にかけてマンチェスター大学で土木工学の第一級優等学士号(BEng)を取得しました。1999年にロンドンで最初のソフトウェアビジネスを立ち上げ、2005年には最初の.NETコンポーネントを作成し、Microsoftエコシステムにおける複雑な問題の解決を専門にしました。彼の旗艦製品であるIronPDFとIronSuite .NETライブラリは、全世界で3000万以上のNuGetインストールを達成しており、彼の基本コードが世界中で使用されている開発者ツールを支えています。商業的な経験を25年間積み、コードを書くことを41年間続けるJacobは、企業向けのC#、Java、およびPython PDF技術の革新を推進し続け、次世代の技術リーダーを指導しています。 関連する記事 更新日 12月 11, 2025 CLIの簡素化と.NETの橋渡し:Curl DotNetとIronPDFを使う Jacob Mellorは、.NETエコシステムにcURLの親しみやすさをもたらすために作成されたライブラリ、CurlDotNetでこのギャップを埋めました。 詳しく読む 更新日 9月 4, 2025 C# String Equals(開発者向けの仕組み) 強力なPDFライブラリであるIronPDFと組み合わせることで、switchパターンマッチングは、ドキュメント処理のためのよりスマートでクリーンなロジックを構築できます 詳しく読む 更新日 8月 5, 2025 C# Switch Pattern Matching(開発者向けの仕組み) 強力なPDFライブラリであるIronPDFと組み合わせることで、switchパターンマッチングは、ドキュメント処理のためのよりスマートでクリーンなロジックを構築できます 詳しく読む CLIの簡素化と.NETの橋渡し:Curl DotNetとIronPDFを使うC# String Equals(開発者向け...
更新日 12月 11, 2025 CLIの簡素化と.NETの橋渡し:Curl DotNetとIronPDFを使う Jacob Mellorは、.NETエコシステムにcURLの親しみやすさをもたらすために作成されたライブラリ、CurlDotNetでこのギャップを埋めました。 詳しく読む
更新日 9月 4, 2025 C# String Equals(開発者向けの仕組み) 強力なPDFライブラリであるIronPDFと組み合わせることで、switchパターンマッチングは、ドキュメント処理のためのよりスマートでクリーンなロジックを構築できます 詳しく読む
更新日 8月 5, 2025 C# Switch Pattern Matching(開発者向けの仕組み) 強力なPDFライブラリであるIronPDFと組み合わせることで、switchパターンマッチングは、ドキュメント処理のためのよりスマートでクリーンなロジックを構築できます 詳しく読む