Share via


Code Analysis of the Utility Classes (CNG Example)

The Utilities.cs file in the Cryptography Next Generation (CNG) secure communication example provides application-wide control flags and methods that are used by the Alice, Bob, and Mallory projects. These flags and methods are discussed in the following sections:

  • Version, fVerbose, and fMallory Flags

  • Autoloader method

  • InitConsole method

  • SplashScreen method

  • ReadALine and ReadAChar methods

  • InitializeOptions method

  • Display methods

For an overview of the example, see Cryptography Next Generation (CNG) Secure Communication Example.

Version, fVerbose, and fMallory Flags

  • Version: An integer value that determines which security model is demonstrated, as defined in the following table.

    Version

    Security model

    1

    No encryption (plaintext messages).

    2

    Encrypted messages.

    3

    Nonsecure digital signatures used with cryptographic keys and encrypted messages.

    4

    Secure digital signatures used with cryptographic keys and encrypted messages.

    5

    Program termination upon security errors.

  • fVerbose: A Boolean value that controls display output. A value of true displays all messages in ASCII and encrypted format. A value of false displays messages in only ASCII format.

  • fMallory: A Boolean value that controls whether the Mallory project is enabled. A value of true runs the example with Mallory. A value of false runs the example without Mallory's interception. You can use this flag for a convenient before-and-after analysis of a man-in-the-middle attack.

Autoloader Method

static bool Autoloader()

This method loads Bob.exe and Mallory.exe. It is called by Alice at the beginning of her Main method.

The autoloader detects whether Alice was loaded as a stand-alone application or by Visual Studio. It does this by calling the Process.GetProcessesByName(String) method to obtain a Process component that is associated with the Alice.exe process. 

  • If the autoloader obtains a Process component, Alice is a stand-alone application. Alice starts Bob.exe and Mallory.exe by calling the Process.Start(String) method for Bob and Mallory. An exception is thrown if Bob.exe or Mallory.exe cannot be found.

  • If the autoloader does not obtain a Process component, this indicates that Alice.exe was loaded by Visual Studio. In this case, you must manually run Bob.exe and Mallory.exe by loading them with two separate instances of the Visual Studio debugger.

See How to: Build and Run the CNG Example for more information about the autoloader.

InitConsole Method

static void InitConsole(string name, int left, int top)

This method accepts a string and two integers. The string is used to title the console window. The integers represent the default left and top pixel coordinates to position the console window on the screen.

Each window is positioned according to the default coordinates. Alice's window is positioned in the upper-left corner of the screen. Bob's window is positioned in the upper-right corner of the screen. Mallory's window is positioned below Alice's and Bob's windows.

Two platform invoke declarations provide access to the unmanaged Win32 functions MoveWindow and GetConsoleWindow, which perform the actual positioning.

SplashScreen Method

static void SplashScreen()

This method clears the console and provides titles for the Alice, Bob, and Mallory windows.

ReadALine and ReadAChar Methods

static string ReadALine(bool fBlankOkay)
static string ReadAChar(string options)

These small utility methods work together to wrap Console.ReadLine calls.

ReadALine accepts a Boolean value. If the value is true, ReadALine returns if the user presses the ENTER key. If the value is false, ReadALine will not return until the user types a minimum of one character and then presses the ENTER key.

ReadAChar accepts a string of characters and returns when the user enters one of the characters in the string.

InitializeOptions Method

static string InitializeOptions()

This method presents you with a menu for setting the Version, fVerbose, and fMallory flags described previously in this topic. In addition, the user can select the value "x" to close the application.

Display Methods

The Utilities.cs file provides two Display methods:

  • static void Display(string s)

    This method passes its string and the current MyColor parameter to the second Display method overload.

  • static void Display(string DisplayString, int color)

    This method wraps Console.WriteLine calls and provides color-coded output. The use of colors makes it easier for you to track the owner of each message. For example, Alice's messages are displayed in green in all three windows. The following table lists the colors that are used in the example and their meanings.

ID

Color

Use

0

Red

Security error messages.

1

Yellow

Options menu and application messages.

2

White

Bob's messages.

3

Cyan

User input.

4

Green

Alice's messages.

5

Purple

Mallory's messages.

6

Yellow

Application restart prompt.

7

Gray

Encrypted message data.

See Also

Reference

Console

ConsoleColor

Concepts

Cryptography Next Generation (CNG) Secure Communication Example

Utilities.cs Source Code (CNG Example)

Source Code Overview (CNG Example)

Other Resources

Marshaling Data with Platform Invoke