Extracting the assembly public key from Phx.PEModuleUnit.Manifest.PublicKey

Oct 7, 2013 at 9:54 PM
For each assembly analyzed I am trying to retrieve the fully qualified assembly information. This information includes the assembly name, version, culture, and public key, as you would see when you do Assembly.FullName

I've had success getting this information by examining Phx.PEModuleUnit.Manifest for each assembly, but I have been unable to extract the 16 character public key string as seen in Assembly.FullName by using the information in Phx.PEModuleUnit.Manifest.PublicKey

Phx.PEModuleUnit.Manifest.PublicKey.Key is a byte[] of length 160, but I do not know how to map this back to the 16 character public key string.

Any help is appreciated!

Oct 11, 2013 at 7:56 AM
I've figured out how to do this. Here is code:
    private static byte[] GetKeyTokenFromFullKey(byte[] fullKey)
        SHA1CryptoServiceProvider csp = new SHA1CryptoServiceProvider();
        byte[] hash = csp.ComputeHash(fullKey);
        byte[] token = new byte[8];
        for (int i = 0; i < 8; i++)
            token[i] = hash[hash.Length - (i + 1)];
        return token;

    byte[] publicKeyBytes = moduleUnit.Manifest.PublicKey.Key;

    StringBuilder keyBuilder = new StringBuilder();
    foreach (byte b in GetKeyTokenFromFullKey(publicKeyBytes))
        keyBuilder.Append(b.ToString("x2", null));
    string publicKey = keyBuilder.ToString();