21

I found several solutions where I can use the .Net RSA Provider to Encrypt a message with the public key and Decrypt it with the private one.

But what I want to have is to Encrypt with the private key and Decrypt with the public key.

I want t store the public key in my app and encrypt a license for example on my dev machine with the private key, send it to the app and let the information decrypt with a public key.

How can I achieve that?

9
  • 1
    You can't. You can only encrypt with the public key and decrypt with the private key. What you mean is signature and verification, which is a valid application for a license. You get authentication, but no confidentiality. If you want to make that interactive you can of course achieve more.
    – Artjom B.
    Commented Dec 20, 2014 at 9:12
  • 5
    Asymmetric encryption/decryption: everyone can encrypt (using a public key), only the host can decrypt (using a private key). Asymmetric signature/verification: only the host can sign (using a private key), everyone can verify (using a public key). Commented Dec 20, 2014 at 9:23
  • 1
    @Zoya If you think that is a good article then you've just proven you completely missed the point about cryptography in general. Strange, your answer seems to indicate otherwise. Commented Dec 20, 2014 at 10:13
  • 1
    @Maarten Why? I read it and looked and the sample code. It was clear for me. I could understand how it works. But it would be great if you could explain a little bit why this code is bad or wrong about cryptography. ( i have to add that i have no knowledge about cryptography )
    – STORM
    Commented Dec 20, 2014 at 21:20
  • 1
    The security proofs of RSA rely on the padding used. This one uses a few (too few) bytes of padding, and the proprietary scheme has no security proof. Commented Dec 20, 2014 at 21:23

1 Answer 1

16

You can use signature generation, In which the private key is used to generate a signature that verifies that your message is authentic.

// Create message and signature on your end
string message = "Here is the license message";

var converter = new ASCIIEncoding();
byte[] plainText = converter.GetBytes(message);

var rsaWrite = new RSACryptoServiceProvider();
var privateParams = rsaWrite.ExportParameters(true);

// Generate the public key / these can be sent to the user.
var publicParams = rsaWrite.ExportParameters(false);

byte[] signature =
    rsaWrite.SignData(plainText, new SHA1CryptoServiceProvider());

// Verify from the user's side. Note that only the public parameters
// are needed.
var rsaRead = new RSACryptoServiceProvider();
rsaRead.ImportParameters(publicParams);
if (rsaRead.VerifyData(plainText,
                       new SHA1CryptoServiceProvider(),
                       signature))
{
    Console.WriteLine("Verified!");
}
else
{
    Console.WriteLine("NOT verified!");
}

you can take further help from HERE

6
  • 1
    I've removed the term "signature strategy" as I've never heard of signature generation to be described that way. Commented Dec 20, 2014 at 10:19
  • 7
    What if you wish to actually encrypt the data -- not just validate it's authenticity using a hash ? Commented Dec 31, 2016 at 6:33
  • @Syeda "Generate the public key / these can be sent to the user" Can you explain how ? What do I do with the RSAParameters object ? Thanks
    – KevinM
    Commented Sep 4, 2019 at 14:51
  • 1
    link is now dead Commented Oct 27, 2021 at 11:30
  • 2
    @RubensFarias I added a link to an archive copy Commented Jul 2, 2022 at 14:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.