このチュートリアルでは、ファイルのコンテンツの暗号化および復号化の方法を示します。 コード例は、Windows フォーム アプリケーション向けに設計されています。 このアプリケーションは、スマート カードを使用するなどの実際のシナリオは示していません。 代わりに、暗号化と復号化の基礎を示しています。
このチュートリアルでは、次の暗号化のガイドラインを使用します。
自動生成される Key と IV を使用すると、対称アルゴリズムである Aes クラスでデータの暗号化と復号化を行えます。
RSA 非対称アルゴリズムを使用して、Aes によって暗号化されたデータのキーの暗号化と復号化を行います。 非対称アルゴリズムは、キーなどの少量のデータに最適です。
Note
暗号化されたコンテンツを他のユーザーとやり取りするのではなく、自分のコンピューター上のデータを保護する場合は、ProtectedData クラスの使用を検討してください。
次の表は、このトピックの暗号化のタスクをまとめたものです。
タスク | [説明] |
---|---|
Windows フォーム アプリケーションを作成する | アプリケーションを実行するために必要なコントロールの一覧を表示します。 |
グローバル オブジェクトを宣言する | 文字列のパスの変数、CspParameters、および RSACryptoServiceProvider を宣言して、Form クラスのグローバル コンテキストを指定します。 |
非対称キーを作成する | 非対称の公開および秘密キーの値のペアを作成し、これにキー コンテナー名を割り当てます。 |
ファイルを暗号化する | 暗号化するファイルを選択するダイアログ ボックスを表示し、ファイルを暗号化します。 |
ファイルを復号化する | 復号化する暗号化されたファイルを選択するダイアログ ボックスを表示し、ファイルを復号化します。 |
秘密キーを取得する | キー コンテナー名を使用して、完全なキーのペアを取得します。 |
公開キーをエクスポートする | パブリック パラメーターのみで XML ファイルにキーを保存します。 |
公開キーをインポートする | キーを XML ファイルからキー コンテナーに読み込みます。 |
アプリケーションをテストする | このアプリケーションをテストするための手順を一覧に示します。 |
必須コンポーネント
このチュートリアルを実行するには、次のコンポーネントが必要です。
- System.IO 名前空間と System.Security.Cryptography 名前空間への参照。
Windows フォーム アプリケーションを作成する
このチュートリアルにあるほとんどのコード例は、ボタン コントロールのイベント ハンドラーとして設計されています。 次の表は、サンプル アプリケーションに必要なコントロールと、コード例に一致する必要な名前を示しています。
コントロール | 名前 | テキストのプロパティ (必要に応じて) |
---|---|---|
Button | buttonEncryptFile |
ファイルの暗号化 |
Button | buttonDecryptFile |
ファイルの復号化 |
Button | buttonCreateAsmKeys |
キーの作成 |
Button | buttonExportPublicKey |
公開キーのエクスポート |
Button | buttonImportPublicKey |
公開キーのインポート |
Button | buttonGetPrivateKey |
秘密キーの取得 |
Label | label1 |
キーが設定されていません |
OpenFileDialog | _encryptOpenFileDialog |
|
OpenFileDialog | _decryptOpenFileDialog |
ボタンのイベント ハンドラーを作成するには、Visual Studio デザイナー内でそのボタンをダブルクリックします。
グローバル オブジェクトを宣言する
Form1 クラスの宣言の一部として、次のコードを追加します。 環境とユーザー設定のための文字列変数を編集します。
// Declare CspParameters and RsaCryptoServiceProvider
// objects with global scope of your Form class.
readonly CspParameters _cspp = new CspParameters();
RSACryptoServiceProvider _rsa;
// Path variables for source, encryption, and
// decryption folders. Must end with a backslash.
const string EncrFolder = @"c:\Encrypt\";
const string DecrFolder = @"c:\Decrypt\";
const string SrcFolder = @"c:\docs\";
// Public key file
const string PubKeyFile = @"c:\encrypt\rsaPublicKey.txt";
// Key container name for
// private/public key value pair.
const string KeyName = "Key01";
' Declare CspParameters and RsaCryptoServiceProvider
' objects with global scope of your Form class.
ReadOnly _cspp As New CspParameters
Dim _rsa As RSACryptoServiceProvider
' Path variables for source, encryption, and
' decryption folders. Must end with a backslash.
Const EncrFolder As String = "c:\Encrypt\"
Const DecrFolder As String = "c:\Decrypt\"
Const SrcFolder As String = "c:\docs\"
' Public key file
Const PubKeyFile As String = "c:\encrypt\rsaPublicKey.txt"
' Key container name for
' private/public key value pair.
Const KeyName As String = "Key01"
非対称キーを作成する
この作業では、Aes キーの暗号化と復号化を行う非対称キーを作成します。 このキーは、コンテンツの暗号化に使用されたもので、ラベル コントロールにキー コンテナー名を表示します。
次のコードを [Create Keys
] ボタン (buttonCreateAsmKeys_Click
) の Click
イベント ハンドラーとして追加します。
private void buttonCreateAsmKeys_Click(object sender, EventArgs e)
{
// Stores a key pair in the key container.
_cspp.KeyContainerName = KeyName;
_rsa = new RSACryptoServiceProvider(_cspp)
{
PersistKeyInCsp = true
};
label1.Text = _rsa.PublicOnly
? $"Key: {_cspp.KeyContainerName} - Public Only"
: $"Key: {_cspp.KeyContainerName} - Full Key Pair";
}
Private Sub buttonCreateAsmKeys_Click(ByVal sender As Object, ByVal e As EventArgs) Handles buttonCreateAsmKeys.Click
' Stores a key pair in the key container.
_cspp.KeyContainerName = KeyName
_rsa = New RSACryptoServiceProvider(_cspp) With {
.PersistKeyInCsp = True
}
If _rsa.PublicOnly Then
Label1.Text = $"Key: {_cspp.KeyContainerName} - Public Only"
Else
Label1.Text = $"Key: {_cspp.KeyContainerName} - Full Key Pair"
End If
End Sub
ファイルを暗号化する
このタスクには、Encrypt File
ボタンのイベント ハンドラー メソッド (buttonEncryptFile_Click
) と EncryptFile
メソッドという 2 つのメソッドが含まれています。 最初のメソッドは、ファイルを選択するためのダイアログ ボックスを表示し、暗号化を実行する 2 番目のメソッドにファイル名を渡します。
暗号化されたコンテンツ、キー、および IV は、すべて 1 つの FileStream に保存されます。これを暗号化パッケージといいます。
EncryptFile
メソッドは以下を実行します。
- コンテンツを暗号化する Aes 対称アルゴリズムを作成します。
- Aes キーを暗号化する RSACryptoServiceProvider オブジェクトを作成します。
- ソース ファイルの FileStream の読み取りと暗号化を行う CryptoStream オブジェクト (バイトのブロック) を使用して、暗号化ファイルの対象の FileStream オブジェクトにします。
- 暗号化されたキーと IV の長さを決定し、長さの値のバイト配列を作成します。
- 暗号化されたパッケージにキー、IV、および長さの値を書き込みます。
暗号化パッケージでは、次の形式を使用します。
- キーの長さ: 0 - 3 バイト
- IV の長さ: 4 - 7 バイト
- 暗号化されたキー
- IV
- 暗号化テキスト
キーと IV の長さを使用すると、暗号化パッケージのすべての部分の開始点と長さを決定することができます。これを使用してファイルを復号化します。
次のコードを [Encrypt File
] ボタン (buttonEncryptFile_Click
) の Click
イベント ハンドラーとして追加します。
private void buttonEncryptFile_Click(object sender, EventArgs e)
{
if (_rsa is null)
{
MessageBox.Show("Key not set.");
}
else
{
// Display a dialog box to select a file to encrypt.
_encryptOpenFileDialog.InitialDirectory = SrcFolder;
if (_encryptOpenFileDialog.ShowDialog() == DialogResult.OK)
{
string fName = _encryptOpenFileDialog.FileName;
if (fName != null)
{
// Pass the file name without the path.
EncryptFile(new FileInfo(fName));
}
}
}
}
Private Sub buttonEncryptFile_Click(ByVal sender As Object, ByVal e As EventArgs) Handles buttonEncryptFile.Click
If _rsa Is Nothing Then
MsgBox("Key not set.")
Else
' Display a dialog box to select a file to encrypt.
_encryptOpenFileDialog.InitialDirectory = SrcFolder
If _encryptOpenFileDialog.ShowDialog = Windows.Forms.DialogResult.OK Then
Try
Dim fName As String = _encryptOpenFileDialog.FileName
If (Not (fName) Is Nothing) Then
Dim fInfo As New FileInfo(fName)
' Use just the file name without path.
Dim name As String = fInfo.FullName
EncryptFile(name)
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End If
End If
End Sub
フォームに次の EncryptFile
メソッドを追加します。
private void EncryptFile(FileInfo file)
{
// Create instance of Aes for
// symmetric encryption of the data.
Aes aes = Aes.Create();
ICryptoTransform transform = aes.CreateEncryptor();
// Use RSACryptoServiceProvider to
// encrypt the AES key.
// rsa is previously instantiated:
// rsa = new RSACryptoServiceProvider(cspp);
byte[] keyEncrypted = _rsa.Encrypt(aes.Key, false);
// Create byte arrays to contain
// the length values of the key and IV.
int lKey = keyEncrypted.Length;
byte[] LenK = BitConverter.GetBytes(lKey);
int lIV = aes.IV.Length;
byte[] LenIV = BitConverter.GetBytes(lIV);
// Write the following to the FileStream
// for the encrypted file (outFs):
// - length of the key
// - length of the IV
// - encrypted key
// - the IV
// - the encrypted cipher content
// Change the file's extension to ".enc"
string outFile =
Path.Combine(EncrFolder, Path.ChangeExtension(file.Name, ".enc"));
using (var outFs = new FileStream(outFile, FileMode.Create))
{
outFs.Write(LenK, 0, 4);
outFs.Write(LenIV, 0, 4);
outFs.Write(keyEncrypted, 0, lKey);
outFs.Write(aes.IV, 0, lIV);
// Now write the cipher text using
// a CryptoStream for encrypting.
using (var outStreamEncrypted =
new CryptoStream(outFs, transform, CryptoStreamMode.Write))
{
// By encrypting a chunk at
// a time, you can save memory
// and accommodate large files.
int count = 0;
int offset = 0;
// blockSizeBytes can be any arbitrary size.
int blockSizeBytes = aes.BlockSize / 8;
byte[] data = new byte[blockSizeBytes];
int bytesRead = 0;
using (var inFs = new FileStream(file.FullName, FileMode.Open))
{
do
{
count = inFs.Read(data, 0, blockSizeBytes);
offset += count;
outStreamEncrypted.Write(data, 0, count);
bytesRead += blockSizeBytes;
} while (count > 0);
}
outStreamEncrypted.FlushFinalBlock();
}
}
}
Private Sub EncryptFile(ByVal inFile As String)
' Create instance of Aes for
' symmetric encryption of the data.
Dim aes As Aes = Aes.Create()
Dim transform As ICryptoTransform = aes.CreateEncryptor
' Use RSACryptoServiceProvider to
' encrypt the AES key.
Dim keyEncrypted() As Byte = _rsa.Encrypt(aes.Key, False)
' Create byte arrays to contain
' the length values of the key and IV.
Dim LenK() As Byte = New Byte((4) - 1) {}
Dim LenIV() As Byte = New Byte((4) - 1) {}
Dim lKey As Integer = keyEncrypted.Length
LenK = BitConverter.GetBytes(lKey)
Dim lIV As Integer = aes.IV.Length
LenIV = BitConverter.GetBytes(lIV)
' Write the following to the FileStream
' for the encrypted file (outFs):
' - length of the key
' - length of the IV
' - encrypted key
' - the IV
' - the encrypted cipher content
' Change the file's extension to ".enc"
Dim startFileName As Integer = inFile.LastIndexOf("\") + 1
Dim outFile As String = (EncrFolder _
+ (inFile.Substring(startFileName, inFile.LastIndexOf(".") - startFileName) + ".enc"))
Using outFs As New FileStream(outFile, FileMode.Create)
outFs.Write(LenK, 0, 4)
outFs.Write(LenIV, 0, 4)
outFs.Write(keyEncrypted, 0, lKey)
outFs.Write(aes.IV, 0, lIV)
' Now write the cipher text using
' a CryptoStream for encrypting.
Using outStreamEncrypted As New CryptoStream(outFs, transform, CryptoStreamMode.Write)
' By encrypting a chunk at
' a time, you can save memory
' and accommodate large files.
Dim count As Integer = 0
Dim offset As Integer = 0
' blockSizeBytes can be any arbitrary size.
Dim blockSizeBytes As Integer = (aes.BlockSize / 8)
Dim data() As Byte = New Byte((blockSizeBytes) - 1) {}
Dim bytesRead As Integer = 0
Using inFs As New FileStream(inFile, FileMode.Open)
Do
count = inFs.Read(data, 0, blockSizeBytes)
offset = (offset + count)
outStreamEncrypted.Write(data, 0, count)
bytesRead = (bytesRead + blockSizeBytes)
Loop Until (count = 0)
outStreamEncrypted.FlushFinalBlock()
inFs.Close()
End Using
outStreamEncrypted.Close()
End Using
outFs.Close()
End Using
End Sub
ファイルを復号化する
このタスクには、[Decrypt File
] ボタン (buttonDecryptFile_Click
) のイベント ハンドラー メソッドと DecryptFile
メソッドという 2 つのメソッドが含まれています。 最初のメソッドは、ファイルを選択するためのダイアログ ボックスを表示し、復号化を実行する 2 番目のメソッドにファイル名を渡します。
Decrypt
メソッドは以下を実行します。
- コンテンツを復号化するための Aes 対称アルゴリズムを作成します。
- 暗号化されたキーと IV の長さを取得するには、暗号化パッケージの FileStream の最初の 8 バイトを読み取ってバイト配列にします。
- キーと IV を暗号化パッケージから抽出してバイト配列にします。
- Aes キーを復号化する RSACryptoServiceProvider オブジェクトを作成します。
- CryptoStream オブジェクトを使用して、FileStream の暗号化パッケージの暗号テキスト セクションをバイトのブロックで読み取って復号化し、復号化されたファイルの FileStream オブジェクトにします。 これが終了すると、復号化は完了です。
次のコードを、[Decrypt File
] ボタンの Click
イベント ハンドラーとして追加します。
private void buttonDecryptFile_Click(object sender, EventArgs e)
{
if (_rsa is null)
{
MessageBox.Show("Key not set.");
}
else
{
// Display a dialog box to select the encrypted file.
_decryptOpenFileDialog.InitialDirectory = EncrFolder;
if (_decryptOpenFileDialog.ShowDialog() == DialogResult.OK)
{
string fName = _decryptOpenFileDialog.FileName;
if (fName != null)
{
DecryptFile(new FileInfo(fName));
}
}
}
}
Private Sub buttonDecryptFile_Click(ByVal sender As Object, ByVal e As EventArgs) Handles buttonDecryptFile.Click
If _rsa Is Nothing Then
MsgBox("Key not set.")
Else
' Display a dialog box to select the encrypted file.
_decryptOpenFileDialog.InitialDirectory = EncrFolder
If (_decryptOpenFileDialog.ShowDialog = Windows.Forms.DialogResult.OK) Then
Try
Dim fName As String = _decryptOpenFileDialog.FileName
If ((fName) IsNot Nothing) Then
Dim fi As New FileInfo(fName)
Dim name As String = fi.Name
DecryptFile(name)
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End If
End If
End Sub
フォームに次の DecryptFile
メソッドを追加します。
private void DecryptFile(FileInfo file)
{
// Create instance of Aes for
// symmetric decryption of the data.
Aes aes = Aes.Create();
// Create byte arrays to get the length of
// the encrypted key and IV.
// These values were stored as 4 bytes each
// at the beginning of the encrypted package.
byte[] LenK = new byte[4];
byte[] LenIV = new byte[4];
// Construct the file name for the decrypted file.
string outFile =
Path.ChangeExtension(file.FullName.Replace("Encrypt", "Decrypt"), ".txt");
// Use FileStream objects to read the encrypted
// file (inFs) and save the decrypted file (outFs).
using (var inFs = new FileStream(file.FullName, FileMode.Open))
{
inFs.Seek(0, SeekOrigin.Begin);
inFs.Read(LenK, 0, 3);
inFs.Seek(4, SeekOrigin.Begin);
inFs.Read(LenIV, 0, 3);
// Convert the lengths to integer values.
int lenK = BitConverter.ToInt32(LenK, 0);
int lenIV = BitConverter.ToInt32(LenIV, 0);
// Determine the start position of
// the cipher text (startC)
// and its length(lenC).
int startC = lenK + lenIV + 8;
int lenC = (int)inFs.Length - startC;
// Create the byte arrays for
// the encrypted Aes key,
// the IV, and the cipher text.
byte[] KeyEncrypted = new byte[lenK];
byte[] IV = new byte[lenIV];
// Extract the key and IV
// starting from index 8
// after the length values.
inFs.Seek(8, SeekOrigin.Begin);
inFs.Read(KeyEncrypted, 0, lenK);
inFs.Seek(8 + lenK, SeekOrigin.Begin);
inFs.Read(IV, 0, lenIV);
Directory.CreateDirectory(DecrFolder);
// Use RSACryptoServiceProvider
// to decrypt the AES key.
byte[] KeyDecrypted = _rsa.Decrypt(KeyEncrypted, false);
// Decrypt the key.
ICryptoTransform transform = aes.CreateDecryptor(KeyDecrypted, IV);
// Decrypt the cipher text from
// from the FileSteam of the encrypted
// file (inFs) into the FileStream
// for the decrypted file (outFs).
using (var outFs = new FileStream(outFile, FileMode.Create))
{
int count = 0;
int offset = 0;
// blockSizeBytes can be any arbitrary size.
int blockSizeBytes = aes.BlockSize / 8;
byte[] data = new byte[blockSizeBytes];
// By decrypting a chunk a time,
// you can save memory and
// accommodate large files.
// Start at the beginning
// of the cipher text.
inFs.Seek(startC, SeekOrigin.Begin);
using (var outStreamDecrypted =
new CryptoStream(outFs, transform, CryptoStreamMode.Write))
{
do
{
count = inFs.Read(data, 0, blockSizeBytes);
offset += count;
outStreamDecrypted.Write(data, 0, count);
} while (count > 0);
outStreamDecrypted.FlushFinalBlock();
}
}
}
}
Private Sub DecryptFile(ByVal inFile As String)
' Create instance of Aes for
' symmetric decryption of the data.
Dim aes As Aes = Aes.Create()
' Create byte arrays to get the length of
' the encrypted key and IV.
' These values were stored as 4 bytes each
' at the beginning of the encrypted package.
Dim LenK() As Byte = New Byte(4 - 1) {}
Dim LenIV() As Byte = New Byte(4 - 1) {}
' Construct the file name for the decrypted file.
Dim outFile As String = (DecrFolder _
+ (inFile.Substring(0, inFile.LastIndexOf(".")) + ".txt"))
' Use FileStream objects to read the encrypted
' file (inFs) and save the decrypted file (outFs).
Using inFs As New FileStream((EncrFolder + inFile), FileMode.Open)
inFs.Seek(0, SeekOrigin.Begin)
inFs.Read(LenK, 0, 3)
inFs.Seek(4, SeekOrigin.Begin)
inFs.Read(LenIV, 0, 3)
Dim lengthK As Integer = BitConverter.ToInt32(LenK, 0)
Dim lengthIV As Integer = BitConverter.ToInt32(LenIV, 0)
Dim startC As Integer = (lengthK + lengthIV + 8)
Dim lenC As Integer = (CType(inFs.Length, Integer) - startC)
Dim KeyEncrypted() As Byte = New Byte(lengthK - 1) {}
Dim IV() As Byte = New Byte(lengthIV - 1) {}
' Extract the key and IV
' starting from index 8
' after the length values.
inFs.Seek(8, SeekOrigin.Begin)
inFs.Read(KeyEncrypted, 0, lengthK)
inFs.Seek(8 + lengthK, SeekOrigin.Begin)
inFs.Read(IV, 0, lengthIV)
Directory.CreateDirectory(DecrFolder)
' User RSACryptoServiceProvider
' to decrypt the AES key
Dim KeyDecrypted() As Byte = _rsa.Decrypt(KeyEncrypted, False)
' Decrypt the key.
Dim transform As ICryptoTransform = aes.CreateDecryptor(KeyDecrypted, IV)
' Decrypt the cipher text from
' from the FileSteam of the encrypted
' file (inFs) into the FileStream
' for the decrypted file (outFs).
Using outFs As New FileStream(outFile, FileMode.Create)
Dim count As Integer = 0
Dim offset As Integer = 0
' blockSizeBytes can be any arbitrary size.
Dim blockSizeBytes As Integer = (aes.BlockSize / 8)
Dim data() As Byte = New Byte(blockSizeBytes - 1) {}
' By decrypting a chunk a time,
' you can save memory and
' accommodate large files.
' Start at the beginning
' of the cipher text.
inFs.Seek(startC, SeekOrigin.Begin)
Using outStreamDecrypted As New CryptoStream(outFs, transform, CryptoStreamMode.Write)
Do
count = inFs.Read(data, 0, blockSizeBytes)
offset += count
outStreamDecrypted.Write(data, 0, count)
Loop Until (count = 0)
outStreamDecrypted.FlushFinalBlock()
End Using
End Using
End Using
End Sub
公開キーをエクスポートする
この作業では、[Create Keys
] ボタンによって作成されたキーをファイルに保存します。 パブリック パラメーターのみがエクスポートされます。
このタスクでは、アリスがボブに公開キーを与えるシナリオをシミュレーションします。そうすることで、ボブはアリスのファイルを暗号化できるようになります。 ボブとその公開キーを持つ他のユーザーはファイルを復号化できなくなります。彼らはプライベート パラメーターを持つ完全なキーのペアを持っていないためです。
次のコードを [Export Public Key
] ボタン (buttonExportPublicKey_Click
) の Click
イベント ハンドラーとして追加します。
void buttonExportPublicKey_Click(object sender, EventArgs e)
{
// Save the public key created by the RSA
// to a file. Caution, persisting the
// key to a file is a security risk.
Directory.CreateDirectory(EncrFolder);
using (var sw = new StreamWriter(PubKeyFile, false))
{
sw.Write(_rsa.ToXmlString(false));
}
}
Private Sub buttonExportPublicKey_Click(ByVal sender As Object, ByVal e As EventArgs) Handles buttonExportPublicKey.Click
' Save the public key created by the RSA
' to a file. Caution, persisting the
' key to a file is a security risk.
Directory.CreateDirectory(EncrFolder)
Using sw As New StreamWriter(PubKeyFile)
sw.Write(_rsa.ToXmlString(False))
End Using
End Sub
公開キーをインポートする
このタスクでは、[Export Public Key
] ボタンによって作成されたパブリック パラメーターのみを持つキーを読み込み、キー コンテナー名として設定します。
この作業では、ボブがアリスのファイルを暗号化できるように、ボブがパブリック パラメーターのみを持つアリスのキーを読み込むシナリオをシミュレーションします。
次のコードを [Import Public Key
] ボタン (buttonImportPublicKey_Click
) の Click
イベント ハンドラーとして追加します。
void buttonImportPublicKey_Click(object sender, EventArgs e)
{
using (var sr = new StreamReader(PubKeyFile))
{
_cspp.KeyContainerName = KeyName;
_rsa = new RSACryptoServiceProvider(_cspp);
string keytxt = sr.ReadToEnd();
_rsa.FromXmlString(keytxt);
_rsa.PersistKeyInCsp = true;
label1.Text = _rsa.PublicOnly
? $"Key: {_cspp.KeyContainerName} - Public Only"
: $"Key: {_cspp.KeyContainerName} - Full Key Pair";
}
}
Private Sub buttonImportPublicKey_Click(ByVal sender As Object, ByVal e As EventArgs) Handles buttonImportPublicKey.Click
Using sr As New StreamReader(PubKeyFile)
_cspp.KeyContainerName = KeyName
_rsa = New RSACryptoServiceProvider(_cspp)
Dim keytxt As String = sr.ReadToEnd
_rsa.FromXmlString(keytxt)
_rsa.PersistKeyInCsp = True
If _rsa.PublicOnly Then
Label1.Text = $"Key: {_cspp.KeyContainerName} - Public Only"
Else
Label1.Text = $"Key: {_cspp.KeyContainerName} - Full Key Pair"
End If
End Using
End Sub
秘密キーを取得する
この作業では、[Create Keys
] ボタンを使用して作成されたキーの名前にキー コンテナー名を設定します。 キー コンテナーには、プライベート パラメーターを持つ完全なキーのペアが格納されます。
この作業では、アリスが自分の秘密キーを使用してボブが暗号化したファイルを復号化するシナリオをシミュレーションします。
次のコードを [Get Private Key
] ボタン (buttonGetPrivateKey_Click
) の Click
イベント ハンドラーとして追加します。
private void buttonGetPrivateKey_Click(object sender, EventArgs e)
{
_cspp.KeyContainerName = KeyName;
_rsa = new RSACryptoServiceProvider(_cspp)
{
PersistKeyInCsp = true
};
label1.Text = _rsa.PublicOnly
? $"Key: {_cspp.KeyContainerName} - Public Only"
: $"Key: {_cspp.KeyContainerName} - Full Key Pair";
}
Private Sub buttonGetPrivateKey_Click(ByVal sender As Object,
ByVal e As EventArgs) Handles buttonGetPrivateKey.Click
_cspp.KeyContainerName = KeyName
_rsa = New RSACryptoServiceProvider(_cspp) With {
.PersistKeyInCsp = True
}
If _rsa.PublicOnly Then
Label1.Text = $"Key: {_cspp.KeyContainerName} - Public Only"
Else
Label1.Text = $"Key: {_cspp.KeyContainerName} - Full Key Pair"
End If
End Sub
アプリケーションをテストする
アプリケーションをビルドしたら、次のテスト シナリオを実行します。
キーの作成、暗号化、および復号化を行うには
Create Keys
ボタンをクリックします。 ラベルはキー名を表示し、完全なキーのペアであることを示します。Export Public Key
ボタンをクリックします。 公開キーのパラメーターのエクスポートにより現在のキーは変更されないことに注意してください。- [
Encrypt File
] ボタンをクリックして、ファイルを選択します。 - [
Decrypt File
] ボタンをクリックし、暗号化したファイルを選択します。 - 復号化したファイルを調べます。
- 次のシナリオで保持されたキー コンテナーを取得するテストを実行するには、アプリケーションを閉じて再起動します。
公開キーを使用して暗号化するには
Import Public Key
ボタンをクリックします。 ラベルはキー名を表示し、公開キーのみであることを示します。- [
Encrypt File
] ボタンをクリックして、ファイルを選択します。 - [
Decrypt File
] ボタンをクリックし、暗号化したファイルを選択します。 復号化するには秘密キーが必要であるため、これは失敗します。
このシナリオでは、公開キーのみによる別のユーザーのファイルの暗号化をデモンストレーションします。 通常、そのユーザーは公開キーのみを与え、秘密キーは復号化のため与えないでおきます。
秘密キーを使用して復号化するには
Get Private Key
ボタンをクリックします。 ラベルはキー名を表示し、完全なキーのペアであるかどうかを示します。- [
Decrypt File
] ボタンをクリックし、暗号化したファイルを選択します。 復号化するための完全なキーのペアがあるため、これは成功します。
関連項目
- 暗号化モデル - 基本クラス ライブラリに暗号化がどのように実装されているかについて説明しています。
- Cryptographic Services
- クロスプラットフォーム暗号化
- ASP.NET Core のデータ保護
.NET