15.02.2015 Views

C# 4 and .NET 4

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

558 ❘ ChaPTer 21 security<br />

Comparing the symmetric keys created from Alice <strong>and</strong> Bob shows that the same key value gets created.<br />

Using this symmetric key <strong>and</strong> the initialization vector, the message from Alice can be decrypted with the<br />

AesCryptoServiceProvider class.<br />

private static void BobReceivesData(byte[] encryptedData)<br />

{<br />

Console.WriteLine("Bob receives encrypted data");<br />

byte[] rawData = null;<br />

}<br />

var aes = new AesCryptoServiceProvider();<br />

int nBytes = aes.BlockSize 3;<br />

byte[] iv = new byte[nBytes];<br />

for (int i = 0; i < iv.Length; i++)<br />

iv[i] = encryptedData[i];<br />

using (var bobAlgorithm = new ECDiffieHellmanCng(bobKey))<br />

using (CngKey alicePubKey = CngKey.Import(alicePubKeyBlob,<br />

CngKeyBlobFormat.EccPublicBlob))<br />

{<br />

byte[] symmKey = bobAlgorithm.DeriveKeyMaterial(alicePubKey);<br />

Console.WriteLine("Bob creates this symmetric key with " +<br />

"Alices public key information: {0}",<br />

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

}<br />

aes.Key = symmKey;<br />

aes.IV = iv;<br />

using (ICryptoTransform decryptor = aes.CreateDecryptor())<br />

using (MemoryStream ms = new MemoryStream())<br />

{<br />

var cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Write);<br />

cs.Write(encryptedData, nBytes, encryptedData.Length - nBytes);<br />

cs.Close();<br />

rawData = ms.ToArray();<br />

Console.WriteLine("Bob decrypts message to: {0}",<br />

Encoding.UTF8.GetString(rawData));<br />

}<br />

aes.Clear();<br />

When you run the application, you can see output similar to the following on the console. The message from<br />

Alice is encrypted, <strong>and</strong> decrypted by Bob with the securely exchanged symmetric key.<br />

Alice sends message: secret message<br />

Alice creates this symmetric key with Bobs public key information:<br />

5NWat8AemzFCYo1IIae9S3Vn4AXyai4aL8ATFo41vbw=<br />

Alice: message is encrypted: 3C5U9CpYxnoFTk3Ew2V0T5Po0Jgryc5R7Te8ztau5N0=<br />

Bob receives encrypted message<br />

Bob creates this symmetric key with Alices public key information:<br />

5NWat8AemzFCYo1IIae9S3Vn4AXyai4aL8ATFo41vbw=<br />

Bob decrypts message to: secret message<br />

aCCess ConTrol To resourCes<br />

With the operating system, resources such as files <strong>and</strong> registry keys, as well as h<strong>and</strong>les of a named pipe, are<br />

secured by using an access control list. Figure 21-3 shows the structure of how this maps. The resource has a<br />

security descriptor associated. The security descriptor contains information about the owner of the resource<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!