通过


RSACryptoServiceProvider.Encrypt 方法

定义

使用 RSA 算法加密数据。

重载

名称 说明
Encrypt(Byte[], Boolean)
已过时.

使用 RSA 算法加密数据。

Encrypt(Byte[], RSAEncryptionPadding)

使用指定的填充通过 RSA 算法加密数据。

Encrypt(Byte[], Boolean)

Source:
RSACryptoServiceProvider.Unix.cs
Source:
RSACryptoServiceProvider.Unix.cs
Source:
RSACryptoServiceProvider.Unix.cs
Source:
RSACryptoServiceProvider.Unix.cs
Source:
RSACryptoServiceProvider.Unix.cs

注意

RSACryptoServiceProvider.Encrypt and RSACryptoServiceProvider.Decrypt methods that take a Boolean are obsolete. Use the overload that accepts RSAEncryptionPadding instead.

使用 RSA 算法加密数据。

public:
 cli::array <System::Byte> ^ Encrypt(cli::array <System::Byte> ^ rgb, bool fOAEP);
public byte[] Encrypt(byte[] rgb, bool fOAEP);
[System.Obsolete("RSACryptoServiceProvider.Encrypt and RSACryptoServiceProvider.Decrypt methods that take a Boolean are obsolete. Use the overload that accepts RSAEncryptionPadding instead.", DiagnosticId="SYSLIB0064", UrlFormat="https://aka.ms/dotnet-warnings/{0}")]
public byte[] Encrypt(byte[] rgb, bool fOAEP);
override this.Encrypt : byte[] * bool -> byte[]
[<System.Obsolete("RSACryptoServiceProvider.Encrypt and RSACryptoServiceProvider.Decrypt methods that take a Boolean are obsolete. Use the overload that accepts RSAEncryptionPadding instead.", DiagnosticId="SYSLIB0064", UrlFormat="https://aka.ms/dotnet-warnings/{0}")>]
override this.Encrypt : byte[] * bool -> byte[]
member this.Encrypt : byte[] * bool -> byte[]
Public Function Encrypt (rgb As Byte(), fOAEP As Boolean) As Byte()

参数

rgb
Byte[]

要加密的数据。

fOAEP
Boolean

true使用 OAEP 填充执行直接RSA加密(仅在运行 Windows XP 或更高版本的计算机上可用);否则,false使用 PKCS#1 v1.5 填充。

返回

Byte[]

加密的数据。

属性

例外

无法获取加密服务提供程序(CSP)。

-或-

参数的 rgb 长度大于允许的最大长度。

rgbnull

示例

下面的代码示例将对象 RSACryptoServiceProvider 初始化为公钥的值(由另一方发送),使用 Aes 算法生成会话密钥,然后使用该对象加密会话密钥 RSACryptoServiceProvider 。 使用此方案,会话密钥可以发回专用 RSA 密钥的所有者,双方可以使用会话密钥交换加密数据。

using System;
using System.Security.Cryptography;

class RSACSPSample
{

    static void Main()
    {
        try
        {		//initialze the byte arrays to the public key information.
            byte[] PublicKey = {214,46,220,83,160,73,40,39,201,155,19,202,3,11,191,178,56,
                                   74,90,36,248,103,18,144,170,163,145,87,54,61,34,220,222,
                                   207,137,149,173,14,92,120,206,222,158,28,40,24,30,16,175,
                                   108,128,35,230,118,40,121,113,125,216,130,11,24,90,48,194,
                                   240,105,44,76,34,57,249,228,125,80,38,9,136,29,117,207,139,
                                   168,181,85,137,126,10,126,242,120,247,121,8,100,12,201,171,
                                   38,226,193,180,190,117,177,87,143,242,213,11,44,180,113,93,
                                   106,99,179,68,175,211,164,116,64,148,226,254,172,147};

            byte[] Exponent = {1,0,1};
      
            //Values to store encrypted symmetric keys.
            byte[] EncryptedSymmetricKey;
            byte[] EncryptedSymmetricIV;

            //Create a new instance of RSACryptoServiceProvider.
            RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();

            //Create a new instance of RSAParameters.
            RSAParameters RSAKeyInfo = new RSAParameters();

            //Set RSAKeyInfo to the public key values. 
            RSAKeyInfo.Modulus = PublicKey;
            RSAKeyInfo.Exponent = Exponent;

            //Import key parameters into RSA.
            RSA.ImportParameters(RSAKeyInfo);

            //Create a new instance of the Aes class.
            Aes aes = Aes.Create();

            //Encrypt the symmetric key and IV.
            EncryptedSymmetricKey = RSA.Encrypt(aes.Key, false);
            EncryptedSymmetricIV = RSA.Encrypt(aes.IV, false);

            Console.WriteLine("Aes Key and IV have been encrypted with RSACryptoServiceProvider."); 
        }
        //Catch and display a CryptographicException  
        //to the console.
        catch(CryptographicException e)
        {
            Console.WriteLine(e.Message);
        }
    }
}
Imports System.Security.Cryptography

Class RSACSPSample

    Shared Sub Main()
        Try
            'initialze the byte arrays to the public key information.
            Dim PublicKey As Byte() = {214, 46, 220, 83, 160, 73, 40, 39, 201, 155, 19, 202, 3, 11, 191, 178, 56, 74, 90, 36, 248, 103, 18, 144, 170, 163, 145, 87, 54, 61, 34, 220, 222, 207, 137, 149, 173, 14, 92, 120, 206, 222, 158, 28, 40, 24, 30, 16, 175, 108, 128, 35, 230, 118, 40, 121, 113, 125, 216, 130, 11, 24, 90, 48, 194, 240, 105, 44, 76, 34, 57, 249, 228, 125, 80, 38, 9, 136, 29, 117, 207, 139, 168, 181, 85, 137, 126, 10, 126, 242, 120, 247, 121, 8, 100, 12, 201, 171, 38, 226, 193, 180, 190, 117, 177, 87, 143, 242, 213, 11, 44, 180, 113, 93, 106, 99, 179, 68, 175, 211, 164, 116, 64, 148, 226, 254, 172, 147}

            Dim Exponent As Byte() = {1, 0, 1}

            'Values to store encrypted symmetric keys.
            Dim EncryptedSymmetricKey() As Byte
            Dim EncryptedSymmetricIV() As Byte

            'Create a new instance of RSACryptoServiceProvider.
            Dim RSA As New RSACryptoServiceProvider()

            'Create a new instance of RSAParameters.
            Dim RSAKeyInfo As New RSAParameters()

            'Set RSAKeyInfo to the public key values. 
            RSAKeyInfo.Modulus = PublicKey
            RSAKeyInfo.Exponent = Exponent

            'Import key parameters into RSA.
            RSA.ImportParameters(RSAKeyInfo)

            'Create a new instance of the Aes class.
            Dim aes As Aes = Aes.Create()

            'Encrypt the symmetric key and IV.
            EncryptedSymmetricKey = RSA.Encrypt(aes.Key, False)
            EncryptedSymmetricIV = RSA.Encrypt(aes.IV, False)

            Console.WriteLine("Aes Key and IV have been encrypted with RSA.")

            'Catch and display a CryptographicException  
            'to the console.
        Catch e As CryptographicException
            Console.WriteLine(e.Message)
        End Try
    End Sub
End Class

注解

下表描述了不同版本的Microsoft Windows支持的填充,以及操作系统和填充的不同组合允许的rgb的最大长度。

填充 rgb 参数的最大长度
OAEP 填充 (PKCS#1 v2) 模量大小 -2 -2*hLen,其中 hLen 是哈希的大小。
直接加密 (PKCS#1 v1.5) 模量大小 - 11。 (11 字节是可能的最小填充。

用于 Decrypt 解密此方法的结果。

另请参阅

适用于

Encrypt(Byte[], RSAEncryptionPadding)

Source:
RSACryptoServiceProvider.Unix.cs
Source:
RSACryptoServiceProvider.Unix.cs
Source:
RSACryptoServiceProvider.Unix.cs
Source:
RSACryptoServiceProvider.Unix.cs
Source:
RSACryptoServiceProvider.Unix.cs

使用指定的填充通过 RSA 算法加密数据。

public:
 override cli::array <System::Byte> ^ Encrypt(cli::array <System::Byte> ^ data, System::Security::Cryptography::RSAEncryptionPadding ^ padding);
public override byte[] Encrypt(byte[] data, System.Security.Cryptography.RSAEncryptionPadding padding);
override this.Encrypt : byte[] * System.Security.Cryptography.RSAEncryptionPadding -> byte[]
Public Overrides Function Encrypt (data As Byte(), padding As RSAEncryptionPadding) As Byte()

参数

data
Byte[]

要加密的数据。

padding
RSAEncryptionPadding

填充。

返回

Byte[]

加密的数据。

例外

datanull

-或-

paddingnull

不支持填充模式。

注解

padding 须为 RSAEncryptionPadding.Pkcs1RSAEncryptionPadding.OaepSHA1

适用于