Search code examples
c#phprsaencryption-asymmetriclicense-key

Can I use the assemblies PublicKey to decrypt a string encrypted with the corresponding PrivateKey?


Signing an assembly in .NET involves a public/private key pair. As far as I can tell from what I've read .NET uses the RSA algorithm and the private key to sign the assembly, checking it with the embedded public key.

I know how to retrieve the public key (Assembly.PublicKey). I was wondering, if that key could be used to decrypt a short string that contains some data encrypted with the private key.

The docs I've read so far (e.g.) seem to imply that only the other way round is possible: That I would have to use the public key to encrypt and the private key to decrypt - but I don't really want to include that in the assembly, do I.

I guess it would be ok, if I just signed the string. But how?

I'm a bit at a loss how to start this. Does anybody have a code snippet?

Also, encrypting / signing of the small string would ideally happen in PHP, since I want to offload that to a web server and all we have so far is your generic PHP/MySQL hosted website.

Use Case: I'm trying to come up with a lightweight licensing scheme for a software we are about to release to beta testers. Since the software will probably be freeware, all we really want to achieve is

  1. know who has the software installed (email address)
  2. let the software expire after a given period, after which the user will have to get a new license
    • this is as easy as filling out a form and waiting for an automated email with the key to arrive
    • we are trying to reduce the likelyhood of old versions coming back to bite our reputation / haunt us

Being able to encrypt a tuple (expiry date, fingerprint) and decrypt that at startup would make an easy licensing module: The first time the application is started, the user is asked for email address, name, organisation. This information is posted to the webserver along with an md5 fingerprint of some system info (nic, computer name, assembly major and minor version). The webserver answers by email (checks validity of email address) with an encrypted version of the tuple (expiry date, fingerprint) that is then saved to disk. On startup, this can be decrypted and compared with current date and regenerated fingerprint.

EDIT: OK, so I don't have all the answers to my question yet. But it looks like .NET won't make it easy to use the private key for encryption (if that is at all possible, the answers don't really agree on that).

The route I will take is this (based on my use case):

  • I will use the private key to sign the license.
  • I will use the public key to verify the license was signed by the private key
  • I will post another question aimed at PHP devs on how to use the .NET keys (produced by sn.exe) to sign some text
  • I am not really worried about the user seeing the license, as it is a hash anyway and computed from stuff he allready knows. All I want is to make it too hard to be worth any bother for your typical building architect to copy my software without me knowing (remember, the software will be freeware - all I want is a paper trail of who has it installed...)

Thank you very much for your answers.


Solution

  • Not in .NET.

    In many traditional public-key encryption algorithm, like RSA, you can encrypt and decrypt both ways, typically one way is called "encryption" and the other "signing", even though you actually end up with an encrypted version of something both ways.

    However, in .NET the RSA implementation has been crippled, and when signing will only produce digests of the input, not the full processed information.

    It seems there's some disagreement about what can and cannot be done with RSA, so let me edit my answer to be more specific.

    I'm talking about RSA math, not any particular RSA implementation.

    RSA math allows you to encode information either of the two keys (private or public), and the encoded data can only be decoded with the other of the two keys.

    Typically, you encode with a public key, encrypting the information, and decode it with the private key, decrypting the information. Or, you take a hash of the information, encode it with the private key, signing the hash, and decode the hash with the public key, in order to compare and verify the signature.

    Typical implementations does not allow one to do full encoding of data from private to public, only by hashing the data, but the math behind RSA fully allows this.