RSACryptoServiceProvider.Encrypt 메서드

정의

알고리즘을 사용하여 데이터를 암호화합니다 RSA .

오버로드

Name Description
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입니다.

패딩 모드는 지원되지 않습니다.

설명

paddingRSAEncryptionPadding.Pkcs1 또는 RSAEncryptionPadding.OaepSHA1 중 하나여야 합니다.

적용 대상