How to: Build and Run the CNG Example

This topic describes the file architecture for the Cryptography Next Generation (CNG) secure communication example, and explains how to build and run the example.

To build the CNG example, you create three console executables. You run these executables simultaneously to see the composite, interactive example.

The CNG example was written in C#. It requires Visual Studio 2008 or later versions.

The CNG example consists of three Visual Studio projects: 

  • Alice

  • Bob

  • Mallory

The example contains six files, which you can view and copy from the following topics:

Each project has its own .cs file (Alice.cs, Bob.cs, and Mallory.cs) that contains project-specific code. The three projects share the Utilities.cs, ChannelManager.cs, and Communicator.cs files. These three files are added directly to the Alice project and indirectly (through file linking) to the Bob and Mallory projects.

To build the Alice, Bob, and Mallory projects

  1. Start Visual Studio.

  2. On the File menu, point to New, and then click Project.

    The New Project dialog box appears.

  3. In the Project types pane, click Visual C#.

  4. In the Templates pane, click Console Application.

  5. In the Name box, type Alice.

  6. In the Location box, enter the location where you want the Alice project stored.

  7. Click OK.

  8. Copy the Alice source code into a file named Alice.cs, and save it to the Alice project directory. When prompted, confirm that you want to replace the default Alice.cs file.

  9. Copy the ChannelManager, Communicator, and Utilities source code into separate files named ChannelManager.cs, Communicator.cs, and Utilities.cs, and save them to the Alice project directory. Add these three files to your Alice project as follows:

    1. On the View menu, click Solution Explorer.

    2. In the Solution Explorer window, right-click the Alice folder, point to Add, and then click Existing Item.

      The Add Existing Item dialog box appears and displays the Alice project directory.

    3. Select the ChannelManager.cs, Communicator.cs, and Utilities.cs files, and then click Add.

  10. Repeat steps 2-8 for the Bob and Mallory projects. Make sure that you save the Bob source code into the Bob project directory, and the Mallory source code into the Mallory project directory.

  11. Repeat step 9 for the Bob and Mallory projects. However, in step 9.c, instead of clicking Add, click the arrow next to the Add button, and click Add as Link.

  12. Set output directories for the Bob and Mallory projects as follows:

    1. On the Project menu, click Properties, and then click the Build tab.

    2. Set the Output path to the build location that the Alice project uses. This location is probably \bin\Debug\ in the Alice project directory you selected in step 6.

  13. To build each project, on the Build menu, click Build Solution.

  14. Confirm that all projects build without errors, and that the executable files Alice.exe, Bob.exe, and Mallory.exe exist in the Alice project directory.

The example can be run from Visual Studio or as a standalone application. These methods produce identical output.

To run the CNG example

  1. To run the example from Visual Studio, double-click the .sln files for the three projects, Alice.sln, Bob.sln, and Mallory.sln, to open three instances of Visual Studio. In the Alice project, on the Debug menu, click Start Debugging. Repeat for the Bob and Mallory projects. 


    To run the example as a stand-alone application, copy the executable (.exe) files for all three projects into a single directory. Double-click Alice.exe in Windows Explorer. Alice will call the Bob and Mallory executables through her built-in autoloader.


    You can also run the Alice executable from a console window by navigating to the directory that contains the three executables and entering alice.exe on the command line.

    When you run the three executables, three console windows appear. The window titles reflect the project names, and the windows are sized and positioned so that you can see the message flow easily on a 1280 x 1024 monitor. You can move and resize the console windows for optimal display.

  2. In the Alice window, when prompted for a security version, type a number between 1 and 5. For the first test, type 1.

    These numbers correspond to the five software versions discussed in the CNG example overview.

  3. Versions 2 through 5 will prompt you for verbose output. Enter n for regular mode or y for verbose mode.

    • Regular mode displays only plaintext messages.

    • Verbose mode displays plaintext messages, digital signature keys, cryptographic keys, initialization vectors, ciphertext, and digital signatures.

  4. When prompted whether you want to disable Mallory, type n for no or y for yes.

    If Mallory is disabled, he will not receive any messages and will remain idle. Alice and Bob will communicate without interception from Mallory.

  5. Follow the scripted conversation between Alice and Bob.

    The details of the conversation depend on the version number you selected in step 2. Each window creates a message loop that alternates between send and receive mode. Send mode is indicated by the :> prompt. Receive mode is indicated by a blank line. If Alice is in send mode, Bob will be in receive mode. When Bob receives a message, he will switch to send mode, and Alice will switch to receive mode.

    Look carefully at Alice and Bob's messages. Alice's second message to Bob and Bob's reply to Alice have been changed by Mallory.

    After the scripted conversation ends, Alice will ask you whether you want to talk to Bob.

  6. If you want to send your own messages, at the prompt:

    1. Type a message and press ENTER. Alice will send the message to Bob.

    2. Set the focus to the Bob window, type a message, and press ENTER. Bob will send the message to Alice.

    3. Repeat the two previous steps to send additional messages.

  7. When you want to quit, press ENTER in Alice's or Bob's window when it is in send mode (indicated by the :> prompt).

    The three windows will be reset to their states in step 2.


    If you close a window by clicking the Close button, the windows may stop responding. You will have to close any open windows and restart the example.

  8. Run the example with different options. If you select verbose mode and version 2, 3, 4, or 5, you can compare the encrypted strings that Alice sends with the encrypted strings that Bob receives. They should match perfectly unless Mallory is enabled. However, notice that characters that are greater than 128 in the ASCII character set are represented by question marks (?) because they cannot be displayed.

Community Additions