Main Site Documentation

Snippet - Example on how to use RSACryptoServiceProvider


#1

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.


#2

Nice one - thanks.


#3

Thanks!


#4

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


#5

You are welcome! :slight_smile:


#6

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


#7

Encryptor inside the provider takes care of that.


#8

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.


#9

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


#10

Hello.

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


#11

@ muetze - Welcome to the forum.

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


#12

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?


#13

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


#14

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


#15

@ 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 4.2.0.0, 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.