RSACryptoServiceProvider 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
使用加密服务提供程序 (CSP) 提供的 RSA 算法的实现执行非对称加密和解密。 此类不能被继承。
public ref class RSACryptoServiceProvider sealed : System::Security::Cryptography::RSA, System::Security::Cryptography::ICspAsymmetricAlgorithm
public ref class RSACryptoServiceProvider sealed : System::Security::Cryptography::RSA
public sealed class RSACryptoServiceProvider : 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
type RSACryptoServiceProvider = class
inherit RSA
interface ICspAsymmetricAlgorithm
type RSACryptoServiceProvider = class
inherit RSA
[<System.Runtime.InteropServices.ComVisible(true)>]
type RSACryptoServiceProvider = class
inherit RSA
interface ICspAsymmetricAlgorithm
Public NotInheritable Class RSACryptoServiceProvider
Inherits RSA
Implements ICspAsymmetricAlgorithm
Public NotInheritable Class RSACryptoServiceProvider
Inherits RSA
- 继承
- 属性
- 实现
示例
下面的代码示例使用 RSACryptoServiceProvider 类将字符串加密为字节数组,然后将字节解密回字符串。
using namespace System;
using namespace System::Security::Cryptography;
using namespace System::Text;
array<Byte>^ RSAEncrypt( array<Byte>^DataToEncrypt, RSAParameters RSAKeyInfo, bool DoOAEPPadding )
{
try
{
//Create a new instance of RSACryptoServiceProvider.
RSACryptoServiceProvider^ RSA = gcnew 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.
array<Byte>^encryptedData = RSA->Encrypt( DataToEncrypt, DoOAEPPadding );
delete RSA;
return encryptedData;
}
//Catch and display a CryptographicException
//to the console.
catch ( CryptographicException^ e )
{
Console::WriteLine( e->Message );
return nullptr;
}
}
array<Byte>^ RSADecrypt( array<Byte>^DataToDecrypt, RSAParameters RSAKeyInfo, bool DoOAEPPadding )
{
try
{
//Create a new instance of RSACryptoServiceProvider.
RSACryptoServiceProvider^ RSA = gcnew 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.
array<Byte>^decryptedData = RSA->Decrypt( DataToDecrypt, DoOAEPPadding );
delete RSA;
return decryptedData;
}
//Catch and display a CryptographicException
//to the console.
catch ( CryptographicException^ e )
{
Console::WriteLine( e );
return nullptr;
}
}
int main()
{
try
{
//Create a UnicodeEncoder to convert between byte array and string.
UnicodeEncoding^ ByteConverter = gcnew UnicodeEncoding;
//Create byte arrays to hold original, encrypted, and decrypted data.
array<Byte>^dataToEncrypt = ByteConverter->GetBytes( "Data to Encrypt" );
array<Byte>^encryptedData;
array<Byte>^decryptedData;
//Create a new instance of RSACryptoServiceProvider to generate
//public and private key data.
RSACryptoServiceProvider^ RSA = gcnew 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 ) );
delete RSA;
}
catch ( ArgumentNullException^ )
{
//Catch this exception in case the encryption did
//not succeed.
Console::WriteLine( "Encryption failed." );
}
}
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*.
RSACryptoServiceProvider^ RSA = gcnew 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.
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) |
使用指定密钥大小的随机密钥对来初始化 RSACryptoServiceProvider 类的新实例。 |
RSACryptoServiceProvider(Int32, CspParameters) |
使用指定的密钥大小和参数初始化 RSACryptoServiceProvider 类的新实例。 |
字段
KeySizeValue |
表示非对称算法所用密钥模块的大小(以位为单位)。 (继承自 AsymmetricAlgorithm) |
LegalKeySizesValue |
指定非对称算法支持的密钥大小。 (继承自 AsymmetricAlgorithm) |
属性
CspKeyContainerInfo |
获取描述有关加密密钥对的附加信息的 CspKeyContainerInfo 对象。 |
KeyExchangeAlgorithm |
获取可用于 RSA 的此实现的密钥交换算法的名称。 |
KeyExchangeAlgorithm |
获取可用于 RSA 的此实现的密钥交换算法的名称。 (继承自 RSA) |
KeySize |
获取当前密钥的大小。 |
LegalKeySizes |
获取非对称算法支持的密钥大小。 |
LegalKeySizes |
获取非对称算法支持的密钥大小。 (继承自 AsymmetricAlgorithm) |
PersistKeyInCsp |
获取或设置一个值,该值指示是否应在加密服务提供程序 (CSP) 中保留此密钥。 |
PublicOnly |
获取一个值,该值指示 RSACryptoServiceProvider 对象是否仅包含公钥。 |
SignatureAlgorithm |
获取可用于此 RSA 的实现的签名算法的名称。 |
SignatureAlgorithm |
获取可用于此 RSA 的实现的签名算法的名称。 (继承自 RSA) |
UseMachineKeyStore |
获取或设置一个值,该值指示是否应在计算机的密钥存储而不是用户配置文件存储中保留密钥。 |
方法
显式接口实现
IDisposable.Dispose() |
此 API 支持产品基础结构,不能在代码中直接使用。 有关此成员的说明,请参见 Dispose()。 (继承自 AsymmetricAlgorithm) |