Sunday, May 23, 2010

Difference PublicKey vs PublicKeyToken

Hi Friends,

We all have seen the PublicKeyToken in the web.config file. Most of the begineers will assum that "When we sign the assambly, it will have PublicKeyToken for the signed it. And the PublicKeyToken is the public key for the assambly." Well this is not true.

We all know that we can sign an assambly with .snk file. For those who don't know how to generate a sign key, you can generate a new key with sn.exe tool shipped with Visual Studio.

Now the .snk file contains PublicKey and a PrivateKey for the assambly to encrypt/decrypt the data.
Now the actual PublicKey is a 128 byte opaque algorithm specific structure. It also have extra 32 byte data for headers.

Now the question is, if PublicKey is 128 + 32 bytes then why we write only 16 characters for PublicKeuToken in web.config. What is the difference between PublicKey and PubilcKeyToken.
PublicKeyToken is a 8 byte hash for the full PublicKey.

Most of compilers use this token instead of full PublicKey to keep the assembly's manifest smaller.

Hope this information will be helpful to all.