Share via


RSACryptoServiceProvider.Encrypt Yöntem

Tanım

Verileri algoritmayla RSA şifreler.

Aşırı Yüklemeler

Encrypt(Byte[], Boolean)

Verileri algoritmayla RSA şifreler.

Encrypt(Byte[], RSAEncryptionPadding)

Belirtilen doldurmayı RSA kullanarak verileri algoritmayla şifreler.

Encrypt(Byte[], Boolean)

Kaynak:
RSACryptoServiceProvider.Unix.cs
Kaynak:
RSACryptoServiceProvider.Unix.cs
Kaynak:
RSACryptoServiceProvider.Unix.cs

Verileri algoritmayla RSA şifreler.

public:
 cli::array <System::Byte> ^ Encrypt(cli::array <System::Byte> ^ rgb, bool fOAEP);
public byte[] Encrypt (byte[] rgb, bool fOAEP);
override this.Encrypt : byte[] * bool -> byte[]
member this.Encrypt : byte[] * bool -> byte[]
Public Function Encrypt (rgb As Byte(), fOAEP As Boolean) As Byte()

Parametreler

rgb
Byte[]

Şifrelenecek veriler.

fOAEP
Boolean

true OAEP doldurma kullanarak doğrudan RSA şifreleme gerçekleştirmek için (yalnızca Windows XP veya üzerini çalıştıran bir bilgisayarda kullanılabilir); aksi takdirde, false PKCS#1 v1.5 doldurmasını kullanmak için.

Döndürülenler

Byte[]

Şifrelenmiş veriler.

Özel durumlar

Şifreleme hizmeti sağlayıcısı (CSP) alınamıyor.

-veya-

Parametrenin rgb uzunluğu izin verilen uzunluk üst sınırından büyük.

rgb, null değeridir.

Örnekler

Aşağıdaki kod örneği bir ortak anahtarın değerine bir RSACryptoServiceProvider nesne başlatır (başka bir taraf tarafından gönderilir), algoritmayı Aes kullanarak bir oturum anahtarı oluşturur ve ardından nesnesini kullanarak RSACryptoServiceProvider oturum anahtarını şifreler. Bu şema kullanılarak, oturum anahtarı özel RSA anahtarının sahibine geri gönderilebilir ve iki taraf şifrelenmiş verileri değiştirmek için oturum anahtarını kullanabilir.

#using <System.dll>

using namespace System;
using namespace System::Security::Cryptography;
int main()
{
   try
   {
      
      //initialze the Byte arrays to the public key information.
      array<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};
      array<Byte>^Exponent = {1,0,1};
      
      //Values to store encrypted symmetric keys.
      array<Byte>^EncryptedSymmetricKey;
      array<Byte>^EncryptedSymmetricIV;
      
      //Create a new instance of RSACryptoServiceProvider.
      RSACryptoServiceProvider^ RSA = gcnew RSACryptoServiceProvider;
      
      //Create a new instance of RSAParameters.
      RSAParameters RSAKeyInfo;
      
      //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 ( CryptographicException^ e ) 
   {
      
      //Catch and display a CryptographicException  
      //to the console.
      Console::WriteLine( e->Message );
   }

}
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

Açıklamalar

Aşağıdaki tabloda, Microsoft Windows'un farklı sürümleri tarafından desteklenen doldurma ve farklı işletim sistemleri ve doldurma bileşimleri tarafından izin verilen maksimum uzunluk rgb açıklanmaktadır.

İç Boşluk Rgb Parametresinin Maksimum Uzunluğu
OAEP doldurma (PKCS#1 v2) Modulus boyutu -2 -2*hLen; burada hLen karmanın boyutudur.
Doğrudan Şifreleme (PKCS#1 v1.5) Mod boyutu - 11. (11 bayt, mümkün olan en düşük doldurmadır.)

Bu yöntemin sonuçlarının şifresini çözmek için kullanın Decrypt .

Ayrıca bkz.

Şunlara uygulanır

Encrypt(Byte[], RSAEncryptionPadding)

Kaynak:
RSACryptoServiceProvider.Unix.cs
Kaynak:
RSACryptoServiceProvider.Unix.cs
Kaynak:
RSACryptoServiceProvider.Unix.cs

Belirtilen doldurmayı RSA kullanarak verileri algoritmayla şifreler.

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()

Parametreler

data
Byte[]

Şifrelenmesi gereken veriler.

padding
RSAEncryptionPadding

Doldurma.

Döndürülenler

Byte[]

Şifrelenmiş veriler.

Özel durumlar

data, null değeridir.

-veya-

padding, null değeridir.

Doldurma modu desteklenmiyor.

Açıklamalar

paddingveya RSAEncryptionPadding.OaepSHA1olmalıdırRSAEncryptionPadding.Pkcs1.

Şunlara uygulanır