English 中文(简体)
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.

最佳回答

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.

问题回答

You cannot decrypt using the public key. That way, the whole point of "public" would be lost.

(You might, however, be able to sign something using the private key, then verify the signature using the public key. That s what the framework uses the keys for - the assembly is signed, and the public key is used to verify the signature.)

This can be done using SignedXml http://msdn.microsoft.com/en-us/library/ms229745.aspx. At a lower level you can prob use RSAPKCS1SignatureDeformatter and RSAPKCS1SignatureFormatter. These work by encrypting a hash of the data then comparing the data with the (decrypted) hash the other end. I believe the hashing is used because private key encryption can only handle small data. Not sure about reusing the assembly public key, if it is causing problems just use a separate key pair.

Word of warning, check out this as these classes can result in 20 second hang ups! http://www.pcreview.co.uk/forums/thread-3428177.php

This approach is vulnerable to the signature verification code being tampered with using Reflexil but that is another matter.

I wrote the following but rereading I think you already got this: You aren t really trying to encrypt or hide data from the user, you want to stop them from creating or tampering the license. You are right that a public private key encryption algorithm can be used for this. This is known as Signing using a private key (server side license generation). And verification of the signature using a public key (license checking in the app). I mention this terminology as it ll help with research.

In RSA Public keys are used for encryption, private keys are used for decryption. You can t use a public key to decrypt anything...

In RSA the only actual difference between a public key and a private key is which one you keep secret.

So you can use a public key as the encryption key and decrypt with the private key, or use the private key as the encryption key and decrypt with the public key.

Encrypting with the private key is used for digital signatures (anybody can decode with the public key).

But as @Lasse V. Karlsen pointed out, .Net might make it more difficult than it should be...

I think both direction are possible encrypt with public and decrypt with private and encrypt with private key. The second is the way how digital signature works.

Warning! This answer is wrong but I m going to leave it here none-the-less because the series of comments attached are, I think of sufficient interest to others to keep the answer around. Ok it makes me look like an idiot but thats nothing new to me ;) Vote as you wish.

A public key can be used to:-

  • Encrypt something that can only be decrypted with the private key
  • Authenticate something signed with the private key

It can not be used to decrypt something to encrypted by a private key. Its for this reason that the Public/Private key system is refered to as an Asymetric system.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签