Finding the Public Key Token

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. 



One Response to Finding the Public Key Token

  1. […]     First, the obvious thing to do is to create a new solution that contains a Web Part Project, and to modify the web part as in my previous post so that it will compile properly. Then build it (but do not yet deploy!) and find the public key token, as outlined in my other post here. […]

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: