.NET 暗号化モデル
.NET には、多くの標準的な暗号アルゴリズムの実装が用意されており、.NET 暗号化モデルは拡張可能です。
オブジェクトの継承
.NET 暗号化システムは、拡張可能な派生クラス継承のパターンを実装しています。 階層は次のとおりです。
SymmetricAlgorithm、AsymmetricAlgorithm、HashAlgorithm などのアルゴリズム型クラス。 このレベルは抽象レベルです。
Aes、RSA、または ECDiffieHellman など、アルゴリズム型クラスから継承されるアルゴリズム クラス。 このレベルは抽象レベルです。
AesManaged、RC2CryptoServiceProvider、または ECDiffieHellmanCng など、アルゴリズム クラスから継承されるアルゴリズム クラスの実装。 このレベルは完全に実装されます。
派生クラスのこのパターンを使用すると、新しいアルゴリズムの追加、または既存アルゴリズムの新規実装の追加を行うことができます。 たとえば、新しい公開キー アルゴリズムを作成するには、AsymmetricAlgorithm クラスから継承します。 特定のアルゴリズムの実装を新しく作成するには、そのアルゴリズムの非抽象派生クラスを作成します。
.NET でのアルゴリズムの実装方法
アルゴリズムに使用できるさまざまな実装例として、対称アルゴリズムを検討します。 すべての対称アルゴリズムの基本は SymmetricAlgorithm です。これは、Aes、TripleDES、および推奨されなくなったその他のものによって継承されます。
Aes は、AesCryptoServiceProvider、AesCng、AesManaged により継承されます。
Windows 上の .NET Framework の場合:
*CryptoServiceProvider
アルゴリズム クラス (AesCryptoServiceProvider など) は、アルゴリズムの Windows Cryptography API (CAPI) 実装のラッパーです。*Cng
アルゴリズム クラス (ECDiffieHellmanCng など) は、Windows Cryptography Next Generation (CNG) 実装のラッパーです。- クラス AesManaged (
*Managed
など) は、すべてマネージド コードで記述されています。*Managed
は連邦情報処理規格 (FIPS: Federal Information Processing Standards) によって認定されておらず、*CryptoServiceProvider
と*Cng
ラッパー クラスよりも低速である場合があります。
.NET Core と .NET 5 以降のバージョンでは、すべての実装クラス (*CryptoServiceProvider
、*Managed
、*Cng
) は、オペレーティング システム (OS) アルゴリズムのラッパーです。 OS アルゴリズムが FIPS によって認定されている場合、.NET は FIPS によって認定されたアルゴリズムを使用します。 詳細については、クロスプラットフォーム暗号化に関するページをご覧ください。
ほとんどの場合、AesCryptoServiceProvider
などのアルゴリズム実装クラスを直接参照する必要はありません。 通常、必要とされるメソッドとプロパティは、Aes
などの基本アルゴリズム クラスにあります。 基本アルゴリズム クラスのファクトリ メソッドを使用して既定の実装クラスのインスタンスを作成し、基本アルゴリズム クラスを参照します。 たとえば、次の例で強調表示されているコード行をご確認ください。
using System.Security.Cryptography;
try
{
using (FileStream fileStream = new("TestData.txt", FileMode.OpenOrCreate))
{
using (Aes aes = Aes.Create())
{
byte[] key =
{
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16
};
aes.Key = key;
byte[] iv = aes.IV;
fileStream.Write(iv, 0, iv.Length);
using (CryptoStream cryptoStream = new(
fileStream,
aes.CreateEncryptor(),
CryptoStreamMode.Write))
{
// By default, the StreamWriter uses UTF-8 encoding.
// To change the text encoding, pass the desired encoding as the second parameter.
// For example, new StreamWriter(cryptoStream, Encoding.Unicode).
using (StreamWriter encryptWriter = new(cryptoStream))
{
encryptWriter.WriteLine("Hello World!");
}
}
}
}
Console.WriteLine("The file was encrypted.");
}
catch (Exception ex)
{
Console.WriteLine($"The encryption failed. {ex}");
}
Imports System
Imports System.IO
Imports System.Security.Cryptography
Module Module1
Sub Main()
Try
' Create a file stream
Using fileStream As New FileStream("TestData.txt", FileMode.OpenOrCreate)
' Create a new instance of the default Aes implementation class
' and configure encryption key.
Using aes As Aes = Aes.Create()
'Encryption key used to encrypt the stream.
'The same value must be used to encrypt and decrypt the stream.
Dim key As Byte() = {
&H1, &H2, &H3, &H4, &H5, &H6, &H7, &H8,
&H9, &H10, &H11, &H12, &H13, &H14, &H15, &H16
}
aes.Key = key
' Stores IV at the beginning of the file.
' This information will be used for decryption.
Dim iv As Byte() = aes.IV
fileStream.Write(iv, 0, iv.Length)
' Create a CryptoStream, pass it the FileStream, and encrypt
' it with the Aes class.
Using cryptoStream As New CryptoStream(fileStream, aes.CreateEncryptor(), CryptoStreamMode.Write)
' By default, the StreamWriter uses UTF-8 encoding.
' To change the text encoding, pass the desired encoding as the second parameter.
' For example, New StreamWriter(cryptoStream, Encoding.Unicode).
Using sWriter As New StreamWriter(cryptoStream)
'Write to the stream.
sWriter.WriteLine("Hello World!")
End Using
End Using
End Using
End Using
'Inform the user that the message was written
'to the stream.
Console.WriteLine("The text was encrypted.")
Catch
'Inform the user that an exception was raised.
Console.WriteLine("The encryption failed.")
Throw
End Try
End Sub
End Module
暗号化の構成
暗号化の構成によって、アルゴリズムの特定の実装をアルゴリズム名に解決できるようになり、.NET 暗号化クラスの機能を拡張できます。 アルゴリズムの独自のハードウェアまたはソフトウェア実装を追加して、実装を任意のアルゴリズム名にマップすることができます。 構成ファイルでアルゴリズムを指定しない場合は、既定の設定が使用されます。
アルゴリズムの選択
データの整合性、データのプライバシー保護、またはキー生成など、さまざまな理由のためにアルゴリズムを選択することができます。 対称アルゴリズムおよびハッシュ アルゴリズムは、整合性の理由 (変更の防止) またはプライバシー上の理由 (表示の防止) のいずれかのためにデータを保護することを意図しています。 ハッシュ アルゴリズムは、主にデータの整合性用に使用されます。
アプリケーションで推奨されるアルゴリズムの一覧を示します。
- データのプライバシー :
- データの整合性 :
- デジタル署名 :
- キー交換 :
- 乱数生成 :
- パスワードからのキー生成 :