For a web application, I would like to create a simple but effective licensing system. In C#, this is a little difficult, since my decryption method could be viewed by anyone wi
Wolfwyrd's answer is excellent, but I just wanted to offer a muich simpler version of Wolfwyrd's GetPublicKeyFromAssembly
method (which uses a lot of helper methods from the library Wolfwyrd graciously supplied).
In this version, this is all the code you need:
///
/// Extracts an RSA public key from a signed assembly
///
/// Signed assembly to extract the key from
/// RSA Crypto Service Provider initialised with the public key from the assembly
private RSACryptoServiceProvider GetPublicKeyFromAssembly(Assembly assembly)
{
// Extract public key - note that public key stored in assembly has an extra 3 headers
// (12 bytes) at the front that are not part of a CAPI public key blob, so they must be removed
byte[] rawPublicKeyData = assembly.GetName().GetPublicKey();
int extraHeadersLen = 12;
int bytesToRead = rawPublicKeyData.Length - extraHeadersLen;
byte[] publicKeyData = new byte[bytesToRead];
Buffer.BlockCopy(rawPublicKeyData, extraHeadersLen, publicKeyData, 0, bytesToRead);
RSACryptoServiceProvider publicKey = new RSACryptoServiceProvider();
publicKey.ImportCspBlob(publicKeyData);
return publicKey;
}