03.01.2015 Views

C# 5.0 Programmer's Reference

Visual Studio 2013 C# 5.0 Programmer's Reference

Visual Studio 2013 C# 5.0 Programmer's Reference

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Asymmetric Key Encryption ❘ 615<br />

numbers are also enormous (64 bytes or longer) so finding numbers that work isn’t as simple as generating<br />

random bytes.<br />

You can create a new set of key parameters by making an instance of the RSACryptoServiceProvider<br />

class. You can then publish the public parameters and save the private parameters so that you can later<br />

decrypt messages.<br />

Microsoft correctly points out that you should never store key values in plaintext on a computer, so<br />

cyber-crooks can’t find them. You could write down the D, DP, DQ, InverseQ, P, and Q parameters<br />

on a piece of paper, but that’s a lot of data.<br />

To help solve this problem, Microsoft suggests that you store key information in a key container, an<br />

object that stores key information encrypted, so it’s not easy to steal.<br />

If this all seems complicated, you’re right, it is.<br />

The following list summarizes the steps for creating RSA keys in a <strong>C#</strong> program.<br />

1. Create a new RSACryptoServiceProvider object. (It is created with a usable set of key values.)<br />

2. Use the object’s ExportParameters method to extract the key values.<br />

3. Save the key values in a key container for later use.<br />

4. Publish the public key values Exponent and Modulus.<br />

The following list summarizes the steps for using RSA to encrypt data in a <strong>C#</strong> program.<br />

1. Create a new RSAParameters structure.<br />

2. Initialize the structure’s Exponent and Modulus parameters with the public values.<br />

3. Create an RSACryptoServiceProvider object.<br />

4. Call the object’s ImportParameters method to set its public key parameters.<br />

5. Call the object’s Encrypt method, passing it the bytes to encrypt.<br />

The following list summarizes the steps for using RSA to decrypt data in a <strong>C#</strong> program.<br />

1. Create an RSACryptoServiceProvider object, initializing its parameters with the<br />

values saved in the key container. (Alternatively, you can create the object and then use<br />

ImportParameters to load the key parameters from an RSAParameters structure.)<br />

2. Call the provider’s Decrypt method, passing it the encrypted bytes.<br />

The following sections show code that performs these steps.<br />

Creating, Saving, and Retrieving Keys<br />

The following code shows how a program might create new RSA key data and save it into a<br />

key container.<br />

// Create the parameters object and set the container name.<br />

CspParameters parameters = new CspParameters();<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!