Auto-entering Password In Sn.exe

HJ. picture HJ. · Jun 29, 2010 · Viewed 8k times · Source

I need to create post build event to perform the following:

sn -i MyKey.pfx MyKeyContainerName
tlbimp $(ConfigurationName)\MyCom.tlb /out:$(ConfigurationName)\NETMyCom.dll /keycontainer:MyKeyContainerName
sn -d MyKeyContainerName

When the Visual Studio executes the 1st statement it requires a password and waits until the user specifies it and fails.

Microsoft (R) .NET Framework Strong Name Utility Version 2.0.50727.42 Copyright (c) Microsoft Corporation. All rights reserved.

Enter the password for the PKCS#12 key file: Failed to parse the PKCS#12 blob in mykey.pfx -- The handle is invalid.

I tried to specify the password using sn command line arguments, but I could not see a way to do it.

Please help.

Regards, Hilmi.

Answer

Sean picture Sean · Jun 1, 2012

if like me, you are not using TFS or MSBUILD to build, then there are at least 2 other ways:

a) run sn.exe from a script, and write the password to stdin

see here for a C# example:

note: with the .NET4 version of sn.exe it seems to be impossible to execute it as external process (at least on Windows XP), and write the password to stdin (I tried with python + with C#, and sn.exe seems to just exit without waiting for password input).

b) use sn.exe to re-sign the password, using a pfx that has already been installed.

If you have already installed the pfx file, then you might know the container name (usually Visual Studio uses a name like VS_KEY_ABAB1234ABAB1234)

If, like me, you do not know or remember the container name, then just re-install the pfx file:

sn -i myPfxFile VS_KEY_ABAB1234ABAB1234

sn.exe will prompt you for a password, as it installs the certificate in the pfx file.

You can then make sn.exe re-sign your assembly, without any prompt for password:

sn -Rca myAssembly.dll myVSkey

The above can be used in a build script, as no interaction is required :-)

NB remember to check that the signing actually works:

sn -v myAssembly.dll