-
C#樹林
[C#] Storing and Reusing Private Keys in RSA Key Containers with CspParameters
Overview This implementation uses the Key Container feature of the CryptoAPI (CAPI) in Windows to persist RSA key pairs at the OS level. Instead of managing keys as files, you specify a "Container Name" linked to the user profile or mach... -
C#樹林
【C#】CspParametersを用いたRSAキーコンテナへの秘密鍵格納と再利用
概要 Windows環境において、CryptoAPI(CAPI)のキーコンテナ機能を利用し、RSA鍵ペアをOSレベルで永続化する実装です。 ファイルとして鍵を保存・管理するのではなく、OSのユーザープロファイルまたはマシンストアに関連付けられた「コンテナ名」を指定す... -
C#樹林
[C#] How to Generate RSA Key Pairs and Convert to PEM Format
Overview In modern .NET environments, you can generate RSA key pairs (private and public keys) using only standard libraries and export them in the highly portable PEM format (Base64 strings). This approach replaces the legacy XML format... -
未分類
【C#】RSA鍵ペアの生成とPEM形式による秘密鍵・公開鍵のテキスト化
概要 最新の.NET環境において、標準ライブラリのみを使用してRSA暗号化方式のキーペア(秘密鍵と公開鍵)を新規生成し、取り回しのしやすいPEM形式(Base64文字列)としてエクスポートする実装です。 従来のXML形式(ToXmlString)に代わり、OpenSSLや他の... -
C#樹林
[C#] How to Decrypt Files Using Symmetric Key Encryption (AES)
Overview To restore an encrypted file to its original state (decryption), you perform the reverse stream process of encryption. By passing the encrypted data read from a file through a CryptoStream in read mode, the data is converted bac... -
C#樹林
【C#】共通鍵暗号(AES)で暗号化されたファイルを復号する方法
概要 暗号化されたファイルを元の状態に戻す(復号する)には、暗号化時と逆のストリーム処理を行います。 ファイルから読み込んだ暗号データを CryptoStream(読み取りモード)に通すことで平文に戻し、その結果を別のファイルへ書き出します。この処理も... -
C#樹林
[C#] How to Encrypt Files Using Symmetric Key Encryption (AES)
Overview When encrypting files, loading the entire file into memory can lead to out-of-memory errors, especially with large files like videos or backups. By combining CryptoStream and Stream.CopyTo, you can perform "stream processing"—en... -
C#樹林
【C#】共通鍵暗号(AES)でファイルを暗号化する方法
概要 ファイルを暗号化する場合、ファイル全体をメモリに読み込んでから処理すると、巨大なファイル(動画やバックアップデータなど)でメモリ不足になる危険があります。 CryptoStream と Stream.CopyTo を組み合わせることで、ファイルの中身を少しずつ... -
C#樹林
[C#] How to Decrypt Strings Encrypted with Symmetric Key Encryption (AES)
Overview This article explains how to return an encrypted string (in Base64 format) back to its original plaintext using a shared Key and Initialization Vector (IV). The decryption process is the reverse of encryption. You use CryptoStre... -
C#樹林
【C#】共通鍵暗号(AES)で暗号化された文字列を復号する方法
概要 暗号化された文字列(Base64形式)を、共通の鍵(Key)と初期化ベクトル(IV)を使用して元の平文に戻す実装です。 復号処理は暗号化の逆の手順となり、CryptoStream を「読み取りモード(Read)」で使用し、暗号化されたバイトデータをストリーム経... -
C#樹林
[C#] How to Encrypt Strings Using Symmetric Key Encryption (AES)
Overview To encrypt strings in .NET, you apply encryption to a data stream using the CryptoStream class. Since strings cannot be encrypted directly, the transformation follows this process: "String → Byte Array → Encryption Stream → Encr... -
C#樹林
【C#】共通鍵暗号(AES)で文字列を暗号化する方法
概要 .NETで文字列を暗号化するには、CryptoStream クラスを使用してデータの流れ(ストリーム)に対して暗号化処理を適用します。 文字列をそのまま暗号化することはできないため、「文字列 → バイト列 → 暗号化ストリーム → 暗号化されたバイト列」とい... -
C#樹林
[C#] How to Generate Encryption Keys and Initialization Vectors (IV) for Symmetric Encryption
Overview To perform secure communication using symmetric encryption like AES (Advanced Encryption Standard), both the encryption and decryption processes must share the same "Key" and "Initialization Vector (IV)." By using classes in the... -
C#樹林
【C#】共通鍵暗号方式の暗号化キーと初期化ベクトル(IV)を生成する方法
概要 AESなどの共通鍵暗号方式(Symmetric Algorithm)で安全な通信を行うには、暗号化と復号で同じ「キー(Key)」と「初期化ベクトル(IV: Initialization Vector)」を共有する必要があります。 .NETの System.Security.Cryptography 名前空間に含まれ... -
C#樹林
[C#] How to Send Emails with Attachments Using MailKit
Overview When sending files such as images or documents via email, you must build a MIME (Multipurpose Internet Mail Extensions) multipart structure. This is different from a simple text email. By using the BodyBuilder class from the Mai... -
C#樹林
【C#】MailKitで添付ファイル付きのメールを送信する方法
概要 メールに画像やドキュメントなどのファイルを添付する場合、単純なテキストメールとは異なり、MIME(Multipurpose Internet Mail Extensions)のマルチパート構造を構築する必要があります。 MailKitライブラリの BodyBuilder クラスを使用することで... -
C#樹林
[C#] How to Send Emails Using MailKit
Overview When implementing email functionality in .NET, the standard System.Net.Mail.SmtpClient is now considered obsolete. Microsoft recommends using the open-source library MailKit instead. By using MailKit, you can easily and securely... -
C#樹林
【C#】MailKitを使用してメールを送信する方法
概要 .NETでメール送信機能を実装する場合、標準ライブラリの System.Net.Mail.SmtpClient は現在「旧式(Obsolete)」扱いとなっており、マイクロソフトはオープンソースライブラリである MailKit の使用を推奨しています。 MailKitを使用することで、最... -
C#樹林
[C#]How to Output HttpClient Communication Logs and Check Request Content
Overview When using IHttpClientFactory, you can output communication details such as HTTP request starts, ends, headers, and status codes simply by changing the log settings. This is a very effective debugging method for tracking what he... -
C#樹林
【C#】HttpClientの通信ログを出力してリクエスト内容を確認する方法
概要 IHttpClientFactory を使用している場合、ログ設定を変更するだけで HTTP リクエストの開始、終了、ヘッダー情報、ステータスコードなどの通信詳細をログに出力できます。 APIとの通信トラブル時に「どんなヘッダーを送ったか」「どのURLにアクセスし... -
C#樹林
[C#]How to Correctly Create HttpClient Using IHttpClientFactory in C#
Overview This implementation pattern uses IHttpClientFactory to solve issues like "socket exhaustion" and "DNS changes not being reflected," which occur when new HttpClient() is reused incorrectly or disposed of too quickly. You inject t... -
C#樹林
【C#】IHttpClientFactoryを使ってHttpClientを正しく生成する方法
概要 従来の new HttpClient() の使い回しや使い捨てによって発生する「ソケット枯渇問題」や「DNS変更が反映されない問題」を解決するために、推奨されている IHttpClientFactory を使用する実装パターンです。 Dependency Injection (DI) コンテナを介し... -
C#樹林
[C#] How to POST JSON Data with HttpClient
Overview This article covers the fundamental implementation for sending JSON-formatted data to a Web API. The process involves serializing a C# object into a JSON string and using the StringContent class to send a POST request with the C... -
C#樹林
【C#】HttpClientでJSONデータをPOST送信する方法
概要 Web APIに対してJSON形式のデータを送信する基本的な実装です。 オブジェクトをJSON文字列にシリアライズし、StringContent クラスを使用して Content-Type: application/json ヘッダーと共にPOSTリクエストを送信します。 仕様(入出力) 入力: 送信... -
C#樹林
[C#] How to POST Form Data (x-www-form-urlencoded) with HttpClient
Overview This method allows you to send data in the application/x-www-form-urlencoded format, which is the same format used by standard HTML <form> tags. By using the FormUrlEncodedContent class, key-value pairs are automatically U... -
C#樹林
【C#】HttpClientでフォームデータ(x-www-form-urlencoded)をPOST送信する方法
概要 Webページの一般的な入力フォーム(<form>タグ)と同じ形式である application/x-www-form-urlencoded でデータを送信する方法です。 FormUrlEncodedContent クラスを使用することで、キーと値のペアを自動的にURLエンコードし、適切な Content... -
C#樹林
[C#] Detailed Control over HTTP Methods and Headers using SendAsync
Overview While HttpClient provides convenient shorthand methods like GetAsync and PostAsync, the SendAsync method is the preferred choice for granular control. By utilizing the HttpRequestMessage class, you can set individual headers per... -
C#樹林
【C#】HTTPメソッドとヘッダーを詳細に制御してAPIを呼び出す方法
概要 HttpClient には GetAsync や PostAsync といった便利な短縮メソッドが用意されていますが、より細かい制御を行いたい場合は SendAsync メソッドを使用します。 HttpRequestMessage クラスを組み合わせることで、リクエストごとに個別のヘッダーを設... -
C#樹林
[C#] How to Branch Logic Based on HttpClient HTTP Status Codes
Overview When sending web requests using HttpClient, a communication success doesn't always mean the server processed the request as intended. The server might return "Page Not Found (404)" or an "Internal Error (500)." By checking the H... -
C#樹林
【C#】HttpClientでHTTPステータスコードを判定して分岐処理する方法
概要 HttpClient を使用してWebリクエストを送信した際、通信自体は成功しても、サーバー側で「ページが見つからない(404)」や「内部エラー(500)」が発生している場合があります。 HttpResponseMessage.StatusCode プロパティを確認することで、サーバ...