RSA.TryEncrypt Method

Definition

Attempts to encrypt the input data with a specified padding mode into a provided buffer.

C#
public virtual bool TryEncrypt(ReadOnlySpan<byte> data, Span<byte> destination, System.Security.Cryptography.RSAEncryptionPadding padding, out int bytesWritten);

Parameters

data
ReadOnlySpan<Byte>

The data to encrypt.

destination
Span<Byte>

The buffer to receive the encrypted data.

padding
RSAEncryptionPadding

The padding mode.

bytesWritten
Int32

When this method returns, the total number of bytes written into destination. This parameter is treated as uninitialized.

Returns

true if destination is long enough to receive the encrypted data; otherwise, false.

Exceptions

padding is null.

padding is unknown, or not supported by this implementation.

-or-

The length of data is too long for the combination of KeySize and the selected padding.

-or-

The encryption operation failed.

Remarks

The default implementation of this method calls Encrypt(Byte[], RSAEncryptionPadding) and copies the result to destination. Derived types should override this method to avoid the intermediate array creation.

The RSA encryption algorithm will always produce an output equal to the number of bytes required for KeySize bits.

Applies to

Product Versions
.NET Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Standard 2.1

See also