|Important||This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here. ArchiveDisclaimer|
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 http://www.microsoft.com/WINDOWS2000/downloads/recommended/encryption/.
For information about using the samples, see the following topics:
To build the sample using the command prompt
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.
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
Open Windows Explorer and navigate to one of the language-specific subdirectories under the PublicKeyCryptography directory.
Open the Visual Studio solution file PublicKeyCS.sln or PublicKeyVB.sln, depending on your choice of programming language.
On the Build menu, click Build Solution.
To run the sample
Navigate to the directory that contains the built executable file, using the command prompt.
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:
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.
provides a symmetric encryption algorithm to encrypt message text.
provides the interface that defines the basic operations of cryptographic transformations.
provides a stream to encrypt data that is fed into a .
for converting strings to byte arrays and vice versa.
For more information about key-pair encryption, see the comments in the source code files.