RSACryptoServiceProvider.Decrypt 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
对以前加密的数据进行解密。
重载
Decrypt(Byte[], Boolean) |
使用 RSA 算法加密数据。 |
Decrypt(Byte[], RSAEncryptionPadding) |
使用指定填充对以前通过 RSA 算法加密的数据进行解密。 |
Decrypt(Byte[], Boolean)
使用 RSA 算法加密数据。
public:
cli::array <System::Byte> ^ Decrypt(cli::array <System::Byte> ^ rgb, bool fOAEP);
public byte[] Decrypt (byte[] rgb, bool fOAEP);
override this.Decrypt : byte[] * bool -> byte[]
member this.Decrypt : byte[] * bool -> byte[]
Public Function Decrypt (rgb As Byte(), fOAEP As Boolean) As Byte()
参数
- rgb
- Byte[]
要解密的数据。
返回
Byte[]
已解密的数据,它是加密前的原始纯文本。
例外
无法获取加密服务提供程序 (CSP)。
- 或 -
参数 fOAEP
的值为 true
,并且 rgb
参数的长度大于 KeySize。
- 或 -
此密钥与解密数据不匹配。 但是,异常用词可能不准确。 例如,它可能会说 Not enough storage is available to process this command
。
rgb
为 null
。
示例
下面的代码示例对数据进行加密和解密。
此示例使用 ASCIIEncoding 类;但是, UnicodeEncoding 类在大型数据操作中可能更可取。 加密的值可以保存为 nvarchar
Microsoft SQL Server 中的数据类型。
using namespace System;
using namespace System::Security::Cryptography;
using namespace System::Text;
int main()
{
try
{
//Create a UnicodeEncoder to convert between byte array and string.
ASCIIEncoding^ ByteConverter = gcnew ASCIIEncoding;
String^ dataString = "Data to Encrypt";
//Create byte arrays to hold original, encrypted, and decrypted data.
array<Byte>^dataToEncrypt = ByteConverter->GetBytes( dataString );
array<Byte>^encryptedData;
array<Byte>^decryptedData;
//Create a new instance of the RSACryptoServiceProvider class
// and automatically create a new key-pair.
RSACryptoServiceProvider^ RSAalg = gcnew RSACryptoServiceProvider;
//Display the origianl data to the console.
Console::WriteLine( "Original Data: {0}", dataString );
//Encrypt the byte array and specify no OAEP padding.
//OAEP padding is only available on Microsoft Windows XP or
//later.
encryptedData = RSAalg->Encrypt( dataToEncrypt, false );
//Display the encrypted data to the console.
Console::WriteLine( "Encrypted Data: {0}", ByteConverter->GetString( encryptedData ) );
//Pass the data to ENCRYPT and boolean flag specifying
//no OAEP padding.
decryptedData = RSAalg->Decrypt( encryptedData, false );
//Display the decrypted plaintext to the console.
Console::WriteLine( "Decrypted plaintext: {0}", ByteConverter->GetString( decryptedData ) );
}
catch ( CryptographicException^ e )
{
//Catch this exception in case the encryption did
//not succeed.
Console::WriteLine( e->Message );
}
}
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.
ASCIIEncoding ByteConverter = new ASCIIEncoding();
string dataString = "Data to Encrypt";
//Create byte arrays to hold original, encrypted, and decrypted data.
byte[] dataToEncrypt = ByteConverter.GetBytes(dataString);
byte[] encryptedData;
byte[] decryptedData;
//Create a new instance of the RSACryptoServiceProvider class
// and automatically create a new key-pair.
RSACryptoServiceProvider RSAalg = new RSACryptoServiceProvider();
//Display the origianl data to the console.
Console.WriteLine("Original Data: {0}", dataString);
//Encrypt the byte array and specify no OAEP padding.
//OAEP padding is only available on Microsoft Windows XP or
//later.
encryptedData = RSAalg.Encrypt(dataToEncrypt, false);
//Display the encrypted data to the console.
Console.WriteLine("Encrypted Data: {0}", ByteConverter.GetString(encryptedData));
//Pass the data to ENCRYPT and boolean flag specifying
//no OAEP padding.
decryptedData = RSAalg.Decrypt(encryptedData, false);
//Display the decrypted plaintext to the console.
Console.WriteLine("Decrypted plaintext: {0}", ByteConverter.GetString(decryptedData));
}
catch(CryptographicException e)
{
//Catch this exception in case the encryption did
//not succeed.
Console.WriteLine(e.Message);
}
}
}
Imports System.Security.Cryptography
Imports System.Text
Module RSACSPExample
Sub Main()
Try
'Create a UnicodeEncoder to convert between byte array and string.
Dim ByteConverter As New ASCIIEncoding
Dim dataString As String = "Data to Encrypt"
'Create byte arrays to hold original, encrypted, and decrypted data.
Dim dataToEncrypt As Byte() = ByteConverter.GetBytes(dataString)
Dim encryptedData() As Byte
Dim decryptedData() As Byte
'Create a new instance of the RSACryptoServiceProvider class
' and automatically create a new key-pair.
Dim RSAalg As New RSACryptoServiceProvider
'Display the origianl data to the console.
Console.WriteLine("Original Data: {0}", dataString)
'Encrypt the byte array and specify no OAEP padding.
'OAEP padding is only available on Microsoft Windows XP or
'later.
encryptedData = RSAalg.Encrypt(dataToEncrypt, False)
'Display the encrypted data to the console.
Console.WriteLine("Encrypted Data: {0}", ByteConverter.GetString(encryptedData))
'Pass the data to ENCRYPT and boolean flag specifying
'no OAEP padding.
decryptedData = RSAalg.Decrypt(encryptedData, False)
'Display the decrypted plaintext to the console.
Console.WriteLine("Decrypted plaintext: {0}", ByteConverter.GetString(decryptedData))
Catch e As CryptographicException
'Catch this exception in case the encryption did
'not succeed.
Console.WriteLine(e.Message)
End Try
End Sub
End Module
注解
使用 Encrypt 来加密数据以使用此方法进行解密。
另请参阅
适用于
Decrypt(Byte[], RSAEncryptionPadding)
使用指定填充对以前通过 RSA 算法加密的数据进行解密。
public:
override cli::array <System::Byte> ^ Decrypt(cli::array <System::Byte> ^ data, System::Security::Cryptography::RSAEncryptionPadding ^ padding);
public override byte[] Decrypt (byte[] data, System.Security.Cryptography.RSAEncryptionPadding padding);
override this.Decrypt : byte[] * System.Security.Cryptography.RSAEncryptionPadding -> byte[]
Public Overrides Function Decrypt (data As Byte(), padding As RSAEncryptionPadding) As Byte()
参数
- data
- Byte[]
要解密的数据。
- padding
- RSAEncryptionPadding
填充。
返回
Byte[]
已解密的数据。
例外
不支持该填充模式。
注解
padding
必须为 RSAEncryptionPadding.Pkcs1 或 RSAEncryptionPadding.OaepSHA1。