Click here to Skip to main content
13,355,726 members (86,665 online)
Click here to Skip to main content
Add your own
alternative version


25 bookmarked
Posted 4 May 2004

Parsing strong name signatures generated with sn.exe

, 4 May 2004
Rate this:
Please Sign up or sign in to vote.
This article shows how to parse Assembly "strong name keyfiles" generated with sn.exe


This application shows how to parse Assembly "strong name keyfiles" generated with sn.exe.


System.Security.Cryptography provides functions to work with RSA keypairs. These functions are wrappers on the Win32 Crypto API. However, the methods provided in the 1.1 Framework do not easily parse SNK keyfiles created by sn.exe. The reason is that none of the methods will accept a PRIVATEKEYBLOB or PUBLICKEYBLOB structure directly. Therefore, it is necessary to write some extra code to parse those structures. Furthermore, there are additional (some would say obscure) tweaks on CpParameters required to get an SNK keyfile to import correctly.

Using the code

You can make a new assembly keypair by running sn.exe which is available from the Framework SDK. To make a new keypair run "sn.exe -k keypair.snk". To export just the public key from your keypair, use "sn.exe -p keypair.snk".

The app I wrote works pretty much as you would expect. You can create new keys, or open an SNK/PUB keypair files by pressing the buttons. If you are using a public key file, the "sign it" button will be disabled. You can also open an XML file with an encoded RSA keypair.

After your keypair is loaded, type some text on the left, hit "sign it", and then hit "verify it". Test that the signature verification really works by twiddling one character in the signature or in the original plaintext, then hitting "verify it" again - it should fail.

The guts of the app lie in RSA1024Util.cs. I hardcoded the class to use 1024 bit encryption because currently sn.exe only generates 1024 bit keyfiles. (As an aside, I wonder why 2048 bits aren't supported for assembly keys? Cryptographers have lately been warning about the eventual fall of 1024 bit keys..)

Here is the function that converts the SNK byte buffer to RSAParameters. If the buffer is "public length" - 160 bytes - then it will only fill in the public key fields, "exponent" and "modulus". Otherwise it will read the rest of the private key fields.

The byte offsets were discovered by digging around the Win32 CryptoAPI documentation and helpful articles by Dr. Michel I. Gallant, of . Note that each byte array must be reversed due to a big endian vs. little endian ordering issue.

public const int _magic_priv_idx = 0x08; 
public const int _magic_pub_idx = 0x14; 
public const int _magic_size = 4; 

public static RSAParameters FigureParams(
byte[] keypair)
    RSAParameters ret = new RSAParameters(); 

    if ((keypair == null) || (keypair.Length < 1)) 
        return ret; 

    bool pubonly = SnkBufIsPubLength(keypair); 

    if ((pubonly) && (!CheckRSA1(keypair)))
        return ret; 

    if ((!pubonly) && (!CheckRSA2(keypair)))
        return ret; 

    int magic_idx = pubonly ? _magic_pub_idx : _magic_priv_idx;

    // Bitlen is stored here, but note this 
    // class is only set up for 1024 bit length keys 
    int bitlen_idx = magic_idx + _magic_size; 
    int bitlen_size = 4; // DWORD

    // Exponent 
    // In read file, will usually be { 1, 0, 1, 0 } or 65537
    int exp_idx = bitlen_idx + bitlen_size; 
    int exp_size = 4; 

    //BYTE modulus[rsapubkey.bitlen/8]; == MOD; Size 128 
    int mod_idx = exp_idx + exp_size;
    int mod_size = 128; 

    //BYTE prime1[rsapubkey.bitlen/16]; == P; Size 64 
    int p_idx = mod_idx + mod_size; 
    int p_size = 64; 

    //BYTE prime2[rsapubkey.bitlen/16]; == Q; Size 64 
    int q_idx = p_idx + p_size; 
    int q_size = 64; 

    //BYTE exponent1[rsapubkey.bitlen/16]; == DP; Size 64
    int dp_idx = q_idx + q_size; 
    int dp_size = 64; 

    //BYTE exponent2[rsapubkey.bitlen/16]; == DQ; Size 64 
    int dq_idx = dp_idx + dp_size; 
    int dq_size = 64; 

    //BYTE coefficient[rsapubkey.bitlen/16]; == InverseQ; Size 64
    int invq_idx = dq_idx + dq_size; 
    int invq_size = 64; 

    //BYTE privateExponent[rsapubkey.bitlen/8]; == D; Size 128 
    int d_idx = invq_idx + invq_size; 
    int d_size = 128; 

    // Figure public params 
    // Must reverse order (little vs. big endian issue)
    ret.Exponent = BlockCopy(keypair, exp_idx, exp_size); 
    ret.Modulus = BlockCopy(keypair, mod_idx, mod_size); 

    if (pubonly) return ret; 

    // Figure private params 
    // Must reverse order (little vs. big endian issue)
    ret.P = BlockCopy(keypair, p_idx, p_size); 

    ret.Q = BlockCopy(keypair, q_idx, q_size); 

    ret.DP = BlockCopy(keypair, dp_idx, dp_size); 

    ret.DQ = BlockCopy(keypair, dq_idx, dq_size); 

    ret.InverseQ = BlockCopy(keypair, invq_idx, invq_size); 

    ret.D = BlockCopy(keypair, d_idx, d_size); 

    return ret; 

There is one other not-well-documented switch that needs to be thrown in order to get the SNK file to be imported correctly. Keys generated by sn.exe have the KeyNumber set to 2, which is "AT_SIGNATURE". For more information see

protected void CtorFromSnkBuf(byte[] snkbuf)
    if ((snkbuf == null) || (snkbuf.Length < 1))

    RSAParameters param = FigureParams(snkbuf); 

    // Must set KeyNumber to AT_SIGNATURE for strong 
    // name keypair to correctly be imported. 
    CspParameters cp = new CspParameters(); 
    cp.KeyNumber = 2; // AT_SIGNATURE 

    _rsa = new RSACryptoServiceProvider(1024, cp); 


Why did the Framework authors make it inconvenient to work with SNK files generated by sn.exe? I don't know, but in any case there's the code to work around that issue.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Per R. Anderson
Web Developer
United States United States
Per Anderson is the founder of Sunfrog Technologies LLC, .

You may also be interested in...

Comments and Discussions

Questionnot working Pin
Member 78618095-Apr-12 0:08
memberMember 78618095-Apr-12 0:08 
AnswerRe: not working Pin
Powerbauer13-Apr-12 0:17
memberPowerbauer13-Apr-12 0:17 
GeneralThank you! Pin
barrd2-Jun-11 19:24
memberbarrd2-Jun-11 19:24 
General"Keyset does not exist " at running time on .net 2.0 Pin
Lambuz17-May-11 4:59
memberLambuz17-May-11 4:59 
GeneralExcellent Work! Pin
Dweeb2318-Sep-08 2:08
memberDweeb2318-Sep-08 2:08 
GeneralRe: Excellent Work! Pin
Per R. Anderson7-Jan-10 16:16
memberPer R. Anderson7-Jan-10 16:16 
GeneralCool Pin
Anonymous17-Aug-05 14:51
sussAnonymous17-Aug-05 14:51 
GeneralRe: Cool Pin
SunshineAUS4-Mar-08 13:09
memberSunshineAUS4-Mar-08 13:09 
GeneralThanks. Here is an update. Pin
staceyw10-Nov-04 7:37
memberstaceyw10-Nov-04 7:37 
GeneralRe: Thanks. Here is an update. Pin
Per R. Anderson30-Nov-04 11:19
memberPer R. Anderson30-Nov-04 11:19 
GeneralCool stuff Pin
lbargaoanu10-Sep-04 4:24
memberlbargaoanu10-Sep-04 4:24 
GeneralExcellent! Pin
jaxterama20-Aug-04 9:46
memberjaxterama20-Aug-04 9:46 
GeneralRe: Excellent! Pin
jaxterama20-Aug-04 10:02
memberjaxterama20-Aug-04 10:02 
GeneralRe: Excellent! Pin
Per R. Anderson30-Nov-04 11:05
memberPer R. Anderson30-Nov-04 11:05 
Questionis possible to sign a binary file? Pin
freedeveloper10-Aug-04 15:41
memberfreedeveloper10-Aug-04 15:41 
AnswerRe: is possible to sign a binary file? Pin
Per R. Anderson30-Nov-04 11:03
memberPer R. Anderson30-Nov-04 11:03 
Generalcreating desired key pair Pin
Stanimir_Stoyanov2-Jul-04 10:05
memberStanimir_Stoyanov2-Jul-04 10:05 
GeneralRe: creating desired key pair Pin
staceyw11-Nov-04 9:28
memberstaceyw11-Nov-04 9:28 
GeneralQuestion about this tool and Verisign Pin
iLuvCSharp2-Jul-04 8:33
memberiLuvCSharp2-Jul-04 8:33 
GeneralRe: Question about this tool and Verisign Pin
Per R. Anderson30-Nov-04 11:13
memberPer R. Anderson30-Nov-04 11:13 
GeneralRe: Question about this tool and Verisign Pin
Anonymous30-Nov-04 14:28
sussAnonymous30-Nov-04 14:28 
General34343 Pin
Anonymous2-Jul-04 8:16
sussAnonymous2-Jul-04 8:16 
GeneralNice work Pin
WillemM10-May-04 23:41
memberWillemM10-May-04 23:41 
GeneralRe: Nice work Pin
Per R. Anderson12-May-04 8:16
memberPer R. Anderson12-May-04 8:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.180111.1 | Last Updated 5 May 2004
Article Copyright 2004 by Per R. Anderson
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid