This documentation is archived and is not being maintained.

Public Key Cryptography Technology Sample

This sample demonstrates how you can use public key encryption to exchange messages. This sample requires the Microsoft Windows 2000 High Encryption Pack. For more information and to download the high encryption pack, see

For information about using the samples, see the following topics:

To build the sample using the command prompt

  1. Navigate to one of the language-specific subdirectories under the PublicKeyCryptography directory, using the command prompt. For information about required settings and the SDK Command Prompt, see How to: Set Sample Settings.

  2. Type msbuild PublicKeyCryptographyCS.sln or msbuild PublicKeyCryptographyVB.sln, depending on your choice of programming language. The executable will be built in the default \bin or \bin\Debug directory.

To build the sample using Visual Studio

  1. Open Windows Explorer and navigate to one of the language-specific subdirectories under the PublicKeyCryptography directory.

  2. Open the Visual Studio solution file PublicKeyCS.sln or PublicKeyVB.sln, depending on your choice of programming language.

  3. On the Build menu, click Build Solution.

To run the sample

  1. Navigate to the directory that contains the built executable file, using the command prompt.

  2. Type PublicKey.exe.


This sample builds a console application. You must run it using the command prompt to view its output.

The messages are encrypted using a symmetric algorithm (RC2) and then the RC2 key is encrypted using an asymmetric algorithm (RSA). Asymmetric cryptographic algorithms, also known as public-key algorithms, require that each entity maintains a pair of related keys: a private key and a public key. Both keys are unique to the entity. The public key can be made available to anyone; this key is used for encoding data to be sent to a receiver. The private key must be kept private by the receiver; this key is used for decoding messages encoded using the receiver's public key.

The following classes are used by this sample:

  • RSACryptoServiceProvider provides a managed implementation of RSA, an asymmetric (public key) algorithm. RSA is used to encrypt the RC2 key to be sent with the encrypted message.

  • RC2CryptoServiceProvider provides a symmetric encryption algorithm to encrypt message text.

  • ICryptoTransformprovides theinterface that defines the basic operations of cryptographic transformations.

  • CryptoStreamprovides astream to encrypt data that is fed into a MemoryStream.

  • Encodingfor converting strings to byte arrays and vice versa.

For more information about key-pair encryption, see the comments in the source code files.