Language
暗号化解除は、暗号化の逆操作です。 秘密鍵暗号化の場合は、データの暗号化に使用されたキーと IV の両方を知っている必要があります。 公開キー暗号化の場合は、公開キー (データが秘密キーを使用して暗号化された場合) または秘密キー (データが公開キーを使用して暗号化された場合) を知っている必要があります。
対称復号
Important
CBC モードを使用した対称復号化 ( Aes.Create() の既定値) は、暗号化テキストの整合性が復号化前に検証されない場合、パディング Oracle 攻撃に対して脆弱です。 暗号化解除を試みる前に、常にデータの整合性を確認します (たとえば、Encrypt-then-MAC パターンで HMAC を使用)。 詳細については、「パディングを 使用した CBC モードの対称暗号化解除によるタイミングの脆弱性」を参照してください。
対称アルゴリズムで暗号化されたデータの暗号化解除は、対称アルゴリズムを使用してデータを暗号化するために使用されるプロセスに似ています。 CryptoStream クラスは、マネージド ストリーム オブジェクトから読み取られたデータを復号化するために、.NETによって提供される対称暗号化クラスと共に使用されます。
次の例は、 Aes アルゴリズムの既定の実装クラスの新しいインスタンスを作成する方法を示しています。 インスタンスは、 CryptoStream オブジェクトに対して復号化を実行するために使用されます。 この例では、まず、 Aes 実装クラスの新しいインスタンスを作成します。 マネージド ストリーム変数fileStreamから初期化ベクター (IV) 値を読み取ります。 次に、 CryptoStream オブジェクトをインスタンス化し、 fileStream インスタンスの値に初期化します。
Aes インスタンスのSymmetricAlgorithm.CreateDecryptorメソッドには、暗号化に使用された IV 値と同じキーが渡されます。
Dim aes As Aes = Aes.Create()
Dim cryptStream As New CryptoStream(
fileStream, aes.CreateDecryptor(key, iv), CryptoStreamMode.Read)
Aes aes = Aes.Create();
CryptoStream cryptStream = new CryptoStream(
fileStream, aes.CreateDecryptor(key, iv), CryptoStreamMode.Read);
次の例は、ストリームの作成、ストリームの暗号化解除、ストリームからの読み取り、ストリームの閉じるプロセス全体を示しています。 TestData.txtという名前のファイルを読み取るファイル ストリーム オブジェクトが作成されます。 その後、 CryptoStream クラスと Aes クラスを使用して、ファイル ストリームの暗号化が解除されます。 この例では、データの暗号化 の対称暗号化の例で使用されるのと同じキー値を指定します。 これらの値の暗号化と転送に必要なコードは表示されません。
using System.Security.Cryptography;
try
{
using (FileStream fileStream = new("TestData.txt", FileMode.Open))
{
using (Aes aes = Aes.Create())
{
byte[] iv = new byte[aes.IV.Length];
int numBytesToRead = aes.IV.Length;
int numBytesRead = 0;
while (numBytesToRead > 0)
{
int n = fileStream.Read(iv, numBytesRead, numBytesToRead);
if (n == 0) break;
numBytesRead += n;
numBytesToRead -= n;
}
// This key is for illustration purposes only and must
// match the key used during encryption.
// In production, use a securely stored or managed key.
byte[] key =
{
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16
};
using (CryptoStream cryptoStream = new(
fileStream,
aes.CreateDecryptor(key, iv),
CryptoStreamMode.Read))
{
// By default, the StreamReader uses UTF-8 encoding.
// To change the text encoding, pass the desired encoding as the second parameter.
// For example, new StreamReader(cryptoStream, Encoding.Unicode).
using (StreamReader decryptReader = new(cryptoStream))
{
string decryptedMessage = await decryptReader.ReadToEndAsync();
Console.WriteLine($"The decrypted original message: {decryptedMessage}");
}
}
}
}
}
catch (Exception ex)
{
Console.WriteLine($"The decryption failed. {ex}");
}
Imports System
Imports System.IO
Imports System.Security.Cryptography
Module Module1
Sub Main()
' This key is for illustration purposes only and must
' match the key used during encryption.
' In production, use a securely stored or managed key.
Dim key As Byte() = {&H1, &H2, &H3, &H4, &H5, &H6, &H7, &H8, &H9, &H10, &H11, &H12, &H13, &H14, &H15, &H16}
Try
' Create a file stream.
Using fileStream As New FileStream("TestData.txt", FileMode.Open)
' Create a new instance of the default Aes implementation class
Using aes As Aes = Aes.Create()
' Reads IV value from beginning of the file.
Dim iv As Byte() = New Byte(aes.IV.Length - 1) {}
Dim numBytesToRead As Integer = CType(aes.IV.Length, Integer)
Dim numBytesRead As Integer = 0
While (numBytesToRead > 0)
Dim n As Integer = fileStream.Read(iv, numBytesRead, numBytesToRead)
If n = 0 Then
Exit While
End If
numBytesRead += n
numBytesToRead -= n
End While
Using cryptoStream As New CryptoStream(fileStream, aes.CreateDecryptor(key, iv), CryptoStreamMode.Read)
' By default, the StreamReader uses UTF-8 encoding.
' To change the text encoding, pass the desired encoding as the second parameter.
' For example, New StreamReader(cryptoStream, Encoding.Unicode).
Using decryptReader As New StreamReader(cryptoStream)
' Display the message.
Console.WriteLine($"The decrypted original message: {decryptReader.ReadToEnd()}")
End Using
End Using
End Using
End Using
Catch
Console.WriteLine("The decryption Failed.")
Throw
End Try
End Sub
End Module
上の例では、データの暗号化の対称暗号化の例で使用されたのと同じキーとアルゴリズムを使用しています。 この例で作成された TestData.txt ファイルの暗号化を解除し、元のテキストをコンソールに表示します。
非対称復号
通常、パーティー (パーティー A) は公開キーと秘密キーの両方を生成し、キーをメモリまたは暗号化キー コンテナーに格納します。 パーティー A は、公開キーを別のパーティ (パーティ B) に送信します。 公開キーを使用して、パーティ B はデータを暗号化し、データをパーティ A に送り返します。データを受信した後、パーティ A は対応する秘密キーを使用して暗号化を解除します。 復号化は、パーティ A がデータの暗号化に使用したパーティ B の公開キーに対応する秘密キーを使用する場合にのみ成功します。
非対称キーをセキュリティで保護された暗号化キー コンテナーに格納する方法と、後で非対称キーを取得する方法については、「 方法: キー コンテナーに非対称キーを格納する」を参照してください。
次の例は、対称キーと IV を表す 2 つのバイト配列の復号化を示しています。 第三者に簡単に送信できる形式で RSA オブジェクトから非対称公開キーを抽出する方法については、「 データの暗号化」を参照してください。
'Create a new instance of the RSA class. The 2048-bit key size shown
'here is for illustration; choose a key size that matches your
'security requirements per NIST SP 800-57.
Dim rsa As RSA = RSA.Create(2048)
' Export the public key information and send it to a third party.
' Wait for the third party to encrypt some data and send it back.
'Decrypt the symmetric key and IV using OAEP padding.
symmetricKey = rsa.Decrypt(encryptedSymmetricKey, RSAEncryptionPadding.OaepSHA256)
symmetricIV = rsa.Decrypt(encryptedSymmetricIV, RSAEncryptionPadding.OaepSHA256)
// Create a new instance of the RSA class. The 2048-bit key size shown
// here is for illustration; choose a key size that matches your
// security requirements per NIST SP 800-57.
RSA rsa = RSA.Create(2048);
// Export the public key information and send it to a third party.
// Wait for the third party to encrypt some data and send it back.
// Decrypt the symmetric key and IV using OAEP padding.
symmetricKey = rsa.Decrypt(encryptedSymmetricKey, RSAEncryptionPadding.OaepSHA256);
symmetricIV = rsa.Decrypt(encryptedSymmetricIV, RSAEncryptionPadding.OaepSHA256);
参照
.NET