Getting an Assembly's Public Key the Programmatic way

I have been struggling with the dumbest problem all day. I have been working with extending the XHEO|Licensing system with a new licensing limit for a new product that I'm working on. Basically, it limits the assembly to be used only by a specific calling assembly. Anyways, I was looking for a way to get the public key and public key token of an assembly programmatically, and after some hunting through the System.Reflection namespace, I found it. It really sucks though, because I went through the MSDN docs for the AssemblyName.GetPublicKeyToken, and it had an example of almost exactly what I needed The problem is, it didn't work

Here is the code I used, and the output that followed.

Function Test2() As String
    ' Get the assemblies loaded in the current application domain.
    ' Get the dynamic assembly named 'MyAssembly'.
    Dim myAssembly As
[Assembly] = [Assembly].GetCallingAssembly
    Dim sb As New
StringBuilder
    Dim i As
Integer
    If Not (myAssembly Is Nothing)
Then
        sb.Append("AssemblyName: ")
        sb.Append(myAssembly.GetName.FullName)
        sb.Append("<br>Public Key: ")
        Dim pk As Byte
() = myAssembly.GetName().GetPublicKey()
        For i = 0 To
(pk.Length - 1)
            sb.Append("{0:x}", pk(i))
        Next
i
        sb.Append("<br>")
        sb.Append("<br>Public Key Token: ")
        Dim pt As Byte
() = myAssembly.GetName().GetPublicKeyToken()
        For i = 0 To
(pt.Length - 1)
            sb.Append("{0:x}", pt(i))
        Next
i
    End
If
    Return
sb.ToString
End Function

AssemblyName: myAssemblyTest, Version=1.0.1524.22437, Culture=neutral, PublicKeyToken=f925674905014eec
Public Key: {{{{{{{{{{{{{{{{{{{{{{{{{{{{.....etc.
Public Key Token: {{{{{{{{{{{{{{{{....etc.

I wasn't sure why the MS-given sample didn't work. With about an hour of experimentation (should have been less, but oh well), I finally stumbled upon the answer. It seems that they were using the ASP.NET syntax for formatting. The correct code for cycling through the byte arrays is as follows:

Function Test2() As String
    ' Get the assemblies loaded in the current application domain.
    ' Get the dynamic assembly named 'MyAssembly'.
    Dim myAssembly As
[Assembly] = [Assembly].GetCallingAssembly
    Dim sb As New
StringBuilder
    Dim i As
Integer
    If Not (myAssembly Is Nothing)
Then
        sb.Append("AssemblyName: ")
        sb.Append(myAssembly.GetName.FullName)
        sb.Append("<br>Public Key: ")
        Dim pk As Byte
() = myAssembly.GetName().GetPublicKey()
        For i = 0 To
(pk.Length - 1)
            sb.Append(pk(i).ToString("x"))
        Next
i
        sb.Append("<br>")
        sb.Append("<br>Public Key Token: ")
        Dim pt As Byte
() = myAssembly.GetName().GetPublicKeyToken()
        For i = 0 To
(pt.Length - 1)
            sb.Append(pt(i).ToString("x"))
        Next i
    End
If
    Return
sb.ToString
End Function

This function had slightly better results:

AssemblyName: myAssemblyTest, Version=1.0.1524.22437, Culture=neutral, PublicKeyToken=f925674905014eec
Public Key: 02400480009400062000240052534131040010.... etc.
Public Key Token: f9256749514eec

As you can see, the Public Key Tokens match. The “x” formatting string makes the ToString method kick out hexadecimal code instead of normal text. In case you didn't know, you can use lots of other formatting tokens as well. The documentation on the subject kinda sucks, so I'm working on an article on the subject, publish date TBD.

There you have it. Hope that code comes in handy for someone. Speaking of publishing, Builder.com has put all their new content on hold for a little while, so I'm shopping around for a new publisher. If any of you know of any sites that pay for articles, please let me know. I have 4 that just need a final polish up and they are good to go. Builder has been great, but they've been slipping ever since they merged with TechRepublic. It took nearly seven weeks to get my last paycheck.

7 Comments

Comments have been disabled for this content.