Home   Cover Cover Cover Cover
 

Strong names

The following command generates a key file with a pair of keys (public/private).

  sn /k mykey.snk

The generated file mykey.snk looks like this (in hexadecimal form):

/csbook/solutions/13/mykey.snk
000000   07 02 00 00 00 24 00 00 52 53 41 32 00 04 00 00   .....$..RSA2....
000010   01 00 01 00 5F 2B 50 2B D4 44 C2 3E 8C 2B 9D 7D   ...._+P+.D.>�+.}
000020   7A EC 69 39 26 2C 5E 72 D8 A2 58 33 87 36 0F 31   z.i9&,^r..X3�6.1
000030   16 F1 CE C0 F7 D5 5F 11 08 D8 F1 59 E9 7B D3 25   ......_....Y.{.%
000040   B2 4F 96 AC 00 CB 49 2B 9C 4F 88 DC 38 1C F1 2F   .O....I+.O�.8../
000050   4C BA D5 02 BF 79 F4 81 69 D1 6D D1 EE 48 92 B2   L....y.�i.m..H�.
000060   56 66 08 0F C9 AC 7A 88 81 E6 8D 1B 02 6C F9 95   Vf....z��.�..l.�
000070   98 E5 24 C1 A0 1C 28 7B 68 B1 63 90 43 76 C3 F4   ..$...({h.c�Cv..
000080   30 0E 7F C6 B9 1E E4 E9 32 E1 FB 33 C9 10 BF AD   0.......2..3....
000090   1D DE 2B C7 F3 D7 2C 62 A9 1C C6 90 B1 09 C9 8C   ..+...,b...�...�
0000A0   0A EA F3 4D 11 B8 8B 0A 33 2E AF 38 2A D5 CB 78   ...M..�.3..8*..x
0000B0   3D 65 49 3A E4 59 7F B4 21 58 49 55 8F F1 9B 48   =eI:.Y..!XIU�.�H
0000C0   82 8A CF ED D4 69 95 2D CE DB B2 70 19 0B 91 5A   ��...i�-...p..�Z
0000D0   EE 4A F5 F9 E5 C5 27 8B DF 2A 16 6C C6 C3 D4 80   .J....'�.*.l...�
0000E0   AA DA 15 07 25 B3 5D 29 AB B1 DD C1 C3 25 F4 09   ....%.]).....%..
0000F0   B6 FB F3 10 A7 C7 12 2B 46 35 2D 77 C6 1F B5 79   .......+F5-w...y
000100   19 86 A5 B7 8C 83 B9 A9 89 28 BF AE 46 15 7D 09   .�..��..�(..F.}.
000110   21 50 FC CB DD 58 BB F2 2A 9B CB D5 39 D0 66 9D   !P...X..*�..9.f.
000120   50 8E 5A 12 AC 19 F2 15 07 33 37 85 68 B2 2F C7   P�Z......37�h./.
000130   4F E4 DC EB DA 4E 80 25 6B EF 3B C4 B4 09 64 FC   O....N�%k.;...d.
000140   77 81 73 C6 B5 B7 A7 24 AA D9 0D 43 32 6F 91 C6   w�s....$...C2o�.
000150   E1 FB 5B 7C 5D 03 BE 64 97 0B C5 6F B7 13 36 7A   ..[|]..d...o..6z
000160   12 96 EE 11 8D 4C C9 FF 4B 3E CC 5C 6C 0D E0 11   ....�L..K>.\l...
000170   4C 7B D7 37 B9 C0 49 77 FC B1 34 46 DC A6 7B 52   L{.7..Iw..4F..{R
000180   F8 D8 EB 68 AF 51 18 AD 14 EF C1 F1 C4 99 02 9D   ...h.Q..........
000190   72 B2 A2 66 74 82 FB 55 79 3B 19 7A 1B 8E 5E CE   r..ft�.Uy;.z.�^.
0001A0   98 81 1C 46 FC 3B 2E 32 52 97 3C C5 F4 E0 C6 F5   .�.F.;.2R.<.....
0001B0   2C CE E0 96 D7 0C 18 A8 E8 1B 54 C2 4A 50 ED FD   ,.........T.JP..
0001C0   4A 88 69 D5 AE 2E 27 80 14 45 5C 5E 8F F2 58 23   J�i...'�.E\^�.X#
0001D0   A3 43 01 4B A1 A6 10 07 09 74 39 E3 9A C3 F7 D1   .C.K.....t9.....
0001E0   C4 F5 26 0C 5C CF 41 A0 A3 19 4F AA 86 17 43 43   ..&.\.A...O.�.CC
0001F0   11 13 07 35 37 46 13 74 79 4D B0 04 DE EC 36 FB   ...57F.tyM....6.
000200   93 CE 87 73 C2 7B D7 BB 79 07 F7 93 74 D2 30 83   �.�s.{..y..�t.0�
000210   EB D6 13 9F A4 6A 51 B8 EF 40 73 D9 04 5A 7F E2   .....jQ..@s..Z..
000220   02 CD 6D 92 36 8C 97 38 3F B0 2A 9A 35 63 8B 9B   ..m�6�.8?.*.5c��
000230   A0 43 58 7B C3 D3 DC 4A 88 E9 AF 75 61 76 D7 D0   .CX{...J�..uav..
000240   0A 1B 99 A7 DD 5D FF 9B 31 4F 6C AC E6 4B 6B 48   .....].�1Ol..KkH
000250   5F D6 2F 34                                       _./4

We can now sign our assembly with the private key by specifying the name of the key file in the AssemblyKeyFile attribute:

/csbook/solutions/13/A05.cs
using System.Reflection;

[assembly:AssemblyKeyFile("mykey.snk")]
class MyClass {
  int x;
  public void Foo() {}
  public void Bar() {}
}

Now we compile our program into a DLL:

  csc /target:library A05.cs

Finally we store the assembly in the Global Assembly Cache. We can do this either by Drag & Drop (i.e. we use the mouse to drag A05.dll into the directory c:\WINNT\assembly) or by the following command:

  gacutil /i A05.dll

The signed assembly can then be used by other applications.