Programming Examples

Are you a Programmer or Application Developer or a DBA? Take a cup of coffee, sit back and spend few minutes here :)

Strong Name Tool – Private & Public

1. Strong Name Tool – SN

Dotnet provides dll and exe in the form of assemblies. We can build the assemblies with Strong Names so that only authorised persons can make code change. DotNet provides a tool called Strong Name tool and using that we can create Security Keys. In this example, we will learn the following:

  1. Create Private-Public Key Pairs.
  2. Extract only public key from the Pair.
  3. View Public Key & Token
  4. Store the Keys in a Key Container

2. Generating Key-Pairs using Strong Name Exe

The below command generates the Key-Pair and stores that in a file called TestKey.snk on a folder C:\Temp2.

Fig 1. Generating Key Pairs
Fig 1. Generating Key Pairs using Strong Name Tool

Explanation

  1. SN – is the command and it stands for Strong Name.
  2. K – This switch specifies that we want to generate the Key-Pair.
  3. Name and Location of the Key Pair file.
  4. Shows that Key Pair was written successfully.

The Key-Pair is a combined key file which includes both Private and Public keys. From this Key-Pair combination, we can also fetch the public key and store that in a separate file.

3. Extract Public Key from Key Pair File

In the previous section, we generated the Key File which is a combination of public as well as private keys. The below command extracts public key from key pair and stores that in a separate file.

Fig 2. Extract Public Key from Key Pair
Fig 2. Extract Public Key from Key Pair

Explanation

  1. The command – Strong Name
  2. The switch -p tells, we want to extract public key from the Key file.
  3. Name of the source file. Note, this was file we created in the previous section which contains both public and private keys.
  4. This tells under what location we want to save the public key file. In our case we named this file as TestPublicKey.snk
  5. When all goes well, we will see a message stating public key is ready in a separate file.

Now, we have a separate file for the public key. How do we see the content?

4. View the Token & Hashing of Public Key File

With strong name key, we can supply the switch -tp to view the public key content. For more details look at the command below:

Fig 3. Viewing the Public Key and Key Token
Fig 3. Viewing the Public Key and Key Token via Strong Name Tool

Explanation

  1. The switch -t tells we want to view the public key token only. A token is short form the public key for easy identification. But it is not the actual key.
  2. Name of the Public Key file which stores the public key.
  3. Shows simplified version of the public key often called as Key Token.
  4. The switch -tp tells we want to see complete Hashing of the public key as well as the simplified token.
  5. Name of the Public key file which we want to view.
  6. Hash Code of the public key. This is complex to compare and time consuming for human eyes. For computers it is easy. For this same reason, we have the public key token if we need to check public key is right one.

5. Using Key Container via Strong Name

Public key can be shared and there is no need to protect it. But the same is not true with the private key and when it is in wrong hand, they can modify your assembly and pose a security threat to your customer. So, it is good idea to keep the combined key file in a Key Container which will be taken care by the windows security system. Once the Key Pair is in the container, the file can be deleted. Now look at the command below:

Fig 4. Storing the key in Key Container
Fig 4. Storing the key in Key Container

Explanation

  1. The switch -I tells that we want to install the key file into a container.
  2. Name of the key file which contains both private and public keys.
  3. Name of the container in which the key file will be guarded.
  4. The command output tells that Key-pair is kept in the container.

Once you see the message, you can delete the Key File say TestKey.snk. This way we can protect the private key bundled in the Key file. If you do not want the container, you can issue the delete command. This will delete all the key files in a container. Below is the command:

Fig 5. Deleting the Key Container
Fig 5. Deleting the Key Container

Explanation

  1. The switch -d tells delete the container
  2. Name of the container. Note, if the container has more than one key file, all will be deleted.
  3. After successful execution of the command, the message indicates Key Container is deleted. This infers that, all the keys it had also got deleted.

6. Summary

The generation and storing of the private and public key are useful when you want to deal with the security. This will be useful when you want to build the assembly with a Strong name key so that you claim the whole proprietary of the code.

Categories: C#

Tags: , , , ,

Do you like this Example? Please comment about it for others!!

This site uses Akismet to reduce spam. Learn how your comment data is processed.