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.

encryption ❘ 553<br />

encrypted using a public key can be decrypted with the corresponding private key. This also works the other<br />

way around: something encrypted using a private key can be decrypted by using the corresponding public<br />

key, but not the private key.<br />

Public <strong>and</strong> private keys are always created as a pair. The public key can be made available to everybody,<br />

<strong>and</strong> it can even be put on a web site, but the private key must be safely locked away. Following are some<br />

examples where these public <strong>and</strong> private keys are used to explain encryption.<br />

If Alice sends a message to Bob (see Figure 21-2), <strong>and</strong> Alice wants to make sure that no one else but Bob can<br />

read the message, she uses Bob’s public key. The message is encrypted using Bob’s public key. Bob opens the<br />

message <strong>and</strong> can decrypt it using his secretly stored private key. This key exchange guarantees that no one<br />

but Bob can read Alice’s message.<br />

Alice<br />

Bob<br />

figure 21-2<br />

Eve<br />

There is one problem left: Bob can’t be sure that the mail comes from Alice. Eve can use Bob’s public key<br />

to encrypt messages sent to Bob <strong>and</strong> pretend to be Alice. We can extend this principle using public/private<br />

keys. Let’s start again with Alice sending a message to Bob. Before Alice encrypts the message using Bob’s<br />

public key, she adds her signature <strong>and</strong> encrypts the signature using her own private key. Then she encrypts<br />

the mail using Bob’s public key. Therefore, it is guaranteed that no one else but Bob can read the mail.<br />

When Bob decrypts the message, he detects an encrypted signature. The signature can be decrypted using<br />

Alice’s public key. For Bob, it is not a problem to access Alice’s public key because the key is public. After<br />

decrypting the signature, Bob can be sure that it was Alice who sent the message.<br />

The encryption <strong>and</strong> decryption algorithms using symmetric keys are a lot faster than those using<br />

asymmetric keys. The problem with symmetric keys is that the keys must be exchanged in a safe manner.<br />

With network communication, one way to do this is by using asymmetric keys first for the key exchange <strong>and</strong><br />

then symmetric keys for encryption of the data that is sent across the wire.<br />

With the .<strong>NET</strong> Framework, you find classes for encryption in the namespace System.Security<br />

.Cryptography. Several symmetric <strong>and</strong> asymmetric algorithms are implemented. You can find algorithm<br />

classes for many different purposes. Some of the classes added in .<strong>NET</strong> 3.5 have a Cng prefix or suffix.<br />

Cng is short for Cryptography Next Generation, which can be used since Windows Vista <strong>and</strong> Windows<br />

Server 2008. This API makes it possible to write a program independently of the algorithm by using a<br />

provider-based model. If you are targeting Windows Server 2003 as well, you need to pay attention to what<br />

encryption classes to use.<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!