RSACryptoServiceProvider クラス
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
暗号化サービス プロバイダー (CSP) によって提供される RSA アルゴリズムの実装を使用して、非対称暗号化と復号化を実行します。 このクラスは継承できません。
public ref class RSACryptoServiceProvider sealed : System::Security::Cryptography::RSA
public ref class RSACryptoServiceProvider sealed : System::Security::Cryptography::RSA, System::Security::Cryptography::ICspAsymmetricAlgorithm
public sealed class RSACryptoServiceProvider : System.Security.Cryptography.RSA
[System.Runtime.InteropServices.ComVisible(true)]
public sealed class RSACryptoServiceProvider : System.Security.Cryptography.RSA, System.Security.Cryptography.ICspAsymmetricAlgorithm
public sealed class RSACryptoServiceProvider : System.Security.Cryptography.RSA, System.Security.Cryptography.ICspAsymmetricAlgorithm
type RSACryptoServiceProvider = class
inherit RSA
[<System.Runtime.InteropServices.ComVisible(true)>]
type RSACryptoServiceProvider = class
inherit RSA
interface ICspAsymmetricAlgorithm
type RSACryptoServiceProvider = class
inherit RSA
interface ICspAsymmetricAlgorithm
Public NotInheritable Class RSACryptoServiceProvider
Inherits RSA
Public NotInheritable Class RSACryptoServiceProvider
Inherits RSA
Implements ICspAsymmetricAlgorithm
- 継承
- 属性
- 実装
例
次のコード例では、 RSACryptoServiceProvider クラスを使用して文字列をバイト配列に暗号化し、バイトを文字列に復号化します。
using System;
using System.Security.Cryptography;
using System.Text;
class RSACSPSample
{
static void Main()
{
try
{
//Create a UnicodeEncoder to convert between byte array and string.
UnicodeEncoding ByteConverter = new UnicodeEncoding();
//Create byte arrays to hold original, encrypted, and decrypted data.
byte[] dataToEncrypt = ByteConverter.GetBytes("Data to Encrypt");
byte[] encryptedData;
byte[] decryptedData;
//Create a new instance of RSACryptoServiceProvider to generate
//public and private key data.
using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider())
{
//Pass the data to ENCRYPT, the public key information
//(using RSACryptoServiceProvider.ExportParameters(false),
//and a boolean flag specifying no OAEP padding.
encryptedData = RSAEncrypt(dataToEncrypt, RSA.ExportParameters(false), false);
//Pass the data to DECRYPT, the private key information
//(using RSACryptoServiceProvider.ExportParameters(true),
//and a boolean flag specifying no OAEP padding.
decryptedData = RSADecrypt(encryptedData, RSA.ExportParameters(true), false);
//Display the decrypted plaintext to the console.
Console.WriteLine("Decrypted plaintext: {0}", ByteConverter.GetString(decryptedData));
}
}
catch (ArgumentNullException)
{
//Catch this exception in case the encryption did
//not succeed.
Console.WriteLine("Encryption failed.");
}
}
public static byte[] RSAEncrypt(byte[] DataToEncrypt, RSAParameters RSAKeyInfo, bool DoOAEPPadding)
{
try
{
byte[] encryptedData;
//Create a new instance of RSACryptoServiceProvider.
using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider())
{
//Import the RSA Key information. This only needs
//to include the public key information.
RSA.ImportParameters(RSAKeyInfo);
//Encrypt the passed byte array and specify OAEP padding.
//OAEP padding is only available on Microsoft Windows XP or
//later.
encryptedData = RSA.Encrypt(DataToEncrypt, DoOAEPPadding);
}
return encryptedData;
}
//Catch and display a CryptographicException
//to the console.
catch (CryptographicException e)
{
Console.WriteLine(e.Message);
return null;
}
}
public static byte[] RSADecrypt(byte[] DataToDecrypt, RSAParameters RSAKeyInfo, bool DoOAEPPadding)
{
try
{
byte[] decryptedData;
//Create a new instance of RSACryptoServiceProvider.
using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider())
{
//Import the RSA Key information. This needs
//to include the private key information.
RSA.ImportParameters(RSAKeyInfo);
//Decrypt the passed byte array and specify OAEP padding.
//OAEP padding is only available on Microsoft Windows XP or
//later.
decryptedData = RSA.Decrypt(DataToDecrypt, DoOAEPPadding);
}
return decryptedData;
}
//Catch and display a CryptographicException
//to the console.
catch (CryptographicException e)
{
Console.WriteLine(e.ToString());
return null;
}
}
}
Imports System.Security.Cryptography
Imports System.Text
_
Class RSACSPSample
Shared Sub Main()
Try
'Create a UnicodeEncoder to convert between byte array and string.
Dim ByteConverter As New UnicodeEncoding()
'Create byte arrays to hold original, encrypted, and decrypted data.
Dim dataToEncrypt As Byte() = ByteConverter.GetBytes("Data to Encrypt")
Dim encryptedData() As Byte
Dim decryptedData() As Byte
'Create a new instance of RSACryptoServiceProvider to generate
'public and private key data.
Using RSA As New RSACryptoServiceProvider
'Pass the data to ENCRYPT, the public key information
'(using RSACryptoServiceProvider.ExportParameters(false),
'and a boolean flag specifying no OAEP padding.
encryptedData = RSAEncrypt(dataToEncrypt, RSA.ExportParameters(False), False)
'Pass the data to DECRYPT, the private key information
'(using RSACryptoServiceProvider.ExportParameters(true),
'and a boolean flag specifying no OAEP padding.
decryptedData = RSADecrypt(encryptedData, RSA.ExportParameters(True), False)
'Display the decrypted plaintext to the console.
Console.WriteLine("Decrypted plaintext: {0}", ByteConverter.GetString(decryptedData))
End Using
Catch e As ArgumentNullException
'Catch this exception in case the encryption did
'not succeed.
Console.WriteLine("Encryption failed.")
End Try
End Sub
Public Shared Function RSAEncrypt(ByVal DataToEncrypt() As Byte, ByVal RSAKeyInfo As RSAParameters, ByVal DoOAEPPadding As Boolean) As Byte()
Try
Dim encryptedData() As Byte
'Create a new instance of RSACryptoServiceProvider.
Using RSA As New RSACryptoServiceProvider
'Import the RSA Key information. This only needs
'toinclude the public key information.
RSA.ImportParameters(RSAKeyInfo)
'Encrypt the passed byte array and specify OAEP padding.
'OAEP padding is only available on Microsoft Windows XP or
'later.
encryptedData = RSA.Encrypt(DataToEncrypt, DoOAEPPadding)
End Using
Return encryptedData
'Catch and display a CryptographicException
'to the console.
Catch e As CryptographicException
Console.WriteLine(e.Message)
Return Nothing
End Try
End Function
Public Shared Function RSADecrypt(ByVal DataToDecrypt() As Byte, ByVal RSAKeyInfo As RSAParameters, ByVal DoOAEPPadding As Boolean) As Byte()
Try
Dim decryptedData() As Byte
'Create a new instance of RSACryptoServiceProvider.
Using RSA As New RSACryptoServiceProvider
'Import the RSA Key information. This needs
'to include the private key information.
RSA.ImportParameters(RSAKeyInfo)
'Decrypt the passed byte array and specify OAEP padding.
'OAEP padding is only available on Microsoft Windows XP or
'later.
decryptedData = RSA.Decrypt(DataToDecrypt, DoOAEPPadding)
'Catch and display a CryptographicException
'to the console.
End Using
Return decryptedData
Catch e As CryptographicException
Console.WriteLine(e.ToString())
Return Nothing
End Try
End Function
End Class
次のコード例では、 RSACryptoServiceProvider を使用して作成されたキー情報を RSAParameters オブジェクトにエクスポートします。
try
{
//Create a new RSACryptoServiceProvider object.
using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider())
{
//Export the key information to an RSAParameters object.
//Pass false to export the public key information or pass
//true to export public and private key information.
RSAParameters RSAParams = RSA.ExportParameters(false);
}
}
catch (CryptographicException e)
{
//Catch this exception in case the encryption did
//not succeed.
Console.WriteLine(e.Message);
}
Try
'Create a new RSACryptoServiceProvider object.
Dim RSA As New RSACryptoServiceProvider()
'Export the key information to an RSAParameters object.
'Pass false to export the public key information or pass
'true to export public and private key information.
Dim RSAParams As RSAParameters = RSA.ExportParameters(False)
Catch e As CryptographicException
'Catch this exception in case the encryption did
'not succeed.
Console.WriteLine(e.Message)
End Try
注釈
この API の詳細については、「 RSACryptoServiceProvider の補足 API 解説」を参照してください。
コンストラクター
| 名前 | 説明 |
|---|---|
| RSACryptoServiceProvider() |
ランダムなキー ペアを使用して、 RSACryptoServiceProvider クラスの新しいインスタンスを初期化します。 |
| RSACryptoServiceProvider(CspParameters) |
指定したパラメーターを使用して、 RSACryptoServiceProvider クラスの新しいインスタンスを初期化します。 |
| RSACryptoServiceProvider(Int32, CspParameters) |
指定したキー サイズとパラメーターを使用して、 RSACryptoServiceProvider クラスの新しいインスタンスを初期化します。 |
| RSACryptoServiceProvider(Int32) |
指定したキー サイズのランダムなキー ペアを使用して、 RSACryptoServiceProvider クラスの新しいインスタンスを初期化します。 |
フィールド
| 名前 | 説明 |
|---|---|
| KeySizeValue |
非対称アルゴリズムで使用されるキーの剰余のサイズをビット単位で表します。 (継承元 AsymmetricAlgorithm) |
| LegalKeySizesValue |
非対称アルゴリズムでサポートされるキー サイズを指定します。 (継承元 AsymmetricAlgorithm) |
プロパティ
| 名前 | 説明 |
|---|---|
| CspKeyContainerInfo |
暗号化キー ペアに関する追加情報を記述する CspKeyContainerInfo オブジェクトを取得します。 |
| KeyExchangeAlgorithm |
RSAのこの実装で使用できるキー交換アルゴリズムの名前を取得します。 |
| KeySize |
現在のキーのサイズを取得します。 |
| LegalKeySizes |
非対称アルゴリズムでサポートされているキー サイズを取得します。 |
| LegalKeySizes |
非対称アルゴリズムでサポートされているキー サイズを取得します。 (継承元 AsymmetricAlgorithm) |
| PersistKeyInCsp |
キーを暗号化サービス プロバイダー (CSP) に保持するかどうかを示す値を取得または設定します。 |
| PublicOnly |
RSACryptoServiceProvider オブジェクトに公開キーのみが含まれているかどうかを示す値を取得します。 |
| SignatureAlgorithm |
この RSAの実装で使用できる署名アルゴリズムの名前を取得します。 |
| UseMachineKeyStore |
ユーザー プロファイル ストアではなく、コンピューターのキー ストアにキーを保持するかどうかを示す値を取得または設定します。 |
メソッド
| 名前 | 説明 |
|---|---|
| Clear() |
AsymmetricAlgorithm クラスによって使用されるすべてのリソースを解放します。 (継承元 AsymmetricAlgorithm) |
| Decrypt(Byte[], Boolean) |
RSA アルゴリズムを使用してデータを復号化します。 |
| Decrypt(Byte[], RSAEncryptionPadding) |
指定したパディングを使用して、 RSA アルゴリズムで以前に暗号化されたデータを復号化します。 |
| DecryptValue(Byte[]) |
このメソッドは、現在のバージョンではサポートされていません。 |
| Dispose() |
AsymmetricAlgorithm クラスの現在のインスタンスで使用されているすべてのリソースを解放します。 (継承元 AsymmetricAlgorithm) |
| Dispose(Boolean) |
AsymmetricAlgorithm クラスによって使用されるアンマネージ リソースを解放し、必要に応じてマネージド リソースを解放します。 (継承元 AsymmetricAlgorithm) |
| Encrypt(Byte[], Boolean) |
RSA アルゴリズムを使用してデータを暗号化します。 |
| Encrypt(Byte[], RSAEncryptionPadding) |
指定したパディングを使用して、 RSA アルゴリズムを使用してデータを暗号化します。 |
| EncryptValue(Byte[]) |
このメソッドは、現在のバージョンではサポートされていません。 |
| Equals(Object) |
指定したオブジェクトが現在のオブジェクトと等しいかどうかを判断します。 (継承元 Object) |
| ExportCspBlob(Boolean) |
RSACryptoServiceProvider オブジェクトに関連付けられているキー情報を含む BLOB をエクスポートします。 |
| ExportEncryptedPkcs8PrivateKey(ReadOnlySpan<Byte>, PbeParameters) |
PKCS#8 EncryptedPrivateKeyInfo 形式の現在のキーをバイトベースのパスワードでエクスポートします。 (継承元 AsymmetricAlgorithm) |
| ExportEncryptedPkcs8PrivateKey(ReadOnlySpan<Char>, PbeParameters) |
現在のキーを PKCS#8 EncryptedPrivateKeyInfo 形式で char ベースのパスワードでエクスポートします。 (継承元 AsymmetricAlgorithm) |
| ExportParameters(Boolean) |
RSAParametersをエクスポートします。 |
| ExportPkcs8PrivateKey() |
PKCS#8 PrivateKeyInfo 形式で現在のキーをエクスポートします。 (継承元 AsymmetricAlgorithm) |
| ExportRSAPrivateKey() |
PKCS#1 RSAPrivateKey 形式で現在のキーをエクスポートします。 (継承元 RSA) |
| ExportRSAPublicKey() |
現在のキーの公開キー部分を PKCS#1 RSAPublicKey 形式でエクスポートします。 (継承元 RSA) |
| ExportSubjectPublicKeyInfo() |
現在のキーの公開キー部分を X.509 SubjectPublicKeyInfo 形式でエクスポートします。 (継承元 AsymmetricAlgorithm) |
| Finalize() |
このインスタンスによって保持されているアンマネージ リソースを解放します。 |
| FromXmlString(String) |
XML 文字列のキー情報から RSA オブジェクトを初期化します。 (継承元 RSA) |
| GetHashCode() |
既定のハッシュ関数として機能します。 (継承元 Object) |
| GetType() |
現在のインスタンスの Type を取得します。 (継承元 Object) |
| HashData(Byte[], Int32, Int32, HashAlgorithmName) |
派生クラスでオーバーライドされた場合は、指定したハッシュ アルゴリズムを使用して、バイト配列の指定した部分のハッシュ値を計算します。 (継承元 RSA) |
| HashData(Stream, HashAlgorithmName) |
派生クラスでオーバーライドされると、指定したハッシュ アルゴリズムを使用して、指定したバイナリ ストリームのハッシュ値を計算します。 (継承元 RSA) |
| ImportCspBlob(Byte[]) |
RSA キー情報を表す BLOB をインポートします。 |
| ImportEncryptedPkcs8PrivateKey(ReadOnlySpan<Byte>, ReadOnlySpan<Byte>, Int32) |
派生クラスでオーバーライドされると、バイトベースのパスワードで復号化した後、PKCS#8 EncryptedPrivateKeyInfo 構造体から公開/秘密キーペアをインポートし、このオブジェクトのキーを置き換えます。 (継承元 AsymmetricAlgorithm) |
| ImportEncryptedPkcs8PrivateKey(ReadOnlySpan<Char>, ReadOnlySpan<Byte>, Int32) |
派生クラスでオーバーライドされると、char ベースのパスワードで復号化した後、PKCS#8 EncryptedPrivateKeyInfo 構造体から公開/秘密キーペアをインポートし、このオブジェクトのキーを置き換えます。 (継承元 AsymmetricAlgorithm) |
| ImportParameters(RSAParameters) |
指定した RSAParametersをインポートします。 |
| ImportPkcs8PrivateKey(ReadOnlySpan<Byte>, Int32) |
派生クラスでオーバーライドされると、復号化後に PKCS#8 PrivateKeyInfo 構造体から公開/秘密キーペアをインポートし、このオブジェクトのキーを置き換えます。 (継承元 AsymmetricAlgorithm) |
| ImportRSAPrivateKey(ReadOnlySpan<Byte>, Int32) |
暗号化解除後に PKCS#1 RSAPrivateKey 構造体から公開/秘密キーペアをインポートし、このオブジェクトのキーを置き換えます。 (継承元 RSA) |
| ImportRSAPublicKey(ReadOnlySpan<Byte>, Int32) |
復号化後に PKCS#1 RSAPublicKey 構造体から公開キーをインポートし、このオブジェクトのキーを置き換えます。 (継承元 RSA) |
| ImportSubjectPublicKeyInfo(ReadOnlySpan<Byte>, Int32) |
派生クラスでオーバーライドされると、復号化後に X.509 SubjectPublicKeyInfo 構造体から公開キーをインポートし、このオブジェクトのキーを置き換えます。 (継承元 AsymmetricAlgorithm) |
| MemberwiseClone() |
現在の Objectの簡易コピーを作成します。 (継承元 Object) |
| SignData(Byte[], HashAlgorithmName, RSASignaturePadding) |
指定したハッシュ アルゴリズムとパディング モードを使用して、指定したバイト配列のハッシュ値を計算し、結果のハッシュ値に署名します。 (継承元 RSA) |
| SignData(Byte[], Int32, Int32, HashAlgorithmName, RSASignaturePadding) |
指定したハッシュ アルゴリズムとパディング モードを使用して、指定したバイト配列の一部のハッシュ値を計算し、結果のハッシュ値に署名します。 (継承元 RSA) |
| SignData(Byte[], Int32, Int32, Object) |
指定したハッシュ アルゴリズムを使用して、指定したバイト配列のサブセットのハッシュ値を計算し、結果のハッシュ値に署名します。 |
| SignData(Byte[], Object) |
指定したハッシュ アルゴリズムを使用して、指定したバイト配列のハッシュ値を計算し、結果のハッシュ値に署名します。 |
| SignData(Stream, HashAlgorithmName, RSASignaturePadding) |
指定したハッシュ アルゴリズムとパディング モードを使用して、指定したストリームのハッシュ値を計算し、結果のハッシュ値に署名します。 (継承元 RSA) |
| SignData(Stream, Object) |
指定したハッシュ アルゴリズムを使用して、指定した入力ストリームのハッシュ値を計算し、結果のハッシュ値に署名します。 |
| SignHash(Byte[], HashAlgorithmName, RSASignaturePadding) |
指定したパディングを使用して、指定したハッシュ値の署名を計算します。 |
| SignHash(Byte[], String) |
指定したハッシュ値の署名を計算します。 |
| ToString() |
現在のオブジェクトを表す文字列を返します。 (継承元 Object) |
| ToXmlString(Boolean) |
現在の RSA オブジェクトのキーを含む XML 文字列を作成して返します。 (継承元 RSA) |
| TryDecrypt(ReadOnlySpan<Byte>, Span<Byte>, RSAEncryptionPadding, Int32) |
指定したパディング モードを使用して入力データの暗号化を解除し、指定されたバッファーに結果を書き込みます。 (継承元 RSA) |
| TryEncrypt(ReadOnlySpan<Byte>, Span<Byte>, RSAEncryptionPadding, Int32) |
指定した埋め込みモードで入力データを指定されたバッファーに暗号化しようとします。 (継承元 RSA) |
| TryExportEncryptedPkcs8PrivateKey(ReadOnlySpan<Byte>, PbeParameters, Span<Byte>, Int32) |
派生クラスでオーバーライドされると、バイトベースのパスワードを使用して、PKCS#8 EncryptedPrivateKeyInfo 形式の現在のキーを指定されたバッファーにエクスポートしようとします。 (継承元 AsymmetricAlgorithm) |
| TryExportEncryptedPkcs8PrivateKey(ReadOnlySpan<Char>, PbeParameters, Span<Byte>, Int32) |
派生クラスでオーバーライドされると、文字ベースのパスワードを使用して、PKCS#8 EncryptedPrivateKeyInfo 形式の現在のキーを指定されたバッファーにエクスポートしようとします。 (継承元 AsymmetricAlgorithm) |
| TryExportPkcs8PrivateKey(Span<Byte>, Int32) |
派生クラスでオーバーライドされると、PKCS#8 PrivateKeyInfo 形式の現在のキーを指定されたバッファーにエクスポートしようとします。 (継承元 AsymmetricAlgorithm) |
| TryExportRSAPrivateKey(Span<Byte>, Int32) |
PKCS#1 RSAPrivateKey 形式の現在のキーを指定されたバッファーにエクスポートしようとします。 (継承元 RSA) |
| TryExportRSAPublicKey(Span<Byte>, Int32) |
PKCS#1 RSAPublicKey 形式の現在のキーを指定されたバッファーにエクスポートしようとします。 (継承元 RSA) |
| TryExportSubjectPublicKeyInfo(Span<Byte>, Int32) |
派生クラスでオーバーライドされると、X.509 SubjectPublicKeyInfo 形式の現在のキーを指定されたバッファーにエクスポートしようとします。 (継承元 AsymmetricAlgorithm) |
| TryHashData(ReadOnlySpan<Byte>, Span<Byte>, HashAlgorithmName, Int32) |
指定したアルゴリズムを使用して、指定されたデータのハッシュを計算し、指定されたバッファーに結果を書き込みます。 (継承元 RSA) |
| TrySignData(ReadOnlySpan<Byte>, Span<Byte>, HashAlgorithmName, RSASignaturePadding, Int32) |
指定されたアルゴリズムを使用して指定されたデータをハッシュし、現在のキーでハッシュに署名し、指定されたバッファーに署名を書き込みます。 (継承元 RSA) |
| TrySignHash(ReadOnlySpan<Byte>, Span<Byte>, HashAlgorithmName, RSASignaturePadding, Int32) |
指定されたバッファーに署名を書き込み、現在のキーを使用してハッシュに署名しようとします。 (継承元 RSA) |
| VerifyData(Byte[], Byte[], HashAlgorithmName, RSASignaturePadding) |
指定したハッシュ アルゴリズムとパディングを使用して指定したデータのハッシュ値を計算し、指定された署名と比較することで、デジタル署名が有効であることを確認します。 (継承元 RSA) |
| VerifyData(Byte[], Int32, Int32, Byte[], HashAlgorithmName, RSASignaturePadding) |
指定したハッシュ アルゴリズムとパディングを使用してバイト配列の一部のデータのハッシュ値を計算し、指定されたシグネチャと比較することで、デジタル署名が有効であることを確認します。 (継承元 RSA) |
| VerifyData(Byte[], Object, Byte[]) |
指定された公開キーを使用して署名のハッシュ値を決定し、指定されたデータのハッシュ値と比較することで、デジタル署名が有効であることを確認します。 |
| VerifyData(ReadOnlySpan<Byte>, ReadOnlySpan<Byte>, HashAlgorithmName, RSASignaturePadding) |
指定したハッシュ アルゴリズムとパディングを使用して指定したデータのハッシュ値を計算し、指定された署名と比較することで、デジタル署名が有効であることを確認します。 (継承元 RSA) |
| VerifyData(Stream, Byte[], HashAlgorithmName, RSASignaturePadding) |
指定したハッシュ アルゴリズムとパディングを使用して指定したストリームのハッシュ値を計算し、指定された署名と比較することで、デジタル署名が有効であることを確認します。 (継承元 RSA) |
| VerifyHash(Byte[], Byte[], HashAlgorithmName, RSASignaturePadding) |
指定したハッシュ アルゴリズムとパディングを使用して署名のハッシュ値を決定し、指定されたハッシュ値と比較することで、デジタル署名が有効であることを確認します。 |
| VerifyHash(Byte[], String, Byte[]) |
指定された公開キーを使用して署名のハッシュ値を決定し、指定されたハッシュ値と比較することで、デジタル署名が有効であることを確認します。 |
| VerifyHash(ReadOnlySpan<Byte>, ReadOnlySpan<Byte>, HashAlgorithmName, RSASignaturePadding) |
指定したハッシュ アルゴリズムとパディングを使用して署名のハッシュ値を決定し、指定されたハッシュ値と比較することで、デジタル署名が有効であることを確認します。 (継承元 RSA) |
明示的なインターフェイスの実装
| 名前 | 説明 |
|---|---|
| IDisposable.Dispose() |
この API は製品インフラストラクチャをサポートします。コードから直接使用するものではありません。 このメンバーの説明については、 Dispose()を参照してください。 (継承元 AsymmetricAlgorithm) |