15.02.2015 Views

C# 4 and .NET 4

Create successful ePaper yourself

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

e n c r y p t i o n ❘ 555<br />

First, take a look at the major steps in the Main() method: Alice ’ s keys are created, <strong>and</strong> the string “Alice”<br />

is signed <strong>and</strong> fi nally verifi ed if the signature is really from Alice by using the public key. The message<br />

that is signed is converted to a byte array by using the Encoding class. To write the encrypted signature<br />

to the console, the byte array that contains the signature is converted to a string with the method<br />

Convert.ToBase64String() .<br />

Never convert encrypted data to a string using the Encoding class. The Encoding<br />

class verifi es <strong>and</strong> converts invalid values that are not allowed with Unicode, <strong>and</strong> thus<br />

converting the string back to a byte array yields a different result.<br />

using System;<br />

using System.Security.Cryptography;<br />

using System.Text;<br />

namespace Wrox.ProCSharp.Security<br />

{<br />

class Program<br />

{<br />

internal static CngKey aliceKeySignature;<br />

internal static byte[] alicePubKeyBlob;<br />

static void Main()<br />

{<br />

CreateKeys();<br />

byte[] aliceData = Encoding.UTF8.GetBytes("Alice");<br />

byte[] aliceSignature = CreateSignature(aliceData, aliceKeySignature);<br />

Console.WriteLine("Alice created signature: {0}",<br />

Convert.ToBase64String(aliceSignature));<br />

}<br />

if (VerifySignature(aliceData, aliceSignature, alicePubKeyBlob))<br />

{<br />

Console.WriteLine("Alice signature verified successfully");<br />

}<br />

Code snippet SigningDemo/Program.cs<br />

CreateKeys() is the method that creates a new key pair for Alice. This key pair is stored in a static fi eld,<br />

so it can be accessed from the other methods. The Create() method of CngKey gets the algorithm as an<br />

argument to defi ne a key pair for the algorithm. With the Export() method, the public key of the key pair<br />

is exported. This public key can be given to Bob for the verifi cation of the signature. Alice keeps the private<br />

key. Instead of creating a key pair with the CngKey class, you can open existing keys that are stored in<br />

the key store. Usually Alice would have a certifi cate containing a key pair in her private store, <strong>and</strong> the store<br />

could be accessed with CngKey.Open() .<br />

static void CreateKeys()<br />

{<br />

aliceKeySignature = CngKey.Create(CngAlgorithm.ECDsaP256);<br />

alicePubKeyBlob = aliceKeySignature.Export(CngKeyBlobFormat.GenericPublicBlob);<br />

}<br />

With the key pair, Alice can create the signature using the ECDsaCng class. The constructor of this class<br />

receives the CngKey from Alice that contains both the public <strong>and</strong> private key. The private key is used,<br />

signing the data with the SignData() method.<br />

static byte[] CreateSignature(byte[] data, CngKey key)<br />

{<br />

var signingAlg = new ECDsaCng(key);<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!