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

使用 RSA 算法加密数据。

C#
public byte[] Encrypt(byte[] rgb, bool fOAEP);

参数

rgb
Byte[]

要加密的数据。

fOAEP
Boolean

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

返回

Byte[]

已加密的数据。

例外

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

- 或 -

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

rgbnull

示例

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

C#
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);
        }
    }
}

注解

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

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

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

另请参阅

适用于

.NET 10 和其他版本
产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

Encrypt(Byte[], RSAEncryptionPadding)

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

使用指定的填充,借助 RSA 算法对数据加密。

C#
public override byte[] Encrypt(byte[] data, System.Security.Cryptography.RSAEncryptionPadding padding);

参数

data
Byte[]

要加密的数据。

padding
RSAEncryptionPadding

填充。

返回

Byte[]

已加密的数据。

例外

datanull

paddingnull

不支持该填充模式。

注解

padding 必须为 RSAEncryptionPadding.Pkcs1RSAEncryptionPadding.OaepSHA1

适用于

.NET 10 和其他版本
产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1