Click or drag to resize

RSACryptoServiceProvider Class

Performs asymmetric encryption and decryption using the implementation of the Crestron.SimplSharp.Cryptography.RSA algorithm provided by the cryptographic service provider (CSP). This class cannot be inherited.
Inheritance Hierarchy
SystemObject
  Crestron.SimplSharp.CryptographyAsymmetricAlgorithm
    Crestron.SimplSharp.CryptographyRSA
      Crestron.SimplSharp.CryptographyRSACryptoServiceProvider

Namespace: Crestron.SimplSharp.Cryptography
Assembly: SimplSharpCryptographyInterface (in SimplSharpCryptographyInterface.dll) Version: 2.21.121
Syntax
C#
public sealed class RSACryptoServiceProvider : RSA

The RSACryptoServiceProvider type exposes the following members.

Constructors
 NameDescription
Public methodRSACryptoServiceProvider Initializes a new instance of the Crestron.SimplSharp.Cryptography.RSACryptoServiceProvider class using the default key.
Public methodRSACryptoServiceProvider(CspParameters) Initializes a new instance of the Crestron.SimplSharp.Cryptography.RSACryptoServiceProvider class with the specified parameters.
Public methodRSACryptoServiceProvider(Int32) Initializes a new instance of the Crestron.SimplSharp.Cryptography.RSACryptoServiceProvider class with the specified key size.
Public methodRSACryptoServiceProvider(Int32, CspParameters) Initializes a new instance of the Crestron.SimplSharp.Cryptography.RSACryptoServiceProvider class with the specified key size and parameters.
Top
Properties
 NameDescription
Public propertyCspKeyContainerInfo Gets a Crestron.SimplSharp.Cryptography.CspKeyContainerInfo object that describes additional information about a cryptographic key pair.
Public propertyKeyExchangeAlgorithm Gets the name of the key exchange algorithm available with this implementation of Crestron.SimplSharp.Cryptography.RSA.
(Overrides AsymmetricAlgorithmKeyExchangeAlgorithm)
Public propertyKeySize Gets the size of the current key.
(Overrides AsymmetricAlgorithmKeySize)
Public propertyLegalKeySizes Gets the key sizes that are supported by the asymmetric algorithm.
(Inherited from AsymmetricAlgorithm)
Public propertyPersistKeyInCsp Gets or sets a value indicating whether the key should be persisted in the cryptographic service provider (CSP).
Public propertyPublicOnly Gets a value that indicates whether the Crestron.SimplSharp.Cryptography.RSACryptoServiceProvider object contains only a public key.
Public propertySignatureAlgorithm Gets the name of the signature algorithm available with this implementation of Crestron.SimplSharp.Cryptography.RSA.
(Overrides AsymmetricAlgorithmSignatureAlgorithm)
Public propertyStatic memberUseMachineKeyStore Gets or sets a value indicating whether the key should be persisted in the computer's key store instead of the user profile store.
Top
Methods
 NameDescription
Public methodClear Releases all resources used by the Crestron.SimplSharp.Cryptography.AsymmetricAlgorithm class.
(Inherited from AsymmetricAlgorithm)
Public methodDecrypt Decrypts data with the Crestron.SimplSharp.Cryptography.RSA algorithm.
Public methodDecryptValue This method is not supported in the current version.
(Overrides RSADecryptValue(Byte))
Public methodDispose Dispose
(Inherited from AsymmetricAlgorithm)
Protected methodDispose(Boolean) Required member to implement AsymmetricAlgorithm class.
(Overrides AsymmetricAlgorithmDispose(Boolean))
Public methodEncrypt Encrypts data with the Crestron.SimplSharp.Cryptography.RSA algorithm.
Public methodEncryptValue This method is not supported in the current version.
(Overrides RSAEncryptValue(Byte))
Public methodEqualsDetermines whether the specified object is equal to the current object.
(Inherited from Object)
Public methodExportCspBlob Exports a blob containing the key information associated with an Crestron.SimplSharp.Cryptography.RSACryptoServiceProvider object.
Public methodExportParameters Exports the Crestron.SimplSharp.Cryptography.RSAParameters.
(Overrides RSAExportParameters(Boolean))
Public methodGetHashCodeServes as the default hash function.
(Inherited from Object)
Public methodGetTypeGets the Type of the current instance.
(Inherited from Object)
Public methodImportCspBlob Imports a blob that represents RSA key information.
Public methodImportParameters Imports the specified Crestron.SimplSharp.Cryptography.RSAParameters.
(Overrides RSAImportParameters(RSAParameters))
Public methodSignData(Byte, Object) Computes the hash value of the specified byte array using the specified hash algorithm, and signs the resulting hash value.
Public methodSignData(Stream, Object) Computes the hash value of the specified input stream using the specified hash algorithm, and signs the resulting hash value.
Public methodSignData(Byte, Int32, Int32, Object) Computes the hash value of a subset of the specified byte array using the specified hash algorithm, and signs the resulting hash value.
Public methodSignHash Computes the signature for the specified hash value by encrypting it with the private key.
Public methodToStringReturns a string that represents the current object.
(Inherited from Object)
Public methodVerifyData Verifies the specified signature data by comparing it to the signature computed for the specified data.
Public methodVerifyHash Verifies the specified signature data by comparing it to the signature computed for the specified hash value.
Top
Fields
 NameDescription
Protected fieldKeySizeValue Represents the size, in bits, of the key modulus used by the asymmetric algorithm.
(Inherited from AsymmetricAlgorithm)
Protected fieldLegalKeySizesValue Specifies the key sizes that are supported by the asymmetric algorithm.
(Inherited from AsymmetricAlgorithm)
Top
Example
This example class stores the asymmetric key pair generated by RSACryptoServiceProvider into a persistent key store by passing a CspParameters object to the constructor. This key pair will be used on subsequent calls to RSAEncrypt and RSADecrypt, even after a reboot.
C#
public class EncryptionLayer
{    
    // keyStoreName identifies the persistent key container used 
    // to store the RSA key pair
    string keyStoreName;

    public EncryptionLayer(string keyStoreName) 
    { 
        this.keyStoreName = keyStoreName;
    }

    public byte[] RSAEncrypt(string plaintext)
    {
        // retrieve RSA key pair from key container
        var csp = new CspParameters();
        csp.KeyContainerName = keyStoreName;

        var rsa = new RSACryptoServiceProvider(csp);

        // encrypt the inputted string
        byte[] cipherbytes = rsa.Encrypt(Encoding.Default.GetBytes(plaintext), false);

        // return the encrypted bytes
        return cipherbytes;
    }

    public string RSADecrypt(byte[] ciphertext)
    {
        // retrieve RSA key pair from key container
        var csp = new CspParameters();
        csp.KeyContainerName = keyStoreName;

        var rsa = new RSACryptoServiceProvider(csp); 

        // decrypt the inputted byte array
        byte[] plainbytes = null;
        plainbytes = rsa.Decrypt(ciphertext, false);

        // convert the plaintext bytes back to a string and return it
        return Encoding.Default.GetString(plainbytes, 0, plainbytes.Length);
    }
}

Version Information

.NET

Supported in: 6.0

.NET Compact Framework

Supported in: 3.5
See Also