Snippet - Example on how to use RSACryptoServiceProvider

Example on how to use RSACryptoServiceProvider

This example shows how to use RSACryptoServiceProvider. It was tested in Microsoft Emulator. Public and Private keys are embedded in the code. Separate Session is used for Encryption and Decryption. This code is for demonstration purposes only.


Nice one - thanks.


That is a handy reference to have around. Thank you!

You are welcome! :slight_smile:

How is the data that is going to be encrypted padded to fill out 128 bytes?

Encryptor inside the provider takes care of that.

Okey, I have data that is already padded (signed) and if I call encrypt on my data buffer which is 128 byte I get error 6.

Okay. Looks like RSACryptoServiceProvider is failing to encrypt data that has 118 bytes and higher it throws that exception with error code 6 as you have mentioned in the other thread. It can be a bug.

To resolve that try this:

Don’t use RSACryptoServiceProvider. Create Encryptor/Decryptor instead and encrypt/decrypt in chunks.

Encryptor encryptor = new Encryptor(session, new Mechanism(MechanismType.RSA_PKCS), publicKey, publicKey.Size, publicKey.Size);

after that use TransformBlock as many times as needed and on the last chunk use TransformFinalBlock


I wanted to use the RSACryptoServiceProvider in a gadgeteer application. But the namespace does not exist. May I forgot something?

I´m using a FEZ Spider starterkit. My application should open a file and it´s signature on a usb device and verify that file. The file contains licence information and i want to get sure if the information was not changed (it is a textfile). The usb device should be generated on a desktop machine using a simple winform application.

Is there another way to sign a file on a desktop machine and verify it on the FEZ Spider?

thx for help

@ muetze - Welcome to the forum.

What namespace are you missing? Did you add references to the required assemblies?

Thank you for your answers.

I´m using the

using Microsoft.SPOT.Cryptoki;
using System.Security.Cryptography;

namespaces, but I can´t find the RSACryptoServiceProvider.
In the System.Security.Cryptography namespace there are only 6 entries (see screenshot).

What´s going wrong?

yes I´m using framework 4.2. in a gadgeteer project.

you are right.
I forgot to add a reference to the System.Security dll

i wonder that i see parts of the namecpace even without the reference.

whatever, thank you for that input.
nice day

@ muetze - Add a reference to ‘System.Security’.

Right click on ‘References’ in the Solution Explorer and select ‘Add Reference…’ from the popup menu. On the .NET tab you should find ‘System.Security’ version, select that and click ‘OK’.

After that you should have the ‘RSACryptoServiceProvider’ as a member of the ‘System.Security’ namespace.

Edit: You got it while I was typing the above…

Namespaces can be split across multiple assemblies. It is common to have portions of a namespace that are commonly used in a common assembly and then have the rest of the namespace types in another assembly that is only needed if you are using that functionality.