Finding the Public Key Token

March 5, 2009

Posted By: Chris Zwarts

Microsoft uses a system of public keys in order to escape the problems of an earlier time, when any assemblies that had the same name, culture and version would interact badly or cause the assemblies to operate incorrectly. The solution was to give each strongly named assembly a public key, unique from all the other dlls. This public key is huge, 1024 bits in length, to ensure that there is minimal chance of duplicate keys. In order that this key be easily referenced (1024 bits is simply to long to work with), Microsoft uses an algorithm to generate a public key token from this key, which can be used in your programs. However, the algorithm is unclear. It is hard to manually generate a public key token. Luckily, Visual studio includes a command prompt tool called Sn.exe, which can be used to extract the public key either from the snk file in your project or from the dll after compilation

Using the Command Prompt

    This is the hard way to do things, but it offers you far more control over the finer elements of strong naming, if you need them. If all you need is the key token, skip this and go to the next heading “Using the Tools Menu”.

    Using the Command Prompt, you can access the public key token in one of 2 ways, depending upon whether or not the program has been built yet. Both of these methods require the Sn.exe tool. Open a visual studio command prompt from start>Program Files>Microsoft Visual Studio>Tools>Command Prompt. Navigate to your project folder, using the dir and cd commands like usual.

    Once you have found your project, you can extract the key. To extract it from the dll, navigate into the build, find the dll, and run >sn.exe -Tp “myassem.dll” (Obviously replacing myassem with the name of your assembly.). Notice the uppercase T in -Tp.

    To extract it from the snk file, navigate to it’s folder and run the following

command: >sn.exe -tp “mystrnm.snk” (Also replacing the name with your own.). Notice the lowercase t.

    Each of these commands will bring up the entire public key (wow it’s long, huh?) as well as, at the very bottom, the much-easier-to-handle public key token. It can be copied out and used as required.

Using the Tools Menu

     This method is far easier if you simply want to extract the token, nothing more. Open up the tools menu in visual studio, go to external tools and click add. In this box, enter the following lines:

Name:        Public Key Extractor

Command:   C:\Program Files\Microsoft Visual Studio #\SDK\v#.#\Bin\sn.exe.

Arguments: -Tp “$(TargetPath)”

 
(If you have more than one version in the SDK path, find the most recent one that has the SN.EXE file in it.)

Uncheck all the boxes, except for the use of the output window.

Now, going to the tool menu, you will find that there is a new command called Public Key Extractor. This tool will find the PKT for any built project. 

Sources:
http://andrewconnell.com/blog/archive/2006/09/15/4587.aspx