Loading ...

How to add a strong name to an existing DLL or assembly | CodeAsp.Net

How to add a strong name to an existing DLL or assembly

 /5
0 (0votes)

In this article I will show you how to add a strong name to an existing dll. The reason to sign an assembly is so ensure that it is your assembly. Since the private key is yours, no one else can sign that assembly with that same key. When the public key of an assembly is the one you know (the one which you original dll has), the assembly is yours and it has not been tampered. Let's add a strong name to an existing dll:

Basically we need to do the following 4 steps in order to add strong name to an existing dlls:

  1. Generate a KeyFile.
  2. Generate the MSIL for the assembly.
  3. Rename the original assembly.
  4. Generate the dll from the keyfile and MSIL for the assembly.

Before starting the above mentioned steps I would suggest you to put your assembly at following path "D:\\assemblies". The path can be different but for this article I will keep that path as a reference. For this example I will use TikaOnDotNet.dll for which I have to add strong name.

Let's cover the above 4 points one by one.

Generate a keyfile

Open the Start >> All Programs >> Microsoft Visual Studio 2010 >> Visual Studio Tools >> Visual Studio Command prompt. Open it with "Run as admin" privileges.

 

Run the following command to generate the keypair file:  

sn -k D:\\assemblies\\keypair.snk

Running the above command will create a new file named keypair.snk in D:\\assemblies folder.

 

Generate the MSIL for the assembly

After creating the keypair.snk you need to generate the MSIL for your dll. For that run the following command:

ildasm D:\\assemblies\\TikaOnDotNet.dll /out:D:\\assemblies\\TikaOnDotNet.il

 Running the above command will create a new file named TikaOnDotNet.il in D:\\assemblies folder.

 

Rename the original assembly

Since we need to create a new dll with same name at same location we should rename the old dll.

Above, I have renamed the TikaOnDotNet.dll to TikaOnDotNet_old.dll. This step is optional if you are generating the signed dll with same name at different location.

Generate the dll from the keyfile and MSIL for the assmbly

This is the final step where we will generate a new dll (strong name one) with the help of keypair and msil file we generated above. Run the following command:

ilasm D:\\assemblies\TikaOnDotNet.il /dll /key=D:\\assemblies\\keypair.snk

After running the above command a new file named TikaOnDotNet.dll is generated. This dll will have strong name.

 

Summary:

Here is the summary of command which you need to run to add strong name to an existing dll:

  1. sn -k keypair.snk
  2. ildasm assembly.dll /out:assembly.il
  3. ilasm assembly.il /dll /key= keypair.snk

 

 

Comments (no comments yet)

Top Articles