Removing the Strong Name from an assembly?

720 views
Skip to first unread message

Philip_L

unread,
Nov 1, 2008, 4:11:39 PM11/1/08
to mono-cecil
No matter what I do, I can't seem to figure out how to
programmatically remove the strong name from one of my signed
assemblies. Here's the test code:

var nameDef = sourceAssembly.Name;

// Remove the strong name
nameDef.PublicKey = new byte[0];
nameDef.PublicKeyToken = new byte[0];
nameDef.Flags = AssemblyFlags.SideBySideCompatible;
nameDef.HasPublicKey = false;

// Remove any strong name references
var module = sourceAssembly.MainModule;
foreach(AssemblyNameReference reference in
module.AssemblyReferences)
{
reference.PublicKeyToken = new byte[0];
}

When I try to load the modified assembly into memory, it gives me this
error:

System.BadImageFormatException: Could not load file or assembly '4880
bytes loaded from [Your Assembly Here], Version=2.0.3.38038,
Culture=neutral, PublicKeyToken=null' or one of its dependencies. An
attempt was made to load a program with an incorrect format.

Am I missing something here? Is there some other thing that I have to
do to the newly-unsigned assembly itself before I can load it into
memory?

Lotfi Gheribi

unread,
Nov 1, 2008, 4:28:12 PM11/1/08
to mono-...@googlegroups.com
I believe Reflexil, which uses Cecil, allows to remove strongname signatures. have a look at its source code.

Philip_L

unread,
Nov 1, 2008, 4:47:50 PM11/1/08
to mono-cecil
Actually, the code that I posted above is actually from Reflexil.
Here's the original code from CecilHelper.cs:

public static void RemoveStrongName(AssemblyDefinition asmdef)
{
asmdef.Name.PublicKey = new byte[0];
asmdef.Name.PublicKeyToken = new byte[0];
asmdef.Name.Flags = AssemblyFlags.SideBySideCompatible;
}

public static void
RemoveStrongNameReference(AssemblyNameReference andef)
{
andef.PublicKeyToken = new byte[0];
}

it's practically identical, and yet, there doesn't seem to be a way to
get it working...

Lotfi Gheribi

unread,
Nov 1, 2008, 4:56:45 PM11/1/08
to mono-...@googlegroups.com
           // Remove any strong name references
           var module = sourceAssembly.MainModule;
           foreach(AssemblyNameReference reference in
module.AssemblyReferences)
           {
               reference.PublicKeyToken = new byte[0];
           }


Why are you removing all strong name signatures of all assemblies referenced by this module?

Philip_L

unread,
Nov 1, 2008, 6:47:16 PM11/1/08
to mono-cecil
It was just test code--I wanted to see if that had any effect on the
strong name signature itself since I was running out of ideas.

Anyway, back to the original question--is there something missing
here, or is the code in the RemoveStrongName() method all I need to
use to strip an assembly of its strong name?

Wicky

unread,
Nov 5, 2008, 7:57:18 AM11/5/08
to mono-cecil
Try this:

AssemblyNameReference anr;
...
anr.Flags &= ~AssemblyFlags.PublicKey;
anr.HashAlgorithm = AssemblyHashAlgorithm.None;
anr.PublicKey = null;

Lotfi Gheribi

unread,
Nov 5, 2008, 11:27:03 AM11/5/08
to mono-...@googlegroups.com

Try this:

       AssemblyNameReference anr;
               ...
               anr.Flags &= ~AssemblyFlags.PublicKey;
               anr.HashAlgorithm = AssemblyHashAlgorithm.None;
               anr.PublicKey = null;

Works on Mono but doesn't seem to work for the .Net :(

The unsigned resulting assembly doesn't load and does not pass the PEVerify (I get the message "File not found or has bad headers.")

I tried to disassemble/reassemble the assembly using ILDASM/ILASM... the resulting assembly DOES work !!!!


Jb Evain

unread,
Nov 5, 2008, 11:32:38 AM11/5/08
to mono-...@googlegroups.com
Hey,

That looks like a Cecil issue indeed, I'll investigate.


--
Jb Evain <j...@nurv.fr>

Jb Evain

unread,
Nov 6, 2008, 11:17:32 AM11/6/08
to mono-...@googlegroups.com
Hey,

On 11/5/08, Jb Evain <j...@nurv.fr> wrote:
> That looks like a Cecil issue indeed, I'll investigate.

It should be fixed in svn.

--
Jb Evain <j...@nurv.fr>

Reply all
Reply to author
Forward
0 new messages